Completed
Push — master ( 615c5e...3a4e35 )
by Márk
04:34
created

SimpleCacheSessionHandler::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Nofw\Session;
4
5
use Psr\SimpleCache\CacheInterface;
6
use Psr\SimpleCache\InvalidArgumentException;
7
8
/**
9
 * PSR-16 Session handler implementation.
10
 *
11
 * @author Márk Sági-Kazár <[email protected]>
12
 */
13
final class SimpleCacheSessionHandler implements \SessionHandlerInterface
14
{
15
    /**
16
     * @var CacheInterface
17
     */
18
    private $cache;
19
20
    /**
21
     * @param CacheInterface $cache
22
     */
23 13
    public function __construct(CacheInterface $cache)
24
    {
25 13
        $this->cache = $cache;
26 13
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function close()
32
    {
33 1
        return true;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function destroy($session_id)
40
    {
41
        try {
42 3
            return $this->cache->delete($session_id);
43 1
        } catch (InvalidArgumentException $e) {
44 1
            return false;
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function gc($maxlifetime)
52
    {
53 1
        return true;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function open($save_path, $name)
60
    {
61 1
        return true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function read($session_id)
68
    {
69
        try {
70 2
            return $this->cache->get($session_id);
71 1
        } catch (InvalidArgumentException $e) {
72 1
            return '';
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 3
    public function write($session_id, $session_data)
80
    {
81
        try {
82 3
            return $this->cache->set($session_id, $session_data);
83 1
        } catch (InvalidArgumentException $e) {
84 1
            return false;
85
        }
86
    }
87
}
88