Completed
Push — develop ( c3dea4...6c36f4 )
by Sean
02:38
created

Parser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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