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

RefreshTokenCreatedEvent   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 5
dl 0
loc 180
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getSchema() 0 4 1
A create() 0 4 1
A getRefreshTokenId() 0 4 1
A getResourceOwnerId() 0 4 1
A getClientId() 0 4 1
A getParameters() 0 4 1
A getExpiresAt() 0 4 1
A getMetadatas() 0 4 1
A getResourceServerId() 0 4 1
A getDomainId() 0 4 1
A getPayload() 0 12 2
A createFromJson() 0 13 2
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\RefreshTokenGrant\Event;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\DataBag\DataBag;
18
use OAuth2Framework\Component\Core\Event\Event;
19
use OAuth2Framework\Component\Core\Id\Id;
20
use OAuth2Framework\Component\RefreshTokenGrant\RefreshTokenId;
21
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
22
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
23
use OAuth2Framework\Component\Core\Domain\DomainObject;
24
25
class RefreshTokenCreatedEvent extends Event
26
{
27
    /**
28
     * @var RefreshTokenId
29
     */
30
    private $refreshTokenId;
31
32
    /**
33
     * @var ResourceOwnerId
34
     */
35
    private $resourceOwnerId;
36
37
    /**
38
     * @var ClientId
39
     */
40
    private $clientId;
41
42
    /**
43
     * @var DataBag
44
     */
45
    private $parameters;
46
47
    /**
48
     * @var \DateTimeImmutable
49
     */
50
    private $expiresAt;
51
52
    /**
53
     * @var DataBag
54
     */
55
    private $metadatas;
56
57
    /**
58
     * @var ResourceServerId|null
59
     */
60
    private $resourceServerId;
61
62
    /**
63
     * RefreshTokenCreatedEvent constructor.
64
     *
65
     * @param RefreshTokenId        $refreshTokenId
66
     * @param ResourceOwnerId       $resourceOwnerId
67
     * @param ClientId              $clientId
68
     * @param DataBag               $parameters
69
     * @param DataBag               $metadatas
70
     * @param \DateTimeImmutable    $expiresAt
71
     * @param ResourceServerId|null $resourceServerId
72
     */
73
    protected function __construct(RefreshTokenId $refreshTokenId, ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, \DateTimeImmutable $expiresAt, ? ResourceServerId $resourceServerId)
74
    {
75
        $this->refreshTokenId = $refreshTokenId;
76
        $this->resourceOwnerId = $resourceOwnerId;
77
        $this->clientId = $clientId;
78
        $this->parameters = $parameters;
79
        $this->expiresAt = $expiresAt;
80
        $this->metadatas = $metadatas;
81
        $this->resourceServerId = $resourceServerId;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public static function getSchema(): string
88
    {
89
        return 'https://oauth2-framework.spomky-labs.com/schemas/events/refresh-token/created/1.0/schema';
90
    }
91
92
    /**
93
     * @param RefreshTokenId        $refreshTokenId
94
     * @param ResourceOwnerId       $resourceOwnerId
95
     * @param ClientId              $clientId
96
     * @param DataBag               $parameters
97
     * @param DataBag               $metadatas
98
     * @param \DateTimeImmutable    $expiresAt
99
     * @param ResourceServerId|null $resourceServerId
100
     *
101
     * @return RefreshTokenCreatedEvent
102
     */
103
    public static function create(RefreshTokenId $refreshTokenId, ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, \DateTimeImmutable $expiresAt, ? ResourceServerId $resourceServerId): self
104
    {
105
        return new self($refreshTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId);
106
    }
107
108
    /**
109
     * @return RefreshTokenId
110
     */
111
    public function getRefreshTokenId(): RefreshTokenId
112
    {
113
        return $this->refreshTokenId;
114
    }
115
116
    /**
117
     * @return ResourceOwnerId
118
     */
119
    public function getResourceOwnerId(): ResourceOwnerId
120
    {
121
        return $this->resourceOwnerId;
122
    }
123
124
    /**
125
     * @return ClientId
126
     */
127
    public function getClientId(): ClientId
128
    {
129
        return $this->clientId;
130
    }
131
132
    /**
133
     * @return DataBag
134
     */
135
    public function getParameters(): DataBag
136
    {
137
        return $this->parameters;
138
    }
139
140
    /**
141
     * @return \DateTimeImmutable
142
     */
143
    public function getExpiresAt(): \DateTimeImmutable
144
    {
145
        return $this->expiresAt;
146
    }
147
148
    /**
149
     * @return DataBag
150
     */
151
    public function getMetadatas(): DataBag
152
    {
153
        return $this->metadatas;
154
    }
155
156
    /**
157
     * @return ResourceServerId
0 ignored issues
show
Documentation introduced by
Should the return type not be ResourceServerId|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
158
     */
159
    public function getResourceServerId(): ? ResourceServerId
160
    {
161
        return $this->resourceServerId;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getDomainId(): Id
168
    {
169
        return $this->getRefreshTokenId();
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getPayload()
176
    {
177
        return (object) [
178
            'resource_owner_id' => $this->resourceOwnerId->jsonSerialize(),
179
            'resource_owner_class' => get_class($this->resourceOwnerId),
180
            'client_id' => $this->clientId->jsonSerialize(),
181
            'parameters' => (object) $this->parameters->all(),
182
            'expires_at' => $this->expiresAt->getTimestamp(),
183
            'metadatas' => (object) $this->metadatas->all(),
184
            'resource_server_id' => $this->resourceServerId ? $this->resourceServerId->getValue() : null,
185
        ];
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public static function createFromJson(\stdClass $json): DomainObject
192
    {
193
        $refreshTokenId = RefreshTokenId::create($json->domain_id);
194
        $resourceOwnerClass = $json->payload->resource_owner_class;
195
        $resourceOwnerId = $resourceOwnerClass::create($json->payload->resource_owner_id);
196
        $clientId = ClientId::create($json->payload->client_id);
197
        $parameters = DataBag::create((array) $json->payload->parameters);
198
        $metadatas = DataBag::create((array) $json->payload->metadatas);
199
        $expiresAt = \DateTimeImmutable::createFromFormat('U', (string) $json->payload->expires_at);
200
        $resourceServerId = null !== $json->payload->resource_server_id ? ResourceServerId::create($json->payload->resource_server_id) : null;
201
202
        return new self($refreshTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId);
203
    }
204
}
205