Verifier::verifyToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File: Verifier.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Jwt\Handler;
10
11
use MSlwk\Jwt\Api\VerifierInterface;
12
13
/**
14
 * Class Verifier
15
 *
16
 * @package MSlwk\Jwt\Handler
17
 */
18
class Verifier extends AbstractHandler implements VerifierInterface
19
{
20
    /**
21
     * @param string $token
22
     * @return bool
23
     */
24 4
    public function verifyToken(string $token): bool
25
    {
26 4
        if (!$this->verifyTokenStructure($token)) {
27 1
            return false;
28
        }
29 3
        $tokenParts = $this->divideToken($token);
30
31 3
        $receivedHeader = $tokenParts[0];
32 3
        $receivedPayload = $tokenParts[1];
33 3
        $receivedSignature = $tokenParts[2];
34
35 3
        $calculatedSignature = $this->calculateSignature($receivedHeader, $receivedPayload);
36 3
        return $calculatedSignature === $receivedSignature;
37
    }
38
39 4
    protected function divideToken(string $token): array
40
    {
41 4
        return explode('.', $token);
42
    }
43
44
    /**
45
     * @param string $token
46
     * @return bool
47
     */
48 4
    protected function verifyTokenStructure(string $token): bool
49
    {
50 4
        return count($this->divideToken($token)) === 3;
51
    }
52
}
53