Failed Conditions
Pull Request — master (#22)
by Florent
08:54
created

Scope   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkScopeUsedOnce() 0 6 2
A checkScopeCharset() 0 6 2
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2017 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace OAuth2Framework\Component\Server\Util;
13
14
use OAuth2Framework\Component\Server\Response\OAuth2Exception;
15
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager;
16
17
final class Scope
18
{
19
    /**
20
     * @param string $scope
21
     * @param array  $scopes
22
     *
23
     * @throws OAuth2Exception
24
     */
25
    public static function checkScopeUsedOnce(string $scope, array $scopes)
26
    {
27
        if (1 < count(array_keys($scopes, $scope))) {
28
            throw new OAuth2Exception(400, ['error' => OAuth2ResponseFactoryManager::ERROR_INVALID_SCOPE, 'error_description' => sprintf('Scope \'%s\' appears more than once.', $scope)]);
29
        }
30
    }
31
32
    /**
33
     * @param string $scope
34
     *
35
     * @throws OAuth2Exception
36
     */
37
    public static function checkScopeCharset(string $scope)
38
    {
39
        if (1 !== preg_match('/^[\x20\x23-\x5B\x5D-\x7E]+$/', $scope)) {
40
            throw new OAuth2Exception(400, ['error' => OAuth2ResponseFactoryManager::ERROR_INVALID_SCOPE, 'error_description' => 'Scope contains illegal characters.']);
41
        }
42
    }
43
}
44