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 findMany(array $identifiers, string $class = null) |
42
|
|
|
{ |
43
|
|
|
return $this->agent->findMany($identifiers, $class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function persist($object) |
50
|
|
|
{ |
51
|
|
|
$this->dispatch(Events::PRE_PERSIST, $object); |
52
|
|
|
$this->agent->persist($object); |
53
|
|
|
$this->dispatch(Events::POST_PERSIST, $object); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function query(Query $query): \Traversable |
60
|
|
|
{ |
61
|
|
|
return $this->agent->query($query); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function queryCount(Query $query): int |
68
|
|
|
{ |
69
|
|
|
return $this->agent->queryCount($query); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function remove($object) |
76
|
|
|
{ |
77
|
|
|
$this->dispatch(Events::PRE_REMOVE, $object); |
78
|
|
|
$this->agent->remove($object); |
79
|
|
|
$this->dispatch(Events::POST_REMOVE, $object); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function flush() |
86
|
|
|
{ |
87
|
|
|
$this->agent->flush(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getIdentifier($object) |
94
|
|
|
{ |
95
|
|
|
return $this->agent->getIdentifier($object); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function supports(string $class): bool |
102
|
|
|
{ |
103
|
|
|
return $this->agent->supports($class); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function setParent($object, $parent) |
110
|
|
|
{ |
111
|
|
|
return $this->agent->setParent($object, $parent); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function dispatch(string $eventName, $object) |
115
|
|
|
{ |
116
|
|
|
$this->dispatcher->dispatch($eventName, new ObjectEvent($this->agent, $object)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|