Failed Conditions
Push — ng ( 935f22...b3431d )
by Florent
04:01
created

ScopeRule::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 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\Rule;
15
16
use OAuth2Framework\Component\Server\Core\Client\ClientId;
17
use OAuth2Framework\Component\Server\Core\Client\Rule\Rule;
18
use OAuth2Framework\Component\Server\Core\DataBag\DataBag;
19
20
final class ScopeRule implements Rule
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, callable $next): DataBag
26
    {
27
        if ($commandParameters->has('scope')) {
28
            $scope = $commandParameters->get('scope');
29
            if (!is_string($scope)) {
30
                throw new \InvalidArgumentException('The "scope" parameter must be a string.');
31
            }
32
            if (1 !== preg_match('/^[\x20\x23-\x5B\x5D-\x7E]+$/', $scope)) {
33
                throw new \InvalidArgumentException('Invalid characters found in the "scope" parameter.');
34
            }
35
            $validatedParameters = $validatedParameters->with('scope', $scope);
36
        }
37
38
        return $next($clientId, $commandParameters, $validatedParameters);
39
    }
40
}
41