Failed Conditions
Push — ng ( ede6c5...efffe8 )
by Florent
11:50
created

DisplayParameterChecker::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 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\Server\AuthorizationEndpoint\ParameterChecker;
15
16
use OAuth2Framework\Component\Server\AuthorizationEndpoint\Authorization;
17
use OAuth2Framework\Component\Server\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
18
use OAuth2Framework\Component\Server\Core\Exception\OAuth2Exception;
19
20
final class DisplayParameterChecker implements ParameterChecker
21
{
22
    public const DISPLAY_PAGE = 'page';
23
24
    public const DISPLAY_POPUP = 'popup';
25
26
    public const DISPLAY_TOUCH = 'touch';
27
28
    public const DISPLAY_WAP = 'wap';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function check(Authorization $authorization): Authorization
34
    {
35
        try {
36
            if ($authorization->hasQueryParam('display') && !in_array($authorization->getQueryParam('display'), $this->getAllowedDisplayValues())) {
37
                throw new OAuth2AuthorizationException(400, OAuth2Exception::ERROR_INVALID_REQUEST, sprintf('Invalid parameter "display". Allowed values are %s', implode(', ', $this->getAllowedDisplayValues())), $authorization);
38
            }
39
40
            return $authorization;
41
        } catch (\InvalidArgumentException $e) {
42
            throw new OAuth2AuthorizationException(400, OAuth2Exception::ERROR_INVALID_REQUEST, $e->getMessage(), $authorization, $e);
43
        }
44
    }
45
46
    /**
47
     * @return string[]
48
     */
49
    private function getAllowedDisplayValues(): array
50
    {
51
        return [
52
            self::DISPLAY_PAGE,
53
            self::DISPLAY_POPUP,
54
            self::DISPLAY_TOUCH,
55
            self::DISPLAY_WAP,
56
        ];
57
    }
58
}
59