Failed Conditions
Push — master ( c24b3a...b512e9 )
by Florent
14:42
created

InitialAccessTokenCreatedEvent::getPayload()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\ClientRegistrationEndpoint\Event;
15
16
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId;
17
use OAuth2Framework\Component\Core\Domain\DomainObject;
18
use OAuth2Framework\Component\Core\Event\Event;
19
use OAuth2Framework\Component\Core\Id\Id;
20
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
21
22
class InitialAccessTokenCreatedEvent extends Event
23
{
24
    /**
25
     * @var InitialAccessTokenId
26
     */
27
    private $initialAccessTokenId;
28
29
    /**
30
     * @var \DateTimeImmutable|null
31
     */
32
    private $expiresAt;
33
34
    /**
35
     * @var UserAccountId
36
     */
37
    private $userAccountId;
38
39
    /**
40
     * InitialAccessTokenCreatedEvent constructor.
41
     *
42
     * @param InitialAccessTokenId    $initialAccessTokenId
43
     * @param UserAccountId           $userAccountId
44
     * @param null|\DateTimeImmutable $expiresAt
45
     */
46
    protected function __construct(InitialAccessTokenId $initialAccessTokenId, UserAccountId $userAccountId, ? \DateTimeImmutable $expiresAt)
47
    {
48
        $this->initialAccessTokenId = $initialAccessTokenId;
49
        $this->expiresAt = $expiresAt;
50
        $this->userAccountId = $userAccountId;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public static function getSchema(): string
57
    {
58
        return 'https://oauth2-framework.spomky-labs.com/schemas/events/initial-access-token/created/1.0/schema';
59
    }
60
61
    /**
62
     * @param InitialAccessTokenId    $initialAccessTokenId
63
     * @param UserAccountId           $userAccountId
64
     * @param null|\DateTimeImmutable $expiresAt
65
     *
66
     * @return InitialAccessTokenCreatedEvent
67
     */
68
    public static function create(InitialAccessTokenId $initialAccessTokenId, UserAccountId $userAccountId, ? \DateTimeImmutable $expiresAt): self
69
    {
70
        return new self($initialAccessTokenId, $userAccountId, $expiresAt);
71
    }
72
73
    /**
74
     * @return InitialAccessTokenId
75
     */
76
    public function getInitialAccessTokenId(): InitialAccessTokenId
77
    {
78
        return $this->initialAccessTokenId;
79
    }
80
81
    /**
82
     * @return UserAccountId
83
     */
84
    public function getUserAccountId(): UserAccountId
85
    {
86
        return $this->userAccountId;
87
    }
88
89
    /**
90
     * @return \DateTimeImmutable|null
91
     */
92
    public function getExpiresAt(): ?\DateTimeImmutable
93
    {
94
        return $this->expiresAt;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getDomainId(): Id
101
    {
102
        return $this->getInitialAccessTokenId();
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getPayload()
109
    {
110
        return (object) [
111
            'user_account_id' => $this->userAccountId ? $this->userAccountId->getValue() : null,
112
            'expires_at' => $this->expiresAt ? $this->expiresAt->getTimestamp() : null,
113
        ];
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public static function createFromJson(\stdClass $json): DomainObject
120
    {
121
        $initialAccessTokenId = InitialAccessTokenId::create($json->domain_id);
122
        $userAccountId = null === $json->payload->user_account_id ? null : UserAccountId::create($json->payload->user_account_id);
123
        $expiresAt = null === $json->payload->expires_at ? null : \DateTimeImmutable::createFromFormat('U', (string) $json->payload->expires_at);
124
125
        return new self(
126
            $initialAccessTokenId,
127
            $userAccountId,
128
            $expiresAt
129
        );
130
    }
131
}
132