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

ScopeParameterChecker::check()   B

Complexity

Conditions 5
Paths 18

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 18
nop 1
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\Exception\OAuth2Exception;
20
21
final class ScopeParameterChecker implements ParameterChecker
22
{
23
    /**
24
     * @var ScopeRepository
25
     */
26
    private $scopeRepository;
27
28
    /**
29
     * @var ScopePolicyManager
30
     */
31
    private $scopePolicyManager;
32
33
    /**
34
     * ScopeParameterChecker constructor.
35
     *
36
     * @param ScopeRepository    $scopeRepository
37
     * @param 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 check(Authorization $authorization): Authorization
49
    {
50
        try {
51
            if ($authorization->hasQueryParam('scope')) {
52
                $requestedScope = $authorization->getQueryParam('scope');
53
                if (1 !== preg_match('/^[\x20\x23-\x5B\x5D-\x7E]+$/', $requestedScope)) {
54
                    throw new \InvalidArgumentException('Invalid characters found in the "scope" parameter.');
55
                }
56
            } else {
57
                $requestedScope = '';
58
            }
59
            $requestedScope = $this->scopePolicyManager->apply($requestedScope, $authorization->getClient());
60
            $scopes = explode(' ', $requestedScope);
61
62
            $availableScope = $this->scopeRepository->getAvailableScopesForClient($authorization->getClient());
0 ignored issues
show
Bug introduced by
The method getAvailableScopesForClient() does not seem to exist on object<OAuth2Framework\C...\Scope\ScopeRepository>.

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
            if (!$this->scopeRepository->areRequestedScopesAvailable($scopes, $availableScope)) {
0 ignored issues
show
Bug introduced by
The method areRequestedScopesAvailable() does not seem to exist on object<OAuth2Framework\C...\Scope\ScopeRepository>.

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...
64
                throw new \InvalidArgumentException(sprintf('An unsupported scope was requested. Available scopes for the client are %s.', implode(', ', $availableScope)));
65
            }
66
            $authorization = $authorization->withScopes($scope);
0 ignored issues
show
Bug introduced by
The variable $scope does not exist. Did you mean $scopes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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...
67
68
            return $authorization;
69
        } catch (\InvalidArgumentException $e) {
70
            throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, $e->getMessage(), $authorization, $e);
0 ignored issues
show
Unused Code introduced by
The call to OAuth2Exception::__construct() has too many arguments starting with $e.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
        }
72
    }
73
}
74