Test Failed
Push — master ( 427389...85a928 )
by Mike
04:01 queued 38s
created

RedisSessionHandler::doRead()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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