Total Complexity | 9 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Coverage | 36.84% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class StateManager |
||
8 | { |
||
9 | protected bool $useState = true; |
||
10 | |||
11 | protected string $cacheKey = 'state'; |
||
12 | |||
13 | /** |
||
14 | * @param bool $useState |
||
15 | */ |
||
16 | 23 | public function __construct(bool $useState = true) |
|
17 | { |
||
18 | 23 | $this->useState = $useState; |
|
19 | } |
||
20 | |||
21 | /** |
||
22 | * Is need validate state. |
||
23 | * |
||
24 | * @return bool |
||
25 | */ |
||
26 | 10 | public function needValidateState(): bool |
|
27 | { |
||
28 | 10 | return $this->useState; |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * Create and cache state. |
||
33 | * |
||
34 | * @return string|null |
||
35 | */ |
||
36 | 4 | public function makeState(): ?string |
|
37 | { |
||
38 | 4 | if (!$this->useState) { |
|
39 | 4 | return null; |
|
40 | } |
||
41 | |||
42 | request()->session()->put($this->cacheKey, $state = $this->generateState()); |
||
43 | |||
44 | return $state; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get state and remove from cache. |
||
49 | * |
||
50 | * @return string|null |
||
51 | */ |
||
52 | public function pullState(): ?string |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get current state value. |
||
63 | * |
||
64 | * @return string|null |
||
65 | */ |
||
66 | public function state(): ?string |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the string used for session state. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | protected function generateState(): string |
||
83 | } |
||
84 | } |
||
85 |