TokenGenerator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace CustomerGauge\Cognito\Testing;
4
5
use Jose\Component\Core\JWKSet;
6
use Jose\Easy\Build;
7
8
final class TokenGenerator
9
{
10
    private $jwk;
11
12
    public $jti = 'token-id';
13
14
    public $algorithm = 'RS256';
15
16
    public $issuer = 'https://cognito-idp.local.amazonaws.com/phpunit-pool-id';
17
18
    public $subject = 'testing';
19
20 3
    public function __construct(JWKSet $jwk)
21
    {
22 3
        $this->jwk = $jwk;
23
    }
24
25 3
    public static function fromFile(string $path): self
26
    {
27 3
        $key = file_get_contents($path);
28
29 3
        return new self(JWKSet::createFromJson($key));
30
    }
31
32 3
    public function sign(array $attributes): string
33
    {
34 3
        $time = time();
35
36 3
        $builder = Build::jws()
37 3
            ->exp($time + 3600)
38 3
            ->iat($time)
39 3
            ->nbf($time)
40 3
            ->jti($this->jti, true)
41 3
            ->alg($this->algorithm)
42 3
            ->iss($this->issuer)
43 3
            ->sub($this->subject);
44
45 3
        foreach ($attributes as $key => $value) {
46 3
            $builder->claim($key, $value, true);
47
        }
48
49 3
        return $builder->sign($this->jwk->get(0));
50
    }
51
}