Failed Conditions
Push — master ( 4fec28...cca42a )
by Florent
04:20
created

AccessToken   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 10
dl 0
loc 206
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A createEmpty() 0 4 1
A getSchema() 0 4 1
A create() 0 16 1
A getTokenId() 0 8 2
A getAccessTokenId() 0 9 2
A markAsRevoked() 0 9 1
A jsonSerialize() 0 8 1
B createFromJson() 0 29 3
A getResponseData() 0 8 1
A apply() 0 13 4
A getEventMap() 0 7 1
A applyAccessTokenCreatedEvent() 0 13 1
A applyAccessTokenRevokedEvent() 0 7 1
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\Core\AccessToken;
15
16
use OAuth2Framework\Component\Core\AccessToken\Event as AccessTokenEvent;
17
use OAuth2Framework\Component\Core\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Core\Event\Event;
20
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
21
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
22
use OAuth2Framework\Component\Core\Token\Token;
23
use OAuth2Framework\Component\Core\Token\TokenId;
24
use OAuth2Framework\Component\Core\Domain\DomainObject;
25
26
class AccessToken extends Token
27
{
28
    /**
29
     * @var AccessTokenId
30
     */
31
    private $accessTokenId = null;
32
33
    /**
34
     * @return AccessToken
35
     */
36
    public static function createEmpty(): self
37
    {
38
        return new self();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public static function getSchema(): string
45
    {
46
        return 'https://oauth2-framework.spomky-labs.com/schemas/model/access-token/1.0/schema';
47
    }
48
49
    /**
50
     * @param AccessTokenId         $accessTokenId
51
     * @param ResourceOwnerId       $resourceOwnerId
52
     * @param ClientId              $clientId
53
     * @param DataBag               $parameters
54
     * @param DataBag               $metadatas
55
     * @param \DateTimeImmutable    $expiresAt
56
     * @param ResourceServerId|null $resourceServerId
57
     *
58
     * @return AccessToken
59
     */
60
    public function create(AccessTokenId $accessTokenId, ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, \DateTimeImmutable $expiresAt, ? ResourceServerId $resourceServerId)
61
    {
62
        $clone = clone $this;
63
        $clone->accessTokenId = $accessTokenId;
64
        $clone->resourceOwnerId = $resourceOwnerId;
65
        $clone->clientId = $clientId;
66
        $clone->parameters = $parameters;
67
        $clone->metadatas = $metadatas;
68
        $clone->expiresAt = $expiresAt;
69
        $clone->resourceServerId = $resourceServerId;
70
71
        $event = AccessTokenEvent\AccessTokenCreatedEvent::create($accessTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId);
72
        $clone->record($event);
73
74
        return $clone;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getTokenId(): TokenId
81
    {
82
        if (null === $this->accessTokenId) {
83
            throw new \RuntimeException('Access token not initialized.');
84
        }
85
86
        return $this->accessTokenId;
87
    }
88
89
    /**
90
     * @return AccessTokenId
91
     */
92
    public function getAccessTokenId(): AccessTokenId
93
    {
94
        $id = $this->getTokenId();
95
        if (!$id instanceof AccessTokenId) {
96
            throw new \RuntimeException('Access token not initialized.');
97
        }
98
99
        return $this->accessTokenId;
100
    }
101
102
    /**
103
     * @return AccessToken
104
     */
105
    public function markAsRevoked(): self
106
    {
107
        $clone = clone $this;
108
        $clone->revoked = true;
109
        $event = AccessTokenEvent\AccessTokenRevokedEvent::create($clone->getTokenId());
110
        $clone->record($event);
111
112
        return $clone;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function jsonSerialize()
119
    {
120
        $data = parent::jsonSerialize() + [
121
            'access_token_id' => $this->getTokenId()->getValue(),
122
        ];
123
124
        return $data;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public static function createFromJson(\stdClass $json): DomainObject
131
    {
132
        $accessTokenId = AccessTokenId::create($json->access_token_id);
133
        $resourceServerId = null !== $json->resource_server_id ? ResourceServerId::create($json->resource_server_id) : null;
134
135
        $expiresAt = \DateTimeImmutable::createFromFormat('U', (string) $json->expires_at);
136
        $clientId = ClientId::create($json->client_id);
137
        $parameters = DataBag::create((array) $json->parameters);
138
        $metadatas = DataBag::create((array) $json->metadatas);
139
        $revoked = $json->is_revoked;
140
        $resourceOwnerClass = $json->resource_owner_class;
141
        if (!method_exists($resourceOwnerClass, 'create')) {
142
            throw new \InvalidArgumentException('Invalid resource owner.');
143
        }
144
        $resourceOwnerId = $resourceOwnerClass::create($json->resource_owner_id);
145
146
        $accessToken = new self();
147
        $accessToken->accessTokenId = $accessTokenId;
148
        $accessToken->resourceServerId = $resourceServerId;
149
150
        $accessToken->expiresAt = $expiresAt;
151
        $accessToken->clientId = $clientId;
152
        $accessToken->parameters = $parameters;
153
        $accessToken->metadatas = $metadatas;
154
        $accessToken->revoked = $revoked;
155
        $accessToken->resourceOwnerId = $resourceOwnerId;
156
157
        return $accessToken;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    public function getResponseData(): array
164
    {
165
        $data = $this->getParameters()->all();
166
        $data['access_token'] = $this->getTokenId()->getValue();
167
        $data['expires_in'] = $this->getExpiresIn();
168
169
        return $data;
170
    }
171
172
    /**
173
     * @param Event $event
174
     *
175
     * @return AccessToken
176
     */
177
    public function apply(Event $event): self
178
    {
179
        $map = $this->getEventMap();
180
        if (!array_key_exists($event->getType(), $map)) {
181
            throw new \InvalidArgumentException('Unsupported event.');
182
        }
183
        if (null !== $this->clientId && $this->accessTokenId->getValue() !== $event->getDomainId()->getValue()) {
184
            throw new \InvalidArgumentException('Event not applicable for this access token.');
185
        }
186
        $method = $map[$event->getType()];
187
188
        return $this->$method($event);
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    private function getEventMap(): array
195
    {
196
        return [
197
            AccessTokenEvent\AccessTokenCreatedEvent::class => 'applyAccessTokenCreatedEvent',
198
            AccessTokenEvent\AccessTokenRevokedEvent::class => 'applyAccessTokenRevokedEvent',
199
        ];
200
    }
201
202
    /**
203
     * @param AccessTokenEvent\AccessTokenCreatedEvent $event
204
     *
205
     * @return AccessToken
206
     */
207
    protected function applyAccessTokenCreatedEvent(AccessTokenEvent\AccessTokenCreatedEvent $event): self
208
    {
209
        $clone = clone $this;
210
        $clone->accessTokenId = $event->getAccessTokenId();
211
        $clone->resourceOwnerId = $event->getResourceOwnerId();
212
        $clone->clientId = $event->getClientId();
213
        $clone->parameters = $event->getParameters();
214
        $clone->metadatas = $event->getMetadatas();
215
        $clone->expiresAt = $event->getExpiresAt();
216
        $clone->resourceServerId = $event->getResourceServerId();
217
218
        return $clone;
219
    }
220
221
    /**
222
     * @return AccessToken
223
     */
224
    protected function applyAccessTokenRevokedEvent(): self
225
    {
226
        $clone = clone $this;
227
        $clone->revoked = true;
228
229
        return $clone;
230
    }
231
}
232