Failed Conditions
Push — master ( dc4823...aacec5 )
by Florent
04:42
created

AuthorizationCode::getClientId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\AuthorizationCodeGrant;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\DataBag\DataBag;
18
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
19
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
20
use OAuth2Framework\Component\Core\Token\TokenId;
21
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
22
23
class AuthorizationCode implements \JsonSerializable
24
{
25
    /**
26
     * @var array
27
     */
28
    private $queryParameters;
29
30
    /**
31
     * @var string
32
     */
33
    private $redirectUri;
34
35
    /**
36
     * @var bool
37
     */
38
    private $used;
39
40
    /**
41
     * @var TokenId
42
     */
43
    protected $tokenId;
44
45
    /**
46
     * @var \DateTimeImmutable
47
     */
48
    private $expiresAt;
49
50
    /**
51
     * @var ResourceOwnerId
52
     */
53
    private $resourceOwnerId;
54
55
    /**
56
     * @var ClientId
57
     */
58
    private $clientId;
59
60
    /**
61
     * @var DataBag
62
     */
63
    private $parameter;
64
65
    /**
66
     * @var DataBag
67
     */
68
    private $metadata;
69
70
    /**
71
     * @var bool
72
     */
73
    private $revoked;
74
75
    /**
76
     * @var ResourceServerId|null
77
     */
78
    private $resourceServerId;
79
80
    public function __construct(AuthorizationCodeId $authorizationCodeId, ClientId $clientId, UserAccountId $userAccountId, array $queryParameters, string $redirectUri, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId)
81
    {
82
        $this->queryParameters = $queryParameters;
83
        $this->redirectUri = $redirectUri;
84
        $this->used = false;
85
        $this->tokenId = $authorizationCodeId;
86
        $this->resourceOwnerId = $userAccountId;
87
        $this->clientId = $clientId;
88
        $this->parameter = $parameter;
89
        $this->metadata = $metadata;
90
        $this->expiresAt = $expiresAt;
91
        $this->resourceServerId = $resourceServerId;
92
        $this->revoked = false;
93
    }
94
95
    public function getQueryParameters(): array
96
    {
97
        return $this->queryParameters;
98
    }
99
100
    public function isUsed(): bool
101
    {
102
        return $this->used;
103
    }
104
105
    public function markAsUsed(): void
106
    {
107
        $this->used = true;
108
    }
109
110
    public function getQueryParams(): array
111
    {
112
        return $this->queryParameters;
113
    }
114
115
    public function getQueryParam(string $key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
116
    {
117
        if (!$this->hasQueryParam($key)) {
118
            throw new \RuntimeException(\Safe\sprintf('Query parameter with key "%s" does not exist.', $key));
119
        }
120
121
        return $this->queryParameters[$key];
122
    }
123
124
    public function hasQueryParam(string $key): bool
125
    {
126
        return \array_key_exists($key, $this->getQueryParams());
127
    }
128
129
    public function getRedirectUri(): string
130
    {
131
        return $this->redirectUri;
132
    }
133
134
    public function toArray(): array
135
    {
136
        return [
137
            'code' => $this->getTokenId()->getValue(),
138
        ];
139
    }
140
141
    public function getTokenId(): TokenId
142
    {
143
        return $this->tokenId;
144
    }
145
146
    public function getExpiresAt(): \DateTimeImmutable
147
    {
148
        return $this->expiresAt;
149
    }
150
151
    public function hasExpired(): bool
152
    {
153
        return $this->expiresAt->getTimestamp() < \time();
154
    }
155
156
    public function getResourceOwnerId(): ResourceOwnerId
157
    {
158
        return $this->resourceOwnerId;
159
    }
160
161
    public function getClientId(): ClientId
162
    {
163
        return $this->clientId;
164
    }
165
166
    public function getParameter(): DataBag
167
    {
168
        return $this->parameter;
169
    }
170
171
    public function getMetadata(): DataBag
172
    {
173
        return $this->metadata;
174
    }
175
176
    public function isRevoked(): bool
177
    {
178
        return $this->revoked;
179
    }
180
181
    public function markAsRevoked(): void
182
    {
183
        $this->revoked = true;
184
    }
185
186
    public function getResourceServerId(): ?ResourceServerId
187
    {
188
        return $this->resourceServerId;
189
    }
190
191
    public function getExpiresIn(): int
192
    {
193
        $expiresAt = $this->expiresAt;
194
        if (null === $expiresAt) {
195
            return 0;
196
        }
197
198
        return $this->expiresAt->getTimestamp() - \time() < 0 ? 0 : $this->expiresAt->getTimestamp() - \time();
199
    }
200
201
    public function jsonSerialize()
202
    {
203
        $data = [
204
            'auth_code_id' => $this->getTokenId()->getValue(),
205
            'query_parameters' => (object) $this->getQueryParameters(),
206
            'redirect_uri' => $this->getRedirectUri(),
207
            'is_used' => $this->isUsed(),
208
            'expires_at' => $this->getExpiresAt()->getTimestamp(),
209
            'client_id' => $this->getClientId()->getValue(),
210
            'parameters' => (object) $this->getParameter()->all(),
211
            'metadatas' => (object) $this->getMetadata()->all(),
212
            'is_revoked' => $this->isRevoked(),
213
            'resource_owner_id' => $this->getResourceOwnerId()->getValue(),
214
            'resource_owner_class' => \get_class($this->getResourceOwnerId()),
215
            'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null,
216
        ];
217
218
        return $data;
219
    }
220
}
221