Failed Conditions
Push — master ( 819484...23fc45 )
by Florent
03:33
created

AuthorizationRequest::hasResponseMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 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\AuthorizationEndpoint\AuthorizationRequest;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode;
18
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType\ResponseType;
19
use OAuth2Framework\Component\Core\Client\Client;
20
use OAuth2Framework\Component\Core\DataBag\DataBag;
21
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer;
22
use OAuth2Framework\Component\Core\TokenType\TokenType;
23
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
24
25
class AuthorizationRequest
26
{
27
    /**
28
     * @var bool
29
     */
30
    private $authorized;
31
32
    /**
33
     * @var Client
34
     */
35
    private $client;
36
37
    /**
38
     * @var UserAccount|null
39
     */
40
    private $userAccount;
41
42
    /**
43
     * @var DataBag
44
     */
45
    private $metadata;
46
47
    /**
48
     * @var TokenType|null
49
     */
50
    private $tokenType;
51
52
    /**
53
     * @var ResponseType]
54
     */
55
    private $responseType;
56
57
    /**
58
     * @var ResponseMode|null
59
     */
60
    private $responseMode;
61
62
    /**
63
     * @var array
64
     */
65
    private $queryParameters = [];
66
67
    /**
68
     * @var string|null
69
     */
70
    private $redirectUri;
71
72
    /**
73
     * @var array
74
     */
75
    private $consentScreenOptions = [];
76
77
    /**
78
     * @var array
79
     */
80
    private $responseParameters = [];
81
82
    /**
83
     * @var array
84
     */
85
    private $responseHeaders = [];
86
87
    /**
88
     * @var ResourceServer|null
89
     */
90
    private $resourceServer;
91
92
    public function __construct(Client $client, array $queryParameters)
93
    {
94
        $this->client = $client;
95
        $this->queryParameters = $queryParameters;
96
        $this->metadata = new DataBag([]);
97
    }
98
99
    public function getQueryParams(): array
100
    {
101
        return $this->queryParameters;
102
    }
103
104
    public function hasQueryParam(string $param): bool
105
    {
106
        return \array_key_exists($param, $this->queryParameters);
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function getQueryParam(string $param)
113
    {
114
        Assertion::true($this->hasQueryParam($param), \Safe\sprintf('Invalid parameter "%s".', $param));
115
116
        return $this->queryParameters[$param];
117
    }
118
119
    public function getClient(): Client
120
    {
121
        return $this->client;
122
    }
123
124
    public function setTokenType(TokenType $tokenType): void
125
    {
126
        $this->tokenType = $tokenType;
127
    }
128
129
    public function getTokenType(): ?TokenType
130
    {
131
        return $this->tokenType;
132
    }
133
134
    public function setResponseType(ResponseType $responseType): void
135
    {
136
        $this->responseType = $responseType;
137
    }
138
139
    public function getResponseType(): ResponseType
140
    {
141
        return $this->responseType;
142
    }
143
144
    public function setResponseMode(ResponseMode $responseMode): void
145
    {
146
        $this->responseMode = $responseMode;
147
    }
148
149
    public function hasResponseMode(): bool
150
    {
151
        return null === $this->responseMode;
152
    }
153
154
    public function getResponseMode(): ResponseMode
155
    {
156
        Assertion::notNull($this->responseMode, 'No response mode');
157
158
        return $this->responseMode;
159
    }
160
161
    public function setRedirectUri(string $redirectUri): void
162
    {
163
        $this->redirectUri = $redirectUri;
164
    }
165
166
    public function hasRedirectUri(): bool
167
    {
168
        return null !== $this->redirectUri;
169
    }
170
171
    public function getRedirectUri(): string
172
    {
173
        Assertion::notNull($this->redirectUri, 'internal_server_error');
174
175
        return $this->redirectUri;
176
    }
177
178
    public function setUserAccount(UserAccount $userAccount): void
179
    {
180
        $this->userAccount = $userAccount;
181
    }
182
183
    public function hasUserAccount(): bool
184
    {
185
        return null !== $this->userAccount;
186
    }
187
188
    public function getUserAccount(): UserAccount
189
    {
190
        Assertion::notNull($this->userAccount, 'internal_server_error');
191
192
        return $this->userAccount;
193
    }
194
195
    /**
196
     * @param mixed $value
197
     */
198
    public function setResponseParameter(string $responseParameter, $value): void
199
    {
200
        $this->responseParameters[$responseParameter] = $value;
201
    }
202
203
    public function getResponseParameters(): array
204
    {
205
        return $this->responseParameters;
206
    }
207
208
    /**
209
     * @return mixed
210
     */
211
    public function getResponseParameter(string $param)
212
    {
213
        Assertion::true($this->hasResponseParameter($param), \Safe\sprintf('Invalid response parameter "%s".', $param));
214
215
        return $this->getResponseParameters()[$param];
216
    }
217
218
    public function hasResponseParameter(string $param): bool
219
    {
220
        return \array_key_exists($param, $this->getResponseParameters());
221
    }
222
223
    /**
224
     * @param mixed $value
225
     */
226
    public function setResponseHeader(string $responseHeader, $value): void
227
    {
228
        $this->responseHeaders[$responseHeader] = $value;
229
    }
230
231
    public function getResponseHeaders(): array
232
    {
233
        return $this->responseHeaders;
234
    }
235
236
    /**
237
     * @return string[]
238
     */
239
    public function getPrompt(): array
240
    {
241
        if (!$this->hasQueryParam('prompt')) {
242
            return [];
243
        }
244
245
        return \explode(' ', $this->getQueryParam('prompt'));
246
    }
247
248
    public function hasUiLocales(): bool
249
    {
250
        return $this->hasQueryParam('ui_locales');
251
    }
252
253
    /**
254
     * @return string[]
255
     */
256
    public function getUiLocales(): array
257
    {
258
        return $this->hasQueryParam('ui_locales') ? \explode(' ', $this->getQueryParam('ui_locales')) : [];
259
    }
260
261
    public function hasPrompt(string $prompt): bool
262
    {
263
        return \in_array($prompt, $this->getPrompt(), true);
264
    }
265
266
    public function isAuthorized(): bool
267
    {
268
        return $this->authorized;
269
    }
270
271
    public function allow(): void
272
    {
273
        $this->authorized = true;
274
    }
275
276
    public function deny(): void
277
    {
278
        $this->authorized = false;
279
    }
280
281
    public function getResourceServer(): ?ResourceServer
282
    {
283
        return $this->resourceServer;
284
    }
285
286
    public function setResourceServer(ResourceServer $resourceServer): void
287
    {
288
        $this->resourceServer = $resourceServer;
289
    }
290
291
    /**
292
     * @param mixed $value
293
     */
294
    public function setConsentScreenOption(string $option, $value): void
295
    {
296
        $this->consentScreenOptions[$option] = $value;
297
    }
298
299
    public function hasScope(): bool
300
    {
301
        return $this->hasQueryParam('scope');
302
    }
303
304
    public function getScope(): string
305
    {
306
        return $this->getQueryParam('scope');
307
    }
308
309
    public function getMetadata(): DataBag
310
    {
311
        return $this->metadata;
312
    }
313
}
314