Completed
Push — master ( c364de...75f5a5 )
by Mariano
03:44
created

PowerRoute   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.31%

Importance

Changes 0
Metric Value
dl 12
loc 120
c 0
b 0
f 0
wmc 14
lcom 1
cbo 5
ccs 43
cts 51
cp 0.8431
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A setConfig() 0 4 1
A start() 0 7 1
A execute() 0 21 3
A evaluateConditions() 12 20 4
A getConditionsMatcher() 0 8 2
A getActionsToRun() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mcustiel\PowerRoute;
4
5
use Mcustiel\Creature\LazyCreator;
6
use Mcustiel\PowerRoute\Actions\Psr7MiddlewareAction;
7
use Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory;
8
use Mcustiel\PowerRoute\Common\ConfigOptions;
9
use Mcustiel\PowerRoute\Common\Factories\ActionFactory;
10
use Mcustiel\PowerRoute\Common\TransactionData;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
class PowerRoute
15
{
16
    const CONDITIONS_MATCHER_ALL = 'allConditionsMatcher';
17
    const CONDITIONS_MATCHER_ONE = 'oneConditionsMatcher';
18
19
    /**
20
     * @var array
21
     */
22
    private $config;
23
    /**
24
     * @var \Mcustiel\PowerRoute\Common\Factories\ActionFactory
25
     */
26
    private $actionFactory;
27
    /**
28
     * @var \Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherInterface[]
29
     */
30
    private $conditionMatchers;
31
    /**
32
     * @var \Mcustiel\PowerRoute\Common\Conditions\ConditionsMatcherFactory
33
     */
34
    private $conditionMatcherFactory;
35
    /**
36
     * @var \Mcustiel\Creature\LazyCreator
37
     */
38
    private $psr7InvokerCreator;
39
40 3
    public function __construct(
41
        array $config,
42
        ActionFactory $actionFactory,
43
        ConditionsMatcherFactory $conditionsMatcherFactory
44
    ) {
45 3
        $this->conditionMatchers = [];
46 3
        $this->config = $config;
47 3
        $this->conditionMatcherFactory = $conditionsMatcherFactory;
48 3
        $this->actionFactory = $actionFactory;
49 3
        $this->psr7InvokerCreator = new LazyCreator(Psr7MiddlewareAction::class);
50 3
    }
51
52
    public function setConfig(array $config)
53
    {
54
        $this->config = $config;
55
    }
56
57
    /**
58
     * @param \Psr\Http\Message\ServerRequestInterface $request
59
     * @param \Psr\Http\Message\ResponseInterface      $response
60
     */
61
    public function start(ServerRequestInterface $request, ResponseInterface $response)
62
    {
63
        $transactionData = new TransactionData($request, $response);
64
        $this->execute($this->config[ConfigOptions::CONFIG_ROOT_NODE], $transactionData);
65
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
            $instance = $action->getInstance();
87 3
            if (is_callable($instance)) {
88 1
                $this->psr7InvokerCreator->getInstance()->execute($transactionData, $action);
89 1
            } else {
90 3
                $instance->execute($transactionData, $action->getArgument());
91
            }
92 3
        }
93 3
    }
94
95 3
    private function evaluateConditions($route, $request)
96
    {
97 3
        if (!$route[ConfigOptions::CONFIG_NODE_CONDITION]) {
98 1
            return true;
99
        }
100 3 View Code Duplication
        if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101 3
            return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ALL)->matches(
102 3
                $route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ALL],
103
                $request
104 3
            );
105
        }
106 2 View Code Duplication
        if (isset($route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107 2
            return $this->getConditionsMatcher(self::CONDITIONS_MATCHER_ONE)->matches(
108 2
                $route[ConfigOptions::CONFIG_NODE_CONDITION][ConfigOptions::CONFIG_NODE_CONDITION_ONE],
109
                $request
110 2
            );
111
        }
112
113
        throw new \RuntimeException('Invalid condition specified for route: ' . $route);
114
    }
115
116 3
    private function getConditionsMatcher($matcher)
117
    {
118 3
        if (!isset($this->conditionMatchers[$matcher])) {
119 3
            $this->conditionMatchers[$matcher] = $this->conditionMatcherFactory->get($matcher);
120 3
        }
121
122 3
        return $this->conditionMatchers[$matcher];
123
    }
124
125 3
    private function getActionsToRun($route, $matched)
126
    {
127 3
        if ($matched) {
128 3
            return $route[ConfigOptions::CONFIG_NODE_ACTIONS][ConfigOptions::CONFIG_NODE_ACTIONS_MATCH];
129
        }
130
131 2
        return $route[ConfigOptions::CONFIG_NODE_ACTIONS][ConfigOptions::CONFIG_NODE_ACTIONS_NOTMATCH];
132
    }
133
}
134