Completed
Pull Request — develop (#391)
by
unknown
62:22
created

Parser::setChain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
ccs 2
cts 2
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 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 10
    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 10
        $this->request = $request;
35 10
        $this->chain = $chain;
36 10
    }
37
38
    /**
39
     * Get the parser chain
40
     *
41
     * @return array The chain of ParserContracts that the parser evaluates.
42
     */
43 8
    public function getChain()
44
    {
45 8
        return $this->chain;
46
    }
47 8
48
    /**
49
     * Set the order of the parser chain
50
     *
51
     * @param array $chain
52
     */
53
    public function setChain(array $chain)
54
    {
55
        $this->chain = $chain;
56 10
57
        return $this;
58 10
    }
59 10
    
60
    /**
61 10
     * Alias for setting the order of the chain
62 8
     *
63
     * @param array $chain
64 8
     */
65
    public function setChainOrder(array $chain)
66 2
    {
67
        $this->setChain($chain);
68
69
        return $this;
70
    }
71
72
    /**
73
     * Iterate throught the parsers and attempt to retrieve
74 10
     * a value, otherwise return null
75
     *
76 10
     * @return string|null
77
     */
78
    public function parseToken()
79
    {
80
        foreach ($this->chain as $parser) {
81
            $response = $parser->parse($this->request);
82
83
            if ($response !== null) {
84
                return $response;
85
            }
86 2
        }
87
88 2
        return null;
89
    }
90 2
91
    /**
92
     * Check whether a token exists in the chain
93
     *
94
     * @return  boolean
95
     */
96
    public function hasToken()
97
    {
98
        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
    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
        $this->request = $request;
111
112
        return $this;
113
    }
114
}
115