HasStateParameter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 8
c 2
b 0
f 1
dl 0
loc 41
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A stateless() 0 5 1
A makeState() 0 4 1
A hasValidState() 0 7 3
1
<?php
2
3
/*
4
 * This file is part of the mingyoung/dingtalk.
5
 *
6
 * (c) 张铭阳 <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyDingTalk\Auth;
13
14
use function EasyDingTalk\str_random;
15
use function EasyDingTalk\tap;
16
17
trait HasStateParameter
18
{
19
    /**
20
     * @var bool
21
     */
22
    protected $stateless = false;
23
24
    /**
25
     * @return $this
26
     */
27
    public function stateless()
28
    {
29
        $this->stateless = true;
30
31
        return $this;
32
    }
33
34
    /**
35
     * Generate state.
36
     *
37
     * @return string
38
     */
39
    protected function makeState()
40
    {
41
        return tap(str_random(64), function ($state) {
42
            $this->app['request']->getSession()->set('state', $state);
43
        });
44
    }
45
46
    /**
47
     * @param string|null $state
48
     *
49
     * @return bool
50
     */
51
    protected function hasValidState($state)
52
    {
53
        if ($this->stateless) {
54
            return true;
55
        }
56
57
        return !is_null($state) && ($state === $this->app['request']->getSession()->get('state'));
58
    }
59
}
60