1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpDDD\DomainDrivenDesign\Event; |
4
|
|
|
|
5
|
|
|
use PhpDDD\DomainDrivenDesign\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
final class EventListener |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var bool |
11
|
|
|
*/ |
12
|
|
|
private $asynchronous; |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $eventClassName; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var callable|string |
20
|
|
|
*/ |
21
|
|
|
private $method; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param callable|string $method |
25
|
|
|
* @param string|null $eventClassName |
26
|
|
|
* @param bool $asynchronous |
27
|
|
|
*/ |
28
|
|
|
public function __construct($method, $eventClassName = null, $asynchronous = false) |
29
|
|
|
{ |
30
|
|
|
static::validateMethod($method); |
31
|
|
|
static::validateEventClassName($eventClassName); |
32
|
|
|
static::validateAsynchronous($asynchronous); |
33
|
|
|
$this->eventClassName = $eventClassName; |
34
|
|
|
$this->method = $method; |
35
|
|
|
$this->asynchronous = (bool) $asynchronous; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param bool $asynchronous |
40
|
|
|
*/ |
41
|
|
|
public static function validateAsynchronous($asynchronous) |
42
|
|
|
{ |
43
|
|
|
if (!is_bool($asynchronous)) { |
44
|
|
|
throw InvalidArgumentException::wrongType('asynchronous', $asynchronous, 'boolean'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string|null $eventClassName |
50
|
|
|
*/ |
51
|
|
|
public static function validateEventClassName($eventClassName) |
52
|
|
|
{ |
53
|
|
|
if (null !== $eventClassName && !is_string($eventClassName)) { |
54
|
|
|
throw InvalidArgumentException::wrongType('eventClassName', $eventClassName, 'null or sting'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string|callable $method |
60
|
|
|
*/ |
61
|
|
|
public static function validateMethod($method) |
62
|
|
|
{ |
63
|
|
|
if (!is_string($method) && !is_callable($method)) { |
64
|
|
|
throw InvalidArgumentException::wrongType('method', $method, 'callable or sting'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isAsynchronous() |
72
|
|
|
{ |
73
|
|
|
return $this->asynchronous; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param EventSubscriberInterface $subscriber |
78
|
|
|
*/ |
79
|
|
|
public function setSubscriber(EventSubscriberInterface $subscriber) |
80
|
|
|
{ |
81
|
|
|
if (!is_callable($this->method)) { |
82
|
|
|
$this->method = [$subscriber, $this->method]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getEventClassName() |
90
|
|
|
{ |
91
|
|
|
return $this->eventClassName; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return callable|string |
96
|
|
|
*/ |
97
|
|
|
public function getMethod() |
98
|
|
|
{ |
99
|
|
|
return $this->method; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|