1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BlueRegister\Events; |
4
|
|
|
|
5
|
|
|
use BlueEvent\Event\Base\Interfaces\EventDispatcherInterface; |
6
|
|
|
|
7
|
|
|
class Event |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \BlueEvent\Event\Base\Interfaces\EventDispatcherInterface |
11
|
|
|
*/ |
12
|
|
|
protected $event; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var null|\BlueRegister\Log |
16
|
|
|
*/ |
17
|
|
|
protected $log; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $config = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $config |
26
|
|
|
* @param \BlueRegister\Log $log |
27
|
|
|
* @throws \LogicException |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
*/ |
30
|
8 |
|
public function __construct(array $config, $log) |
31
|
|
|
{ |
32
|
8 |
|
$this->log = $log; |
33
|
8 |
|
$this->config = $config; |
34
|
|
|
|
35
|
8 |
|
switch (true) { |
36
|
8 |
|
case $config['event_object'] instanceof EventDispatcherInterface: |
37
|
1 |
|
$this->event = $config['event_object']; |
38
|
1 |
|
break; |
39
|
|
|
|
40
|
7 |
|
case is_string($config['event_object']) && $this->classExists($config['event_object']): |
41
|
5 |
|
$this->event = new $config['event_object']($config['event_config']); |
42
|
|
|
|
43
|
5 |
|
if (!$this->event instanceof EventDispatcherInterface) { |
44
|
2 |
|
$message = 'Event should be instance of ' . EventDispatcherInterface::class . ': ' |
45
|
2 |
|
. get_class($this->event); |
46
|
2 |
|
$this->makeLog($message); |
47
|
2 |
|
throw new \LogicException($message); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
break; |
51
|
|
|
|
52
|
2 |
|
default: |
53
|
2 |
|
$message = 'Cannot create Event instance: ' . get_class($config['event_object']); |
54
|
2 |
|
$this->makeLog($message); |
55
|
2 |
|
throw new \LogicException($message); |
56
|
|
|
|
57
|
|
|
break; |
58
|
2 |
|
} |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* check that class exists and throw exception if not |
63
|
|
|
* |
64
|
|
|
* @param string $namespace |
65
|
|
|
* @return $this |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
*/ |
68
|
6 |
|
protected function classExists($namespace) |
69
|
|
|
{ |
70
|
6 |
|
if (!class_exists($namespace)) { |
71
|
1 |
|
throw new \InvalidArgumentException('Class don\'t exists: ' . $namespace); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|array $message |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
4 |
|
public function makeLog($message) |
82
|
|
|
{ |
83
|
4 |
|
if (!is_null($this->log)) { |
84
|
1 |
|
$this->log->makeLog($message); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
4 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $name |
92
|
|
|
* @param array $data |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
2 |
|
public function callEvent($name, array $data) |
96
|
|
|
{ |
97
|
2 |
|
if (!is_null($this->event)) { |
98
|
2 |
|
$this->event->triggerEvent($name, $data + [$this->config]); |
99
|
1 |
|
$this->makeLog('Triggered: ' . $name); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|