TokenGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 42
ccs 18
cts 18
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFile() 0 5 1
A __construct() 0 3 1
A sign() 0 18 2
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
}