1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Component\ObjectAgent; |
4
|
|
|
|
5
|
|
|
use Psi\Component\ObjectAgent\Event\ObjectEvent; |
6
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
|
9
|
|
|
class EventDispatchingAgent implements AgentInterface |
10
|
|
|
{ |
11
|
|
|
private $dispatcher; |
12
|
|
|
private $agent; |
13
|
|
|
|
14
|
|
|
public function __construct( |
15
|
|
|
AgentInterface $agent, |
16
|
|
|
EventDispatcherInterface $dispatcher |
17
|
|
|
) { |
18
|
|
|
$this->agent = $agent; |
19
|
|
|
$this->dispatcher = $dispatcher; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getCapabilities(): Capabilities |
26
|
|
|
{ |
27
|
|
|
return $this->agent->getCapabilities(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function find($identifier, string $class = null) |
34
|
|
|
{ |
35
|
|
|
return $this->agent->find($identifier, $class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function save($object) |
42
|
|
|
{ |
43
|
|
|
$this->dispatch(Events::PRE_SAVE, $object); |
44
|
|
|
$this->agent->save($object); |
45
|
|
|
$this->dispatch(Events::POST_SAVE, $object); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function query(Query $query): \Traversable |
52
|
|
|
{ |
53
|
|
|
return $this->agent->query($query); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function queryCount(Query $query): int |
57
|
|
|
{ |
58
|
|
|
return $this->agent->queryCount($query); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function delete($object) |
65
|
|
|
{ |
66
|
|
|
$this->dispatch(Events::PRE_DELETE, $object); |
67
|
|
|
$this->agent->delete($object); |
68
|
|
|
$this->dispatch(Events::POST_DELETE, $object); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getIdentifier($object) |
75
|
|
|
{ |
76
|
|
|
return $this->agent->getIdentifier($object); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function supports(string $class): bool |
83
|
|
|
{ |
84
|
|
|
return $this->agent->supports($class); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function setParent($object, $parent) |
91
|
|
|
{ |
92
|
|
|
return $this->agent->setParent($object, $parent); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function dispatch(string $eventName, $object) |
96
|
|
|
{ |
97
|
|
|
$this->dispatcher->dispatch($eventName, new ObjectEvent($this->agent, $object)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|