1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pvg\Application\Configuration\ValueObject; |
6
|
|
|
|
7
|
|
|
class EventListenerConfig |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
private $event; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
private $listenerServiceId; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $action; |
17
|
|
|
|
18
|
|
|
/** @var int */ |
19
|
|
|
private $priority; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @throws \ReflectionException |
23
|
|
|
* @throws \InvalidArgumentException |
24
|
|
|
*/ |
25
|
|
|
public function __construct( |
26
|
|
|
string $event, |
27
|
|
|
string $listenerServiceId, |
28
|
|
|
string $action, |
29
|
|
|
int $priority = 0 |
30
|
|
|
) { |
31
|
|
|
$this |
32
|
|
|
->setEvent($event) |
33
|
|
|
->setListenerServiceId($listenerServiceId) |
34
|
|
|
->setAction($action); |
35
|
|
|
$this->priority = $priority; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function event() : string |
39
|
|
|
{ |
40
|
|
|
return $this->event; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function listenerServiceId() : string |
44
|
|
|
{ |
45
|
|
|
return $this->listenerServiceId; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function action() : string |
49
|
|
|
{ |
50
|
|
|
return $this->action; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function priority() : int |
54
|
|
|
{ |
55
|
|
|
return $this->priority; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws \InvalidArgumentException |
60
|
|
|
*/ |
61
|
|
|
private function setEvent(string $event) : self |
62
|
|
|
{ |
63
|
|
|
if (empty($event)) { |
64
|
|
|
throw new \InvalidArgumentException('Event name cannot be empty'); |
65
|
|
|
} |
66
|
|
|
$this->event = $event; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws \InvalidArgumentException |
73
|
|
|
*/ |
74
|
|
|
private function setListenerServiceId(string $listenerServiceId) : self |
75
|
|
|
{ |
76
|
|
|
if (empty($listenerServiceId)) { |
77
|
|
|
throw new \InvalidArgumentException('Service ID (from service container) cannot be empty!'); |
78
|
|
|
} |
79
|
|
|
$this->listenerServiceId = $listenerServiceId; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws \InvalidArgumentException |
86
|
|
|
* @throws \ReflectionException |
87
|
|
|
*/ |
88
|
|
|
private function setAction(string $action) : self |
89
|
|
|
{ |
90
|
|
|
if (empty($action)) { |
91
|
|
|
throw new \InvalidArgumentException('Service listener method cannot be empty'); |
92
|
|
|
} |
93
|
|
|
$this->action = $action; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return static[] |
100
|
|
|
* |
101
|
|
|
* @throws \ReflectionException |
102
|
|
|
* @throws \InvalidArgumentException |
103
|
|
|
*/ |
104
|
|
|
public static function createFromConfigArray(array $configArray) : array |
105
|
|
|
{ |
106
|
|
|
$result = []; |
107
|
|
|
foreach ($configArray as $event => $listeners) { |
108
|
|
|
if (empty($listeners)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
foreach ($listeners as $listener) { |
112
|
|
|
$priority = $listener['priority'] ?? 0; |
113
|
|
|
$result[] = new static($event, $listener['service'], $listener['action'], $priority); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|