Passed
Pull Request — master (#190)
by Arman
04:27
created

JwtToken::fetchData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Jwt;
16
17
use Quantum\Libraries\Jwt\Exceptions\JwtException;
18
use Firebase\JWT\JWT;
19
use Firebase\JWT\Key;
20
21
/**
22
 * Class JwtToken
23
 * @package Quantum\Libraries\JwtToken
24
 * @uses JWT
25
 */
26
class JwtToken extends JWT
27
{
28
29
    /**
30
     * JWT secret key
31
     * @var string
32
     */
33
    private $key;
34
35
    /**
36
     * Encryption algorithm
37
     * @var string
38
     */
39
    private $algorithm = 'HS256';
40
41
    /**
42
     * Payload data
43
     * @var array
44
     */
45
    private $payload = [];
46
47
    /**
48
     * @var object|null
49
     */
50
    private $fetchedPayload = null;
51
52
    /**
53
     * JwtToken constructor.
54
     * @param mixed $key
55
     */
56
    public function __construct(string $key = null)
57
    {
58
        $this->key = $key ?? env('APP_KEY');
59
    }
60
61
    /**
62
     * Sets extra leeway time
63
     * @return $this
64
     */
65
    public function setLeeway($leeway): JwtToken
66
    {
67
        parent::$leeway = $leeway;
68
        return $this;
69
    }
70
71
    /**
72
     * Sets the encryption algorithm
73
     * @param string $algorithm
74
     * @return $this
75
     */
76
    public function setAlgorithm(string $algorithm): JwtToken
77
    {
78
        $this->algorithm = $algorithm;
79
        return $this;
80
    }
81
82
    /**
83
     * Sets the claim
84
     * @param string $key
85
     * @param mixed $value
86
     * @return $this
87
     */
88
    public function setClaim(string $key, $value): JwtToken
89
    {
90
        $this->payload[$key] = $value;
91
        return $this;
92
    }
93
94
    /**
95
     * Set claims
96
     * @param array $claims
97
     * @return $this
98
     */
99
    public function setClaims(array $claims): JwtToken
100
    {
101
        foreach ($claims as $key => $value) {
102
            $this->payload[$key] = $value;
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * Sets user data
110
     * @param array $data
111
     * @return $this
112
     */
113
    public function setData(array $data): JwtToken
114
    {
115
        $this->payload['data'] = $data;
116
        return $this;
117
    }
118
119
    /**
120
     * Composes and signs the JWT
121
     * @param mixed|null $keyId
122
     * @param array|null $head
123
     * @return string
124
     * @throws JwtException
125
     */
126
    public function compose($keyId = null, array $head = null): string
127
    {
128
        if (empty($this->payload)) {
129
            throw JwtException::payloadNotFound();
130
        }
131
132
        return parent::encode($this->payload, $this->key, $this->algorithm, $keyId, $head);
133
    }
134
135
    /**
136
     * Retrieve and verifies the JWT
137
     * @param string $jwt
138
     * @return $this
139
     */
140
    public function retrieve(string $jwt): JwtToken
141
    {
142
        $this->fetchedPayload = parent::decode($jwt, new Key($this->key, $this->algorithm));
143
        return $this;
144
    }
145
146
    /**
147
     * Fetches the payload
148
     * @return object
149
     */
150
    public function fetchPayload(): ?object
151
    {
152
        return $this->fetchedPayload;
153
    }
154
155
    /**
156
     * Fetches the user data
157
     * @return array|null
158
     */
159
    public function fetchData(): ?array
160
    {
161
        return isset($this->fetchedPayload->data) ? (array)$this->fetchedPayload->data : null;
162
    }
163
164
    /**
165
     * Fetches the claim
166
     * @param string $key
167
     * @return mixed|null
168
     */
169
    public function fetchClaim(string $key)
170
    {
171
        return $this->fetchedPayload->$key ?? null;
172
    }
173
}