Completed
Pull Request — master (#1)
by Woody
23:31 queued 01:08
created

FirebaseParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Equip\Auth\Jwt;
3
4
use Firebase\JWT\JWT;
5
use Firebase\JWT\ExpiredException;
6
use Equip\Auth\Jwt\Configuration;
7
use Equip\Auth\Exception\InvalidException;
8
use Equip\Auth\Token;
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
            );
40 1
        } catch (ExpiredException $e) {
41 1
            throw new InvalidException(
42 1
                'Token has expired: ' . $token,
43 1
                InvalidException::CODE_TOKEN_EXPIRED
44
            );
45
        }
46 2
        return new Token($token, $metadata);
47
    }
48
}
49