1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
final class Session |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $id; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $contents; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var SessionIdInterface |
19
|
|
|
*/ |
20
|
|
|
private $sessionId; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $oldIds = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $status = \PHP_SESSION_NONE; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $id |
34
|
|
|
* @param array $contents |
35
|
|
|
* @param SessionIdInterface $sessionId |
36
|
|
|
*/ |
37
|
16 |
|
public function __construct(string $id, array $contents, SessionIdInterface $sessionId) |
38
|
|
|
{ |
39
|
16 |
|
$this->id = $id; |
40
|
16 |
|
$this->contents = $contents; |
41
|
16 |
|
$this->sessionId = $sessionId; |
42
|
|
|
|
43
|
16 |
|
if ($this->id !== '') { |
44
|
4 |
|
$this->status = PHP_SESSION_ACTIVE; |
45
|
|
|
} |
46
|
16 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
13 |
|
public function getId(): string |
52
|
|
|
{ |
53
|
13 |
|
return $this->id; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $contents |
58
|
|
|
*/ |
59
|
4 |
|
public function setContents(array $contents) |
60
|
|
|
{ |
61
|
4 |
|
$this->contents = $contents; |
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
12 |
|
public function getContents(): array |
68
|
|
|
{ |
69
|
12 |
|
return $this->contents; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
13 |
|
public function getOldIds(): array |
76
|
|
|
{ |
77
|
13 |
|
return $this->oldIds; |
78
|
|
|
} |
79
|
|
|
|
80
|
11 |
View Code Duplication |
public function begin() |
|
|
|
|
81
|
|
|
{ |
82
|
11 |
|
if ($this->status === \PHP_SESSION_ACTIVE) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
11 |
|
$this->status = \PHP_SESSION_ACTIVE; |
87
|
|
|
|
88
|
11 |
|
if ($this->id === '') { |
89
|
11 |
|
$this->id = $this->sessionId->generate(); |
90
|
|
|
} |
91
|
11 |
|
} |
92
|
|
|
|
93
|
2 |
|
public function end() |
94
|
|
|
{ |
95
|
2 |
|
if ($this->status === \PHP_SESSION_NONE) { |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$this->oldIds[] = $this->id; |
100
|
2 |
|
$this->status = \PHP_SESSION_NONE; |
101
|
2 |
|
$this->id = ''; |
102
|
2 |
|
$this->contents = []; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
13 |
|
public function isActive(): bool |
109
|
|
|
{ |
110
|
13 |
|
return $this->status === \PHP_SESSION_ACTIVE; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
View Code Duplication |
public function regenerate(): bool |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
// Can only regenerate active sessions |
116
|
1 |
|
if ($this->status !== \PHP_SESSION_ACTIVE) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$this->oldIds[] = $this->id; |
121
|
1 |
|
$this->id = $this->sessionId->generate(); |
122
|
|
|
|
123
|
1 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function toArray(): array |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
1 |
|
'id' => $this->id, |
130
|
1 |
|
'contents' => $this->contents, |
131
|
1 |
|
'oldIds' => $this->oldIds, |
132
|
1 |
|
'status' => $this->status, |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $session |
138
|
|
|
* @throws \InvalidArgumentException |
139
|
|
|
* @return Session |
140
|
|
|
*/ |
141
|
1 |
|
public function fromArray(array $session): self |
142
|
|
|
{ |
143
|
1 |
|
if (!isset($session['id']) || !isset($session['contents']) || !isset($session['oldIds']) || !isset($session['oldIds'])) { |
144
|
|
|
throw new \InvalidArgumentException('Session array most contain "id", "contents", "oldIds", and "status".'); |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
$clone = clone $this; |
148
|
1 |
|
$clone->id = $session['id']; |
149
|
1 |
|
$clone->contents = $session['contents']; |
150
|
1 |
|
$clone->oldIds = $session['oldIds']; |
151
|
1 |
|
$clone->status = $session['status']; |
152
|
|
|
|
153
|
1 |
|
return $clone; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.