Failed Conditions
Push — ng ( f9780e...ccd5de )
by Florent
11:07
created

ScopeParameterChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 21 4
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\Scope;
15
16
use OAuth2Framework\Component\Server\AuthorizationEndpoint\Authorization;
17
use OAuth2Framework\Component\Server\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
18
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager;
19
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
20
21
final class ScopeParameterChecker implements ParameterChecker
22
{
23
    /**
24
     * @var ScopeRepository
25
     */
26
    private $scopeRepository;
27
28
    /**
29
     * @var null|ScopePolicyManager
30
     */
31
    private $scopePolicyManager;
32
33
    /**
34
     * ScopeParameterChecker constructor.
35
     *
36
     * @param ScopeRepository         $scopeRepository
37
     * @param null|ScopePolicyManager $scopePolicyManager
38
     */
39
    public function __construct(ScopeRepository $scopeRepository, ? ScopePolicyManager $scopePolicyManager)
40
    {
41
        $this->scopeRepository = $scopeRepository;
42
        $this->scopePolicyManager = $scopePolicyManager;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function process(Authorization $authorization, callable $next): Authorization
49
    {
50
        try {
51
            if ($authorization->hasQueryParam('scope')) {
52
                Assertion::regex($authorization->getQueryParam('scope'), '/^[\x20\x23-\x5B\x5D-\x7E]+$/', 'Invalid characters found in the \'scope\' parameter.');
53
                $scope = explode(' ', $authorization->getQueryParam('scope'));
54
            } else {
55
                $scope = [];
56
            }
57
            if (null !== $this->scopePolicyManager) {
58
                $scope = $this->scopePolicyManager->apply($scope, $authorization->getClient());
0 ignored issues
show
Documentation introduced by
$scope is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            }
60
            $availableScope = $this->scopeRepository->getAvailableScopesForClient($authorization->getClient());
61
            Assertion::true($this->scopeRepository->areRequestedScopesAvailable($scope, $availableScope), sprintf('An unsupported scope was requested. Available scopes for the client are %s.', implode(', ', $availableScope)));
0 ignored issues
show
Documentation introduced by
$scope is of type string|array, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
            $authorization = $authorization->withScopes($scope);
0 ignored issues
show
Bug introduced by
The method withScopes() does not seem to exist on object<OAuth2Framework\C...Endpoint\Authorization>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
64
            return $next($authorization);
65
        } catch (\InvalidArgumentException $e) {
66
            throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, $e->getMessage(), $authorization, $e);
67
        }
68
    }
69
}
70