FirebaseParser::parseToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
namespace Equip\Auth\Jwt;
3
4
use Equip\Auth\Exception\InvalidException;
5
use Equip\Auth\Jwt\Configuration;
6
use Equip\Auth\Token;
7
use Firebase\JWT\ExpiredException;
8
use Firebase\JWT\JWT;
9
10
/**
11
 * Parser for JWT authentication token strings that uses the firebase/php-jwt
12
 * library.
13
 */
14
class FirebaseParser implements ParserInterface
15
{
16
    /**
17
     * @var Configuration
18
     */
19
    protected $config;
20
21
    /**
22
     * @param Configuration $config
23
     */
24 3
    public function __construct(Configuration $config)
25
    {
26 3
        $this->config = $config;
27 3
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 3
    public function parseToken($token)
33
    {
34
        try {
35 3
            $metadata = (array) JWT::decode(
36 3
                (string) $token,
37 3
                $this->config->getPublicKey(),
38 3
                [$this->config->getAlgorithm()]
39 3
            );
40 3
        } catch (ExpiredException $e) {
41 1
            throw InvalidException::tokenExpired($token, $e);
42
        }
43 2
        return new Token($token, $metadata);
44
    }
45
}
46