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

SimpleCacheSessionHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 75
ccs 21
cts 21
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A close() 0 4 1
A destroy() 0 8 2
A gc() 0 4 1
A open() 0 4 1
A read() 0 8 2
A write() 0 8 2
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