SessionHandler::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BrainExe\Core\Application;
4
5
use BrainExe\Core\Annotations\Inject;
6
use BrainExe\Core\Annotations\Service;
7
use Predis\ClientInterface;
8
use Predis\Session\Handler;
9
10
/**
11
 * @Service("Core.Application.SessionHandler")
12
 */
13
class SessionHandler extends Handler
14
{
15
16
    private const KEY = 'sessions:';
17
18
    /**
19
     * @Inject({
20
     *     "@redis",
21
     *     "%session.lifetime%"
22
     * })
23
     * @param ClientInterface $client
24
     * @param int $lifetime
25
     */
26 4
    public function __construct(ClientInterface $client, $lifetime)
27
    {
28 4
        parent::__construct($client, [
29 4
            'gc_maxlifetime' => $lifetime
30
        ]);
31 4
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function read($sessionId)
37
    {
38 2
        if ($data = $this->client->get(self::KEY . $sessionId)) {
39 1
            return $data;
40
        }
41
42 1
        return '';
43
    }
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function write($sessionId, $sessionData)
48
    {
49 1
        $this->client->setex(self::KEY . $sessionId, $this->ttl, $sessionData);
50
51 1
        return true;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function destroy($sessionId)
58
    {
59 1
        $this->client->del(self::KEY . $sessionId);
60
61 1
        return true;
62
    }
63
}
64