Failed Conditions
Push — master ( 8b88a3...8d741a )
by Florent
04:37
created

Authorization::isUserAccountFullyAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\AuthorizationEndpoint;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode;
17
use OAuth2Framework\Component\Core\Client\Client;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer;
20
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
21
use OAuth2Framework\Component\Core\TokenType\TokenType;
22
23
class Authorization
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
24
{
25
    /**
26
     * @var bool
27
     */
28
    private $authorized;
29
30
    /**
31
     * @var Client
32
     */
33
    private $client;
34
35
    /**
36
     * @var UserAccount|null
37
     */
38
    private $userAccount = null;
39
40
    /**
41
     * @var DataBag
42
     */
43
    private $metadata;
44
45
    /**
46
     * @var null|bool
47
     */
48
    private $userAccountFullyAuthenticated = null;
49
50
    /**
51
     * @var array
52
     */
53
    private $data = [];
54
55
    /**
56
     * @var TokenType|null
57
     */
58
    private $tokenType = null;
59
60
    /**
61
     * @var ResponseType]
62
     */
63
    private $responseType = null;
64
65
    /**
66
     * @var ResponseMode|null
67
     */
68
    private $responseMode = null;
69
70
    /**
71
     * @var array
72
     */
73
    private $queryParameters = [];
74
75
    /**
76
     * @var string|null
77
     */
78
    private $redirectUri = null;
79
80
    /**
81
     * @var array
82
     */
83
    private $consentScreenOptions = [];
84
85
    /**
86
     * @var array
87
     */
88
    private $responseParameters = [];
89
90
    /**
91
     * @var array
92
     */
93
    private $responseHeaders = [];
94
95
    /**
96
     * @var null|ResourceServer
97
     */
98
    private $resourceServer = null;
99
100
    /**
101
     * Authorization constructor.
102
     *
103
     * @param Client $client
104
     * @param array  $queryParameters
105
     */
106
    private function __construct(Client $client, array $queryParameters)
107
    {
108
        $this->client = $client;
109
        $this->queryParameters = $queryParameters;
110
        $this->metadata = DataBag::create([]);
111
    }
112
113
    /**
114
     * @param Client $client
115
     * @param array  $queryParameters
116
     *
117
     * @return Authorization
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

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...
118
     */
119
    public static function create(Client $client, array $queryParameters): self
120
    {
121
        return new self($client, $queryParameters);
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getQueryParams(): array
128
    {
129
        return $this->queryParameters;
130
    }
131
132
    /**
133
     * @param string $param
134
     *
135
     * @return bool
136
     */
137
    public function hasQueryParam(string $param): bool
138
    {
139
        return array_key_exists($param, $this->queryParameters);
140
    }
141
142
    /**
143
     * @param string $param
144
     *
145
     * @return mixed
146
     */
147
    public function getQueryParam(string $param)
148
    {
149
        if (!$this->hasQueryParam($param)) {
150
            throw new \InvalidArgumentException(sprintf('Invalid parameter "%s".', $param));
151
        }
152
153
        return $this->queryParameters[$param];
154
    }
155
156
    /**
157
     * @return Client
158
     */
159
    public function getClient(): Client
160
    {
161
        return $this->client;
162
    }
163
164
    /**
165
     * @param TokenType $tokenType
166
     *
167
     * @return Authorization
168
     */
169
    public function withTokenType(TokenType $tokenType): self
170
    {
171
        $this->tokenType = $tokenType;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return null|TokenType
178
     */
179
    public function getTokenType(): ? TokenType
180
    {
181
        return $this->tokenType;
182
    }
183
184
    /**
185
     * @param ResponseType $responseType
186
     *
187
     * @return Authorization
188
     */
189
    public function withResponseType(ResponseType $responseType): self
190
    {
191
        $this->responseType = $responseType;
192
193
        return $this;
194
    }
195
196
    /**
197
     * @return ResponseType
198
     */
199
    public function getResponseType(): ResponseType
200
    {
201
        return $this->responseType;
202
    }
203
204
    /**
205
     * @param ResponseMode $responseMode
206
     *
207
     * @return Authorization
208
     */
209
    public function withResponseMode(ResponseMode $responseMode): self
210
    {
211
        $this->responseMode = $responseMode;
212
213
        return $this;
214
    }
215
216
    /**
217
     * @return null|ResponseMode
218
     */
219
    public function getResponseMode(): ? ResponseMode
220
    {
221
        return $this->responseMode;
222
    }
223
224
    /**
225
     * @param string $redirectUri
226
     *
227
     * @return Authorization
228
     */
229
    public function withRedirectUri(string $redirectUri): self
230
    {
231
        $this->redirectUri = $redirectUri;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @return null|string
238
     */
239
    public function getRedirectUri(): ? string
240
    {
241
        return $this->redirectUri;
242
    }
243
244
    /**
245
     * @param UserAccount $userAccount
246
     * @param bool        $isFullyAuthenticated
247
     *
248
     * @return Authorization
249
     */
250
    public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self
251
    {
252
        $this->userAccount = $userAccount;
253
        $this->userAccountFullyAuthenticated = $isFullyAuthenticated;
254
255
        return $this;
256
    }
257
258
    /**
259
     * @return null|UserAccount
260
     */
261
    public function getUserAccount(): ? UserAccount
262
    {
263
        return $this->userAccount;
264
    }
265
266
    /**
267
     * @param string $responseParameter
268
     * @param mixed  $value
269
     *
270
     * @return Authorization
271
     */
272
    public function withResponseParameter(string $responseParameter, $value): self
273
    {
274
        $this->responseParameters[$responseParameter] = $value;
275
276
        return $this;
277
    }
278
279
    /**
280
     * @return array
281
     */
282
    public function getResponseParameters(): array
283
    {
284
        return $this->responseParameters;
285
    }
286
287
    /**
288
     * @param string $param
289
     *
290
     * @return mixed
291
     */
292
    public function getResponseParameter(string $param)
293
    {
294
        if (!$this->hasResponseParameter($param)) {
295
            throw new \InvalidArgumentException(sprintf('Invalid response parameter "%s".', $param));
296
        }
297
298
        return $this->getResponseParameters()[$param];
299
    }
300
301
    /**
302
     * @param string $param
303
     *
304
     * @return bool
305
     */
306
    public function hasResponseParameter(string $param): bool
307
    {
308
        return array_key_exists($param, $this->getResponseParameters());
309
    }
310
311
    /**
312
     * @param string $responseHeader
313
     * @param mixed  $value
314
     *
315
     * @return Authorization
316
     */
317
    public function withResponseHeader(string $responseHeader, $value): self
318
    {
319
        $this->responseHeaders[$responseHeader] = $value;
320
321
        return $this;
322
    }
323
324
    /**
325
     * @return array
326
     */
327
    public function getResponseHeaders(): array
328
    {
329
        return $this->responseHeaders;
330
    }
331
332
    /**
333
     * @return bool|null
334
     */
335
    public function isUserAccountFullyAuthenticated(): ? bool
336
    {
337
        return $this->userAccountFullyAuthenticated;
338
    }
339
340
    /**
341
     * @return string[]
342
     */
343
    public function getPrompt(): array
344
    {
345
        if (!$this->hasQueryParam('prompt')) {
346
            return [];
347
        }
348
349
        return explode(' ', $this->getQueryParam('prompt'));
350
    }
351
352
    /**
353
     * @return bool
354
     */
355
    public function hasUiLocales(): bool
356
    {
357
        return $this->hasQueryParam('ui_locales');
358
    }
359
360
    /**
361
     * @return string[]
362
     */
363
    public function getUiLocales(): array
364
    {
365
        return $this->hasQueryParam('ui_locales') ? explode(' ', $this->getQueryParam('ui_locales')) : [];
366
    }
367
368
    /**
369
     * @param string $prompt
370
     *
371
     * @return bool
372
     */
373
    public function hasPrompt(string $prompt): bool
374
    {
375
        return in_array($prompt, $this->getPrompt());
376
    }
377
378
    /**
379
     * @return bool
380
     */
381
    public function isAuthorized(): bool
382
    {
383
        return $this->authorized;
384
    }
385
386
    /**
387
     * @return Authorization
388
     */
389
    public function allow(): self
390
    {
391
        $this->authorized = true;
392
393
        return $this;
394
    }
395
396
    /**
397
     * @return Authorization
398
     */
399
    public function deny(): self
400
    {
401
        $this->authorized = false;
402
403
        return $this;
404
    }
405
406
    /**
407
     * @param string $key
408
     *
409
     * @return bool
410
     */
411
    public function hasData(string $key): bool
412
    {
413
        return array_key_exists($key, $this->data);
414
    }
415
416
    /**
417
     * @param string $key
418
     *
419
     * @return mixed
420
     */
421
    public function getData(string $key)
422
    {
423
        if (!$this->hasData($key)) {
424
            throw new \InvalidArgumentException(sprintf('Invalid data "%s".', $key));
425
        }
426
427
        return $this->data[$key];
428
    }
429
430
    /**
431
     * @param string $key
432
     * @param mixed  $data
433
     *
434
     * @return Authorization
435
     */
436
    public function withData(string $key, $data): self
437
    {
438
        $this->data[$key] = $data;
439
440
        return $this;
441
    }
442
443
    /**
444
     * @return null|ResourceServer
445
     */
446
    public function getResourceServer(): ? ResourceServer
447
    {
448
        return $this->resourceServer;
449
    }
450
451
    /**
452
     * @param ResourceServer $resourceServer
453
     *
454
     * @return Authorization
455
     */
456
    public function withResourceServer(ResourceServer $resourceServer): self
457
    {
458
        $this->resourceServer = $resourceServer;
459
460
        return $this;
461
    }
462
463
    /**
464
     * @param string $option
465
     * @param mixed  $value
466
     *
467
     * @return Authorization
468
     */
469
    public function withConsentScreenOption(string $option, $value): self
470
    {
471
        $this->consentScreenOptions[$option] = $value;
472
473
        return $this;
474
    }
475
476
    /**
477
     * @param string $option
478
     *
479
     * @return Authorization
480
     */
481
    public function withoutConsentScreenOption(string $option): self
482
    {
483
        if (!array_key_exists($option, $this->consentScreenOptions)) {
484
            return $this;
485
        }
486
487
        unset($this->consentScreenOptions[$option]);
488
489
        return $this;
490
    }
491
492
    public function hasScope(): bool
493
    {
494
        return $this->hasQueryParam('scope');
495
    }
496
497
    /**
498
     * @return string
499
     */
500
    public function getScope(): string
501
    {
502
        return $this->getQueryParam('scope');
503
    }
504
505
    public function getMetadata(): DataBag
506
    {
507
        return $this->metadata;
508
    }
509
}
510