Session::markFlashKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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