Passed
Push — master ( dae0f1...427389 )
by Mike
08:24
created

RedisSessionHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\Redis\Business\Model\Session;
6
7
8
use DataProvider\RedisSessionDataProvider;
9
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
10
use Xervice\Redis\Business\Exception\RedisException;
11
use Xervice\Redis\Business\RedisFacade;
12
13
class RedisSessionHandler extends AbstractSessionHandler
14
{
15
    /**
16
     * @var \Xervice\Redis\Business\RedisFacade
17
     */
18
    private $facade;
19
20
    /**
21
     * @var string
22
     */
23
    private $prefix;
24
25
    /**
26
     * @var int
27
     */
28
    private $ttl;
29
30
    /**
31
     * @var RedisSessionDataProvider
32
     */
33
    private $sessionDataProvider;
34
35
    /**
36
     * RedisSessionHandler constructor.
37
     *
38
     * @param \Xervice\Redis\Business\RedisFacade $facade
39
     * @param array $options
40
     */
41 1
    public function __construct(RedisFacade $facade, array $options = [])
42
    {
43 1
        $this->facade = $facade;
44 1
        $this->prefix = $options['prefix'] ?? 'session:xervice.';
45 1
        $this->ttl = $options['ttl'] ?? 86400;
46 1
        $this->sessionDataProvider = new RedisSessionDataProvider();
47 1
    }
48
49
    /**
50
     * @param string $sessionId
51
     *
52
     * @return string
53
     */
54 1
    protected function doRead($sessionId): string
55
    {
56
        try {
57 1
            $this->sessionDataProvider = $this->facade->get($this->prefix . $sessionId);
58 1
        } catch (RedisException $exception) {
59 1
            $this->sessionDataProvider->setData('');
60
        }
61
62 1
        return $this->sessionDataProvider->getData();
63
    }
64
65
    /**
66
     * @param string $sessionId
67
     * @param string $data
68
     *
69
     * @return bool
70
     */
71
    protected function doWrite($sessionId, $data): bool
72
    {
73
        $this->sessionDataProvider->setData($data);
74
        $this->facade->setEx($this->prefix . $sessionId, $this->sessionDataProvider, 'ex', $this->ttl);
75
76
        return true;
77
    }
78
79
    /**
80
     * @param string $sessionId
81
     *
82
     * @return bool
83
     */
84
    protected function doDestroy($sessionId): bool
85
    {
86
        $this->facade->delete($this->prefix . $sessionId);
87
88
        return true;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function close(): bool
95
    {
96
        return true;
97
    }
98
99
    /**
100
     * @param int $maxlifetime
101
     *
102
     * @return bool
103
     */
104
    public function gc($maxlifetime): bool
105
    {
106
        return true;
107
    }
108
109
    /**
110
     * @param string $session_id
111
     * @param string $session_data
112
     *
113
     * @return bool
114
     */
115
    public function updateTimestamp($session_id, $session_data): bool
116
    {
117
        $this->facade->expire($this->prefix . $session_id, $this->ttl);
118
119
        return true;
120
    }
121
}
122