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])) { |
|
|
|
|
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])) { |
|
|
|
|
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
|
|
|
|
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.