Test Setup Failed
Push — dev ( b27119...389162 )
by Herberto
04:46
created

OauthAccessToken::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Acme\App\Infrastructure\Auth\Authentication\Oauth;
6
7
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId;
8
use DateTime;
9
use DateTimeImmutable;
10
use DateTimeInterface;
11
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
12
use League\OAuth2\Server\Entities\ClientEntityInterface;
13
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
14
use League\OAuth2\Server\Entities\Traits\EntityTrait;
15
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
16
17
class OauthAccessToken implements AccessTokenEntityInterface
18
{
19
    use AccessTokenTrait;
20
    use EntityTrait;
21
    use TokenEntityTrait;
22
23
    /**
24
     * @var bool
25
     */
26
    private $revoked = false;
27
28
    /**
29
     * @var DateTimeImmutable
30
     */
31
    private $createdAt;
32
33
    /**
34
     * @var DateTimeImmutable
35
     */
36
    private $updatedAt;
37
38
    public function __construct(
39
        UserId $userId,
40
        ClientEntityInterface $client,
41
        array $scopes = [],
42
        DateTimeInterface $expiryDate = null
43
    ) {
44
        $this->setIdentifier(new OauthAccessTokenId());
45
        $this->setUserIdentifier($userId);
46
        $this->setClient($client);
47
        foreach ($scopes as $scope) {
48
            $this->addScope($scope);
49
        }
50
        if ($expiryDate) {
51
            $this->setExpiryDateTime(
52
                $expiryDate instanceof DateTimeImmutable
53
                    ? DateTime::createFromImmutable($expiryDate)
54
                    : $expiryDate
55
            );
56
        }
57
    }
58
59
    public function isRevoked(): bool
60
    {
61
        return $this->revoked;
62
    }
63
64
    public function revoke(): void
65
    {
66
        $this->revoked = true;
67
    }
68
69
    /**
70
     * It will only set the identifier if it's not yet set.
71
     */
72
    public function setIdentifier($identifier): void
73
    {
74
        $this->identifier = $this->identifier ?? $identifier;
75
    }
76
}
77