Failed Conditions
Push — master ( 7f2d83...323120 )
by Florent
05:07
created

AuthorizationRequest::getPrompt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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