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\Action\ActionInterface; |
9
|
|
|
use Netgen\InformationCollection\API\Action\CrucialActionInterface; |
10
|
|
|
use Netgen\InformationCollection\API\Exception\ActionFailedException; |
11
|
|
|
use Netgen\InformationCollection\API\Priority; |
12
|
|
|
use Netgen\InformationCollection\API\Value\Event\InformationCollected; |
13
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use function usort; |
16
|
|
|
|
17
|
|
|
class ActionRegistry implements ActionInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface |
21
|
|
|
*/ |
22
|
|
|
protected $configResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $actions; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Psr\Log\LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
protected $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $debug = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ActionAggregate constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $config |
|
|
|
|
43
|
|
|
* @param LoggerInterface $logger |
44
|
|
|
*/ |
45
|
|
|
public function __construct(iterable $actions, ConfigResolverInterface $configResolver, LoggerInterface $logger) |
46
|
|
|
{ |
47
|
|
|
$this->actions = $actions; |
48
|
|
|
$this->configResolver = $configResolver; |
49
|
|
|
$this->config = $configResolver->getParameter('actions', 'netgen_information_collection'); |
|
|
|
|
50
|
|
|
$this->logger = $logger; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function act(InformationCollected $event): void |
54
|
|
|
{ |
55
|
|
|
$config = $this->prepareConfig($event->getContentType()->identifier); |
56
|
|
|
|
57
|
|
|
foreach ($this->actions as $action) { |
58
|
|
|
if ($this->canActionAct($action, $config)) { |
59
|
|
|
try { |
60
|
|
|
$action->act($event); |
61
|
|
|
} catch (ActionFailedException $e) { |
62
|
|
|
$this->logger |
63
|
|
|
->error($e->getMessage()); |
64
|
|
|
|
65
|
|
|
if ($this->debug) { |
66
|
|
|
throw $e; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($action instanceof CrucialActionInterface) { |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets debug variable based on kernel.debug param. |
79
|
|
|
* |
80
|
|
|
* @param bool $debug |
81
|
|
|
*/ |
82
|
|
|
public function setDebug($debug) |
83
|
|
|
{ |
84
|
|
|
$this->debug = $debug; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check if given action can act. |
89
|
|
|
* |
90
|
|
|
* @param ActionInterface $action |
91
|
|
|
* @param array $config |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
protected function canActionAct(ActionInterface $action, array $config): bool |
96
|
|
|
{ |
97
|
|
|
return in_array(get_class($action), $config, true); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns configuration for given content type identifier if exists |
102
|
|
|
* or default one. |
103
|
|
|
* |
104
|
|
|
* @param string $contentTypeIdentifier |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function prepareConfig($contentTypeIdentifier): array |
109
|
|
|
{ |
110
|
|
|
if (!empty($this->config['content_types'][$contentTypeIdentifier])) { |
|
|
|
|
111
|
|
|
return $this->config['content_types'][$contentTypeIdentifier]; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!empty($this->config['default'])) { |
|
|
|
|
115
|
|
|
return $this->config['default']; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return []; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.