Passed
Branch develop (905d13)
by Mariano
09:27
created

PowerRoute   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 78.43%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 14
c 4
b 0
f 2
lcom 1
cbo 4
dl 0
loc 115
ccs 40
cts 51
cp 0.7843
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setConditionsMatcherFactory() 0 4 1
A setConfig() 0 4 1
A start() 0 6 1
A execute() 0 16 2
A evaluateConditions() 0 20 4
A getConditionsMatcher() 0 7 2
A getActionsToRun() 0 10 2
1
<?php
2
namespace Mcustiel\PowerRoute;
3
4
use Psr\Http\Message\ResponseInterface;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Mcustiel\PowerRoute\Common\Factories\MatcherFactory;
7
use Mcustiel\PowerRoute\Common\Factories\InputSourceFactory;
8
use Mcustiel\PowerRoute\Common\Factories\ActionFactory;
9
use Mcustiel\PowerRoute\Common\TransactionData;
10
use JMS\Serializer\Exception\RuntimeException;
11
use Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherInterface;
12
use Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory;
13
use Mcustiel\PowerRoute\Common\ConfigOptions;
14
15
class PowerRoute
16
{
17
    const CONDITIONS_MATCHER_ALL = 'allConditionsMatcher';
18
    const CONDITIONS_MATCHER_ONE = 'oneConditionsMatcher';
19
20
    /**
21
     * @var array $config
22
     */
23
    private $config;
24
    /**
25
     * @var \Mcustiel\PowerRoute\Common\Factories\ActionFactory $actionFactory
26
     */
27
    private $actionFactory;
28
    /**
29
     * @var \Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherInterface[] $conditionsMatchers
30
     */
31
    private $conditionMatchers;
32
    /**
33
     * @var \Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory $conditionMatcherFactory
34
     */
35
    private $conditionMatcherFactory;
36
37 3
    public function __construct(
38
        array $config,
39
        ActionFactory $actionFactory,
40
        ConditionsMatcherFactory $conditionsMatcherFactory
41
    ) {
42 3
        $this->conditionMatchers = [];
43 3
        $this->config = $config;
44 3
        $this->conditionMatcherFactory = $conditionsMatcherFactory;
45 3
        $this->actionFactory = $actionFactory;
46 3
    }
47
48
    public function setConditionsMatcherFactory(ConditionsMatcherFactory $factory)
49
    {
50
        $this->conditionMatchersFacotry = $factory;
0 ignored issues
show
Bug introduced by
The property conditionMatchersFacotry does not seem to exist. Did you mean conditionMatchers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
    }
52
53
    public function setConfig(array $config)
54
    {
55
        $this->config = $config;
56
    }
57
58
    /**
59
     * @param \Psr\Http\Message\ServerRequestInterface $request
60
     * @param \Psr\Http\Message\ResponseInterface      $response
61
     */
62
    public function start(ServerRequestInterface $request, ResponseInterface $response)
63
    {
64
        $transactionData = new TransactionData($request, $response);
65
        $this->execute($this->config[ConfigOptions::CONFIG_ROOT_NODE], $transactionData);
66
        return $transactionData->getResponse();
67
    }
68
69
    /**
70
     * @param string                             $routeName
71
     * @param \PowerRoute\Common\TransactionData $transactionData
72
     */
73 3
    public function execute($routeName, TransactionData $transactionData)
74
    {
75 3
        $route = $this->config[ConfigOptions::CONFIG_NODES][$routeName];
76
77 3
        $actions = $this->actionFactory->createFromConfig(
78 3
            $this->getActionsToRun(
79 3
                $route,
80 3
                $this->evaluateConditions($route, $transactionData->getRequest())
81 3
            ),
82
            $this
83 3
        );
84
85 3
        foreach ($actions as $action) {
86 3
            $action->execute($transactionData);
87 3
        }
88 3
    }
89
90 3
    private function evaluateConditions($route, $request)
91
    {
92 3
        if (!$route[ConfigOptions::CONFIG_NODE_CONDITION]) {
93 1
            return true;
94
        }
95 3
        if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL])) {
96 3
            return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ALL)->matches(
97 3
                $route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL],
98
                $request
99 3
            );
100
        }
101 2
        if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE])) {
102 2
            return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ONE)->matches(
103 2
                $route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE],
104
                $request
105 2
            );
106
        }
107
108
        throw new \RuntimeException('Invalid exception');
109
    }
110
111 3
    private function getConditionsMatcher($matcher)
112
    {
113 3
        if (!isset($this->conditionMatchers[$matcher])) {
114 3
            $this->conditionMatchers[$matcher] = $this->conditionMatcherFactory->get($matcher);
115 3
        }
116 3
        return $this->conditionMatchers[$matcher];
117
    }
118
119 3
    private function getActionsToRun($route, $matched)
120
    {
121 3
        if ($matched) {
122 3
            return $route[ConfigOptions::CONFIG_NODE_CONDITION_ACTIONS]
123 3
                [ConfigOptions::CONFIG_NODE_CONDITION_ACTIONS_MATCH];
124
        }
125
126 2
        return $route[ConfigOptions::CONFIG_NODE_CONDITION_ACTIONS]
127 2
            [ConfigOptions::CONFIG_NODE_CONDITION_ACTIONS_NOTMATCH];
128
    }
129
}
130