Passed
Push — master ( 301b02...f57bb7 )
by Jelmer
03:05
created

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