StateManager::pullState()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace FMCSSOClient\OAuth;
4
5
use Illuminate\Support\Str;
6
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
53
    {
54
        if (!$this->useState) {
55
            return null;
56
        }
57
58
        return (string) request()->session()->pull($this->cacheKey);
59
    }
60
61
    /**
62
     * Get current state value.
63
     *
64
     * @return string|null
65
     */
66
    public function state(): ?string
67
    {
68
        if (!$this->useState) {
69
            return null;
70
        }
71
72
        return (string) request()->session()->get($this->cacheKey);
73
    }
74
75
    /**
76
     * Get the string used for session state.
77
     *
78
     * @return string
79
     */
80
    protected function generateState(): string
81
    {
82
        return Str::random(40);
83
    }
84
}
85