Completed
Push — develop ( 7b8c01...54328c )
by Sean
02:43
created

Parser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 9
c 5
b 1
f 0
lcom 1
cbo 0
dl 0
loc 99
ccs 24
cts 24
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getChain() 0 4 1
A __construct() 0 5 1
A setChain() 0 6 1
A setChainOrder() 0 6 1
A parseToken() 0 12 3
A hasToken() 0 4 1
A setRequest() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of jwt-auth
5
 *
6
 * (c) Sean Tymon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tymon\JWTAuth\Http;
13
14
use Illuminate\Http\Request;
15
16
class Parser
17
{
18
    /**
19
     * @var array
20
     */
21
    private $chain;
22
23
    /**
24
     * @var \Illuminate\Http\Request
25
     */
26
    protected $request;
27
28
    /**
29
     * @param \Illuminate\Http\Request $request
30
     * @param array $chain
31
     */
32 18
    public function __construct(Request $request, array $chain = [])
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
33
    {
34 18
        $this->request = $request;
35 18
        $this->chain = $chain;
36 18
    }
37
38
    /**
39
     * Get the parser chain
40
     *
41
     * @return array The chain of ParserContracts that the parser evaluates.
42
     */
43 2
    public function getChain()
44
    {
45 2
        return $this->chain;
46
    }
47
48
    /**
49
     * Set the order of the parser chain
50
     *
51
     * @param array $chain
52
     */
53 16
    public function setChain(array $chain)
54
    {
55 16
        $this->chain = $chain;
56
57 16
        return $this;
58
    }
59
    
60
    /**
61
     * Alias for setting the order of the chain
62
     *
63
     * @param array $chain
64
     */
65 14
    public function setChainOrder(array $chain)
66
    {
67 14
        $this->setChain($chain);
68
69 14
        return $this;
70
    }
71
72
    /**
73
     * Iterate throught the parsers and attempt to retrieve
74
     * a value, otherwise return null
75
     *
76
     * @return string|null
77
     */
78 16
    public function parseToken()
79
    {
80 16
        foreach ($this->chain as $parser) {
81 16
            $response = $parser->parse($this->request);
82
83 16
            if ($response !== null) {
84 10
                return $response;
85
            }
86 14
        }
87
88 6
        return null;
89
    }
90
91
    /**
92
     * Check whether a token exists in the chain
93
     *
94
     * @return  boolean
95
     */
96 16
    public function hasToken()
97
    {
98 16
        return $this->parseToken() !== null;
99
    }
100
101
    /**
102
     * Set the request instance.
103
     *
104
     * @param \Illuminate\Http\Request $request
105
     *
106
     * @return Parser
107
     */
108 2
    public function setRequest(Request $request)
1 ignored issue
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
109
    {
110 2
        $this->request = $request;
111
112 2
        return $this;
113
    }
114
}
115