AuthorizationCodeFacade::getStorage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\OAuth2\Storage\AuthorizationCodes;
4
5
6
use DateTime;
7
use kalanis\OAuth2\IKeyGenerator;
8
use kalanis\OAuth2\Storage\Clients\IClient;
9
use kalanis\OAuth2\Storage\Exceptions\InvalidAuthorizationCodeException;
10
use kalanis\OAuth2\Storage\ITokenFacade;
11
12
13
/**
14
 * AuthorizationCode
15
 * @package kalanis\OAuth2\Storage\AuthorizationCodes
16
 */
17 1
class AuthorizationCodeFacade implements ITokenFacade
18
{
19
20 1
    public function __construct(
21
        private readonly int $lifetime,
22
        private readonly IKeyGenerator $keyGenerator,
23
        private readonly IAuthorizationCodeStorage $storage,
24
    )
25
    {
26 1
    }
27
28
    /**
29
     * Create authorization code
30
     */
31
    public function create(IClient $client, string|int|null $userId, array $scope = []): IAuthorizationCode
32
    {
33 1
        $accessExpires = new DateTime;
34 1
        $accessExpires->modify('+' . $this->lifetime . ' seconds');
35
36 1
        $authorizationCode = new AuthorizationCode(
37 1
            $this->keyGenerator->generate(),
38
            $accessExpires,
39 1
            $client->getId(),
40
            $userId,
41
            $scope
42
        );
43 1
        $this->storage->store($authorizationCode);
44
45 1
        return $authorizationCode;
46
    }
47
48
    /**
49
     * Get authorization code entity
50
     */
51
    public function getEntity(string $token): IAuthorizationCode
52
    {
53 1
        $entity = $this->storage->getValidAuthorizationCode($token);
54 1
        if (!$entity) {
55 1
            $this->storage->remove($token);
56 1
            throw new InvalidAuthorizationCodeException;
57
        }
58 1
        return $entity;
59
    }
60
61
    /**
62
     * Get token identifier name
63
     */
64
    public function getIdentifier(): string
65
    {
66 1
        return self::AUTHORIZATION_CODE;
67
    }
68
69
    /**
70
     * Get token lifetime
71
     * @return int
72
     */
73
    public function getLifetime(): int
74
    {
75
        return $this->lifetime;
76
    }
77
78
    /**
79
     * Get storage
80
     */
81
    public function getStorage(): IAuthorizationCodeStorage
82
    {
83
        return $this->storage;
84
    }
85
}
86