LcobucciGenerator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
crap 1
1
<?php
2
namespace Equip\Auth\Jwt;
3
4
use Lcobucci\JWT\Builder;
5
use Lcobucci\JWT\Signer;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Generator for JWT authentication token strings that uses the lcobucci/jwt
10
 * library.
11
 */
12
class LcobucciGenerator implements GeneratorInterface
13
{
14
    /**
15
     * @var RequestInterface
16
     */
17
    protected $request;
18
19
    /**
20
     * @var Builder
21
     */
22
    protected $builder;
23
24
    /** 
25
     * @var Signer
26
     */
27
    protected $signer;
28
29
    /**
30
     * @var Configuration
31
     */
32
    protected $config;
33
34
    /**
35
     * @param RequestInterface $request
36
     * @param Builder $builder
37
     * @param Signer $signer
38
     * @param Configuration $config
39
     */
40 4
    public function __construct(
41
        RequestInterface $request,
42
        Builder $builder,
43
        Signer $signer,
44
        Configuration $config
45
    ) {
46 4
        $this->request = $request;
47 4
        $this->builder = $builder;
48 4
        $this->signer = $signer;
49 4
        $this->config = $config;
50 4
    }
51
52
    /**
53
     * @param array $claims
54
     * @return string
55
     */
56 4
    public function getToken(array $claims = [])
57
    {
58 4
        $issuer = (string) $this->request->getUri();
59 4
        $issued_at = $this->config->getTimestamp();
60 4
        $expiration = $issued_at + $this->config->getTtl();
61 4
        $key = $this->config->getPrivateKey();
62 4
        foreach ($claims as $name => $value) {
63 4
            $this->builder->set($name, $value);
64 4
        }
65 4
        $token = $this->builder
66 4
            ->setIssuer($issuer)
67 4
            ->setIssuedAt($issued_at)
68 4
            ->setExpiration($expiration)
69 4
            ->sign($this->signer, $key)
70 4
            ->getToken();
71 4
        return (string) $token;
72
    }
73
}
74