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

Session::rotateId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace jschreuder\Middle\Session;
4
5
final class Session implements SessionInterface
6
{
7
    const FLASH_DATA_META_KEY = '_flash_data_keys';
8
    const FLASH_DATA_KEY_PREFIX = '_flash_data.';
9
10
    /** @var  array */
11
    private $sessionData;
12
13
    /** @var  bool */
14
    private $changed = false;
15
16 7
    public function __construct(array $sessionData = [])
17
    {
18 7
        $this->sessionData = $this->processFlashData($sessionData);
19 7
    }
20
21 7
    private function processFlashData(array $sessionData) : array
22
    {
23 7
        if (!isset($sessionData[self::FLASH_DATA_META_KEY])) {
24 6
            return $sessionData;
25
        }
26
27 1
        foreach ($sessionData[self::FLASH_DATA_META_KEY] as $key => $hops) {
28 1
            if ($hops <= 0) {
29 1
                unset($sessionData[self::FLASH_DATA_META_KEY][$key]);
30 1
                unset($sessionData[self::FLASH_DATA_KEY_PREFIX . $key]);
31 1
                $this->changed = true;
32
            } else {
33 1
                $sessionData[self::FLASH_DATA_META_KEY][$key] = $hops - 1;
34 1
                $this->changed = true;
35
            }
36
        }
37 1
        return $sessionData;
38
    }
39
40
    /** @return  void */
41 1
    private function markFlashKey(string $key)
42
    {
43 1
        $this->sessionData[self::FLASH_DATA_META_KEY][$key] = 1;
44 1
    }
45
46 2
    public function has(string $key) : bool
47
    {
48 2
        return isset($this->sessionData[$key]);
49
    }
50
51 1
    public function get(string $key)
52
    {
53 1
        return $this->sessionData[$key] ?? null;
54
    }
55
56 1
    public function set(string $key, $value)
57
    {
58 1
        $this->changed = true;
59 1
        $this->sessionData[$key] = $value;
60 1
    }
61
62 3
    public function hasFlash(string $key) : bool
63
    {
64 3
        return isset($this->sessionData[self::FLASH_DATA_KEY_PREFIX . $key]);
65
    }
66
67
    public function getFlash(string $key)
68
    {
69
        return $this->sessionData[self::FLASH_DATA_KEY_PREFIX . $key] ?? null;
70
    }
71
72 1
    public function setFlash(string $key, $value)
73
    {
74 1
        $this->changed = true;
75 1
        $this->markFlashKey($key);
76 1
        $this->sessionData[self::FLASH_DATA_KEY_PREFIX . $key] = $value;
77 1
    }
78
79 1
    public function destroy()
80
    {
81 1
        $this->changed = true;
82 1
        $this->sessionData = [];
83 1
    }
84
85 1
    public function rotateId()
86
    {
87 1
        $this->changed = true;
88
        // These sessions don't have an ID to change, but this should force overwrite
89 1
    }
90
91 1
    public function isEmpty() : bool
92
    {
93 1
        return count($this->sessionData) === 0;
94
    }
95
96 5
    public function hasChanged() : bool
97
    {
98 5
        return $this->changed;
99
    }
100
101 2
    public function toArray() : array
102
    {
103 2
        return $this->sessionData;
104
    }
105
}
106