MemcachedSessionHandler::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Inspirum\Mcrouter\Services;
4
5
use Illuminate\Contracts\Cache\Repository as CacheContract;
6
use Illuminate\Session\CacheBasedSessionHandler as LaravelCacheBasedSessionHandler;
7
use Inspirum\Mcrouter\Model\Values\Mcrouter;
8
9
class MemcachedSessionHandler extends LaravelCacheBasedSessionHandler
10
{
11
    /**
12
     * Mcrouter config
13
     *
14
     * @var \Inspirum\Mcrouter\Model\Values\Mcrouter
15
     */
16
    private $mcrouter;
17
18
    /**
19
     * Create a new cache driven handler instance.
20
     *
21
     * @param \Illuminate\Contracts\Cache\Repository        $cache
22
     * @param int                                           $minutes
23
     * @param \Inspirum\Mcrouter\Model\Values\Mcrouter|null $mcrouter
24
     */
25 6
    public function __construct(CacheContract $cache, $minutes, Mcrouter $mcrouter = null)
26
    {
27 6
        parent::__construct($cache, $minutes);
28
29 6
        $this->mcrouter = $mcrouter ?: new Mcrouter('');
30 6
    }
31
32
    /**
33
     * Read session data
34
     *
35
     * @param string $sessionId
36
     *
37
     * @return string
38
     */
39 4
    public function read($sessionId)
40
    {
41 4
        return parent::read($this->getSharedKey($sessionId));
42
    }
43
44
    /**
45
     * Write session data
46
     *
47
     * @param string $sessionId
48
     * @param string $data
49
     *
50
     * @return bool
51
     */
52 3
    public function write($sessionId, $data)
53
    {
54 3
        return parent::write($this->getSharedKey($sessionId), $data);
55
    }
56
57
    /**
58
     * Destroy a session
59
     *
60
     * @param string $sessionId
61
     *
62
     * @return bool
63
     */
64 2
    public function destroy($sessionId)
65
    {
66 2
        return parent::destroy($this->getSharedKey($sessionId));
67
    }
68
69
    /**
70
     * Get the cache key with Mcrouter shared prefix.
71
     *
72
     * @param string $key
73
     *
74
     * @return string
75
     */
76 6
    private function getSharedKey(string $key): string
77
    {
78 6
        return $this->mcrouter->getSharedKey($key);
79
    }
80
}
81