Passed
Push — master ( f57bb7...4ff9d0 )
by Jelmer
02:50
created

ZendSession::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace jschreuder\Middle\Session;
4
5
use Zend\Session\Container;
6
use Zend\Session\SessionManager;
7
8
final class ZendSession implements SessionInterface
9
{
10
    const FLASH_DATA_KEY_PREFIX = '_flash_data.';
11
12
    /** @var  SessionManager */
13
    private $sessionManager;
14
15
    /** @var  Container */
16
    private $container;
17
18
    private $changed = false;
19
20 9
    public function __construct(SessionManager $sessionManager, Container $container)
21
    {
22 9
        $this->sessionManager = $sessionManager;
23 9
        $this->container = $container;
24 9
    }
25
26 2
    public function has(string $key) : bool
27
    {
28 2
        return isset($this->container[$key]);
29
    }
30
31 1
    public function get(string $key)
32
    {
33 1
        return $this->container[$key];
34
    }
35
36
    /** @return  void */
37 1
    public function set(string $key, $value)
38
    {
39 1
        $this->changed = true;
40 1
        $this->container[$key] = $value;
41 1
    }
42
43 2
    public function hasFlash(string $key) : bool
44
    {
45 2
        return isset($this->container[self::FLASH_DATA_KEY_PREFIX . $key]);
46
    }
47
48 1
    public function getFlash(string $key)
49
    {
50 1
        return $this->container[self::FLASH_DATA_KEY_PREFIX . $key];
51
    }
52
53
    /** @return  void */
54 1
    public function setFlash(string $key, $value)
55
    {
56 1
        $key = self::FLASH_DATA_KEY_PREFIX . $key;
57 1
        $this->changed = true;
58 1
        $this->container[$key] = $value;
59 1
        $this->container->setExpirationHops(1, [$key]);
60 1
    }
61
62
    /** @return  void */
63 1
    public function destroy()
64
    {
65 1
        $this->sessionManager->destroy();
66 1
    }
67
68
    /** @return  void */
69 1
    public function rotateId()
70
    {
71 1
        $this->changed = true;
72 1
        $this->sessionManager->regenerateId();
73 1
    }
74
75
    public function isEmpty() : bool
76
    {
77
        return $this->container->count() === 0;
78
    }
79
80
    public function hasChanged() : bool
81
    {
82
        return $this->changed;
83
    }
84
85
    public function toArray() : array
86
    {
87
        return $this->container->getArrayCopy();
88
    }
89
}
90