1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GpsLab component. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace GpsLab\Domain\Event\Aggregator; |
10
|
|
|
|
11
|
|
|
use GpsLab\Domain\Event\EventInterface; |
12
|
|
|
use GpsLab\Domain\Event\NameResolver\EventClassLastPartResolver; |
13
|
|
|
use GpsLab\Domain\Event\NameResolver\EventNameResolverInterface; |
14
|
|
|
|
15
|
|
|
trait AggregateEventsRaiseInSelfTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EventInterface[] |
19
|
|
|
*/ |
20
|
|
|
private $events = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EventNameResolverInterface |
24
|
|
|
*/ |
25
|
|
|
private $resolver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param EventNameResolverInterface $resolver |
29
|
|
|
*/ |
30
|
1 |
|
protected function changeEventNameResolver(EventNameResolverInterface $resolver) |
31
|
|
|
{ |
32
|
1 |
|
$this->resolver = $resolver; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return EventNameResolverInterface |
37
|
|
|
*/ |
38
|
3 |
|
private function getEventNameResolver() |
39
|
|
|
{ |
40
|
3 |
|
if (!($this->resolver instanceof EventNameResolverInterface)) { |
41
|
2 |
|
$this->resolver = new EventClassLastPartResolver(); // default name resolver |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
return $this->resolver; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param EventInterface $event |
49
|
|
|
*/ |
50
|
3 |
|
private function raiseInSelf(EventInterface $event) |
51
|
|
|
{ |
52
|
3 |
|
$event_name = $this->getEventNameResolver()->getEventName($event); |
53
|
3 |
|
$method = sprintf('on%s', $event_name); |
54
|
|
|
|
55
|
|
|
// if method is not exists is not a critical error |
56
|
3 |
|
if (method_exists($this, $method)) { |
57
|
2 |
|
call_user_func([$this, $method], $event); |
58
|
|
|
} |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param EventInterface $event |
63
|
|
|
*/ |
64
|
3 |
|
protected function raise(EventInterface $event) |
65
|
|
|
{ |
66
|
3 |
|
$this->events[] = $event; |
67
|
3 |
|
$this->raiseInSelf($event); |
68
|
3 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return EventInterface[] |
72
|
|
|
*/ |
73
|
2 |
|
public function pullEvents() |
74
|
|
|
{ |
75
|
2 |
|
$events = $this->events; |
76
|
2 |
|
$this->events = []; |
77
|
|
|
|
78
|
2 |
|
return $events; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|