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