1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Action; |
6
|
|
|
|
7
|
|
|
use function in_array; |
8
|
|
|
use Netgen\InformationCollection\API\Value\Event\InformationCollected; |
9
|
|
|
use Netgen\InformationCollection\API\Exception\ActionFailedException; |
10
|
|
|
use Netgen\InformationCollection\API\Priority; |
11
|
|
|
use Netgen\InformationCollection\API\Action\CrucialActionInterface; |
12
|
|
|
use Netgen\InformationCollection\API\Action\ActionInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use function usort; |
15
|
|
|
|
16
|
|
|
class ActionRegistry |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $actions; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Psr\Log\LoggerInterface |
30
|
|
|
*/ |
31
|
|
|
protected $logger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $debug = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* ActionAggregate constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $config |
42
|
|
|
* @param LoggerInterface $logger |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $config, LoggerInterface $logger) |
45
|
|
|
{ |
46
|
|
|
$this->config = $config; |
47
|
|
|
$this->actions = []; |
48
|
|
|
$this->logger = $logger; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Adds action to stack. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @param ActionInterface $action |
56
|
|
|
* @param int $priority |
57
|
|
|
*/ |
58
|
|
|
public function addAction(string $name, ActionInterface $action, int $priority = Priority::DEFAULT_PRIORITY): void |
59
|
|
|
{ |
60
|
|
|
$this->actions[] = [ |
61
|
|
|
'name' => $name, |
62
|
|
|
'action' => $action, |
63
|
|
|
'priority' => $priority, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function act(InformationCollected $event) |
68
|
|
|
{ |
69
|
|
|
$this->prepareActions(); |
70
|
|
|
$config = $this->prepareConfig($event->getContentType()->identifier); |
71
|
|
|
|
72
|
|
|
foreach ($this->actions as $action) { |
73
|
|
|
if ($this->canActionAct($action['name'], $config)) { |
74
|
|
|
try { |
75
|
|
|
$action['action']->act($event); |
76
|
|
|
} catch (ActionFailedException $e) { |
77
|
|
|
$this->logger |
78
|
|
|
->error($e->getMessage()); |
79
|
|
|
|
80
|
|
|
if ($this->debug) { |
81
|
|
|
throw $e; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($action['action'] instanceof CrucialActionInterface) { |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets debug variable based on kernel.debug param. |
94
|
|
|
* |
95
|
|
|
* @param bool $debug |
96
|
|
|
*/ |
97
|
|
|
public function setDebug($debug) |
98
|
|
|
{ |
99
|
|
|
$this->debug = $debug; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if given action can act. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @param array $config |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
protected function canActionAct($name, array $config) |
111
|
|
|
{ |
112
|
|
|
return in_array($name, $config, true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns configuration for given content type identifier if exists |
117
|
|
|
* or default one. |
118
|
|
|
* |
119
|
|
|
* @param string $contentTypeIdentifier |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function prepareConfig($contentTypeIdentifier) |
124
|
|
|
{ |
125
|
|
|
if (!empty($this->config['content_types'][$contentTypeIdentifier])) { |
126
|
|
|
return $this->config['content_types'][$contentTypeIdentifier]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!empty($this->config['default'])) { |
130
|
|
|
return $this->config['default']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sorts actions by priority. |
138
|
|
|
*/ |
139
|
|
|
protected function prepareActions() |
140
|
|
|
{ |
141
|
|
|
usort($this->actions, function ($one, $two) { |
142
|
|
|
if ($one['priority'] === $two['priority']) { |
143
|
|
|
return 0; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return ($one['priority'] > $two['priority']) ? -1 : 1; |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|