Completed
Push — master ( 3c79d6...11a354 )
by Alexander
02:32
created

MockArraySessionStorage::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Flash;
4
5
use Yiisoft\Yii\Web\Session\SessionInterface;
6
7
class MockArraySessionStorage implements SessionInterface
8
{
9
    private $id = '';
10
11
    private $name = '';
12
13
    private $started = false;
14
15
    private $closed = false;
16
17
    private $data;
18
19
    public function __construct(array $data = [])
20
    {
21
        $this->data = $data;
22
    }
23
24
    public function get(string $key, $default = null)
25
    {
26
        return $this->data[$key] ?? $default;
27
    }
28
29
    public function set(string $key, $value): void
30
    {
31
        $this->data[$key] = $value;
32
    }
33
34
    public function close(): void
35
    {
36
        $this->closed = true;
37
        $this->id = null;
38
    }
39
40
    public function open(): void
41
    {
42
        if ($this->isActive()) {
43
            return;
44
        }
45
46
        if (empty($this->id)) {
47
            $this->id = $this->generateId();
48
        }
49
50
        $this->started = true;
51
        $this->closed = false;
52
    }
53
54
    public function isActive(): bool
55
    {
56
        return $this->started && !$this->closed;
57
    }
58
59
    public function regenerateId(): void
60
    {
61
        $this->id = $this->generateId();
62
    }
63
64
    public function discard(): void
65
    {
66
        $this->close();
67
    }
68
69
    public function all(): array
70
    {
71
        return $this->data;
72
    }
73
74
    public function remove(string $key): void
75
    {
76
        unset($this->data[$key]);
77
    }
78
79
    public function has(string $key): bool
80
    {
81
        return isset($this->data[$key]);
82
    }
83
84
    public function pull(string $key)
85
    {
86
        $value = $this->data[$key] ?? null;
87
        $this->remove($key);
88
        return $value;
89
    }
90
91
    public function destroy(): void
92
    {
93
        $this->close();
94
    }
95
96
    public function getCookieParameters(): array
97
    {
98
        return [];
99
    }
100
101
    public function getId(): ?string
102
    {
103
        return $this->id;
104
    }
105
106
    public function setId(string $sessionId): void
107
    {
108
        $this->id = $sessionId;
109
    }
110
111
    public function getName(): string
112
    {
113
        return $this->name;
114
    }
115
116
    public function clear(): void
117
    {
118
        $this->data = [];
119
    }
120
121
    private function generateId(): string
122
    {
123
        return hash('sha256', uniqid('ss_mock_', true));
124
    }
125
}
126