Session   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 95
ccs 41
cts 41
cp 1
rs 10
c 1
b 0
f 0
wmc 17

14 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 4 1
A get() 0 3 1
A toArray() 0 3 1
A hasChanged() 0 3 1
A markFlashKey() 0 3 1
A hasFlash() 0 3 1
A getFlash() 0 3 1
A processFlashData() 0 17 4
A __construct() 0 3 1
A rotateId() 0 3 1
A has() 0 3 1
A isEmpty() 0 3 1
A setFlash() 0 5 1
A destroy() 0 4 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
    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