Failed Conditions
Push — ng ( ada769...ebc492 )
by Florent
08:29 queued 40s
created

ProcessorManager::callableForNextRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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\TokenEndpoint\Processor;
15
16
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
17
use OAuth2Framework\Component\Server\TokenEndpoint\GrantTypeData;
18
use OAuth2Framework\Component\Server\TokenEndpoint\GrantType;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
final class ProcessorManager
22
{
23
    /**
24
     * @var callable
25
     */
26
    private $processors = [];
27
28
    /**
29
     * @param callable $processor
30
     *
31
     * @return ProcessorManager
32
     */
33
    public function add(callable $processor): self
34
    {
35
        $this->processors[] = $processor;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @param ServerRequestInterface $request
42
     * @param GrantTypeData          $grantTypeData
43
     * @param GrantType              $grantType
44
     *
45
     * @return GrantTypeData
46
     *
47
     * @throws OAuth2Exception
48
     */
49
    public function handle(ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData
50
    {
51
        $grantTypeData = call_user_func($this->resolve(0), $request, $grantTypeData, $grantType);
52
53
        return $grantType->grant($request, $grantTypeData);
54
    }
55
56
    /**
57
     * @param int $index
58
     *
59
     * @return callable
60
     */
61
    private function resolve(int $index): callable
62
    {
63
        if (!isset($this->processors[$index])) {
64
            return function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData {
0 ignored issues
show
Unused Code introduced by
The parameter $grantType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
                return $grantTypeData;
66
            };
67
        }
68
        $processor = $this->processors[$index];
69
70
        return function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType) use ($processor, $index): GrantTypeData {
71
            return $processor($request, $grantTypeData, $grantType, $this->resolve($index + 1));
72
        };
73
    }
74
}
75