Failed Conditions
Push — ng ( 625bbc...a06888 )
by Florent
08:03
created

RuleManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 6 1
A all() 0 4 1
A handle() 0 4 1
A callableForNextRule() 0 13 2
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\ClientRule;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\DataBag\DataBag;
18
19
class RuleManager
20
{
21
    /**
22
     * @var Rule[]
23
     */
24
    private $rules = [];
25
26
    /**
27
     * Appends new middleware for this message bus. Should only be used at configuration time.
28
     *
29
     * @param Rule $rule
30
     *
31
     * @return RuleManager
32
     */
33
    public function add(Rule $rule): self
34
    {
35
        $this->rules[] = $rule;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @return Rule[]
42
     */
43
    public function all(): array
44
    {
45
        return $this->rules;
46
    }
47
48
    /**
49
     * @param ClientId $clientId
50
     * @param DataBag  $commandParameters
51
     *
52
     * @return DataBag
53
     */
54
    public function handle(ClientId $clientId, DataBag $commandParameters): DataBag
55
    {
56
        return call_user_func($this->callableForNextRule(0), $clientId, $commandParameters, DataBag::create([]));
57
    }
58
59
    /**
60
     * @param int $index
61
     *
62
     * @return \Closure
63
     */
64
    private function callableForNextRule(int $index): \Closure
65
    {
66
        if (!isset($this->rules[$index])) {
67
            return function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag {
68
                return $validatedParameters;
69
            };
70
        }
71
        $rule = $this->rules[$index];
72
73
        return function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters) use ($rule, $index): DataBag {
74
            return $rule->handle($clientId, $commandParameters, $validatedParameters, $this->callableForNextRule($index + 1));
75
        };
76
    }
77
}
78