RedisSessionHandler::doRead()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
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\Redis\Business\RedisFacade 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 array $options
37
     */
38 1
    public function __construct(array $options = [])
39
    {
40 1
        $this->prefix = $options['prefix'] ?? 'session:xervice.';
41 1
        $this->ttl = $options['ttl'] ?? 86400;
42 1
        $this->sessionDataProvider = new RedisSessionDataProvider();
43 1
    }
44
45
    /**
46
     * @param string $sessionId
47
     *
48
     * @return string
49
     */
50 1
    protected function doRead($sessionId): string
51
    {
52
        try {
53 1
            $this->sessionDataProvider = $this->getFacade()->get($this->prefix . $sessionId);
54 1
        } catch (RedisException $exception) {
55 1
            $this->sessionDataProvider->setData('');
0 ignored issues
show
Bug introduced by
The method setData() does not exist on Xervice\DataProvider\Bus...r\DataProviderInterface. It seems like you code against a sub-type of Xervice\DataProvider\Bus...r\DataProviderInterface such as DataProvider\RedisSessionDataProvider or DataProvider\RedisSessionDataProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            $this->sessionDataProvider->/** @scrutinizer ignore-call */ 
56
                                        setData('');
Loading history...
56
        }
57
58 1
        return $this->sessionDataProvider->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on Xervice\DataProvider\Bus...r\DataProviderInterface. It seems like you code against a sub-type of Xervice\DataProvider\Bus...r\DataProviderInterface such as DataProvider\RedisSessionDataProvider or DataProvider\RedisSessionDataProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        return $this->sessionDataProvider->/** @scrutinizer ignore-call */ getData();
Loading history...
59
    }
60
61
    /**
62
     * @param string $sessionId
63
     * @param string $data
64
     *
65
     * @return bool
66
     */
67
    protected function doWrite($sessionId, $data): bool
68
    {
69
        $this->sessionDataProvider->setData($data);
70
        $this->getFacade()->setEx($this->prefix . $sessionId, $this->sessionDataProvider, 'ex', $this->ttl);
71
72
        return true;
73
    }
74
75
    /**
76
     * @param string $sessionId
77
     *
78
     * @return bool
79
     */
80
    protected function doDestroy($sessionId): bool
81
    {
82
        $this->getFacade()->delete($this->prefix . $sessionId);
83
84
        return true;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function close(): bool
91
    {
92
        return true;
93
    }
94
95
    /**
96
     * @param int $maxlifetime
97
     *
98
     * @return bool
99
     */
100
    public function gc($maxlifetime): bool
101
    {
102
        return true;
103
    }
104
105
    /**
106
     * @param string $session_id
107
     * @param string $session_data
108
     *
109
     * @return bool
110
     */
111
    public function updateTimestamp($session_id, $session_data): bool
112
    {
113
        $this->getFacade()->expire($this->prefix . $session_id, $this->ttl);
114
115
        return true;
116
    }
117
}
118