1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\ObjectAgent; |
6
|
|
|
|
7
|
|
|
use Psi\Component\ObjectAgent\Event\ObjectEvent; |
8
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
10
|
|
|
|
11
|
|
|
class EventDispatchingAgent implements AgentInterface |
12
|
|
|
{ |
13
|
|
|
private $dispatcher; |
14
|
|
|
private $agent; |
15
|
|
|
|
16
|
|
|
public function __construct( |
17
|
|
|
AgentInterface $agent, |
18
|
|
|
EventDispatcherInterface $dispatcher |
19
|
|
|
) { |
20
|
|
|
$this->agent = $agent; |
21
|
|
|
$this->dispatcher = $dispatcher; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function getCapabilities(): Capabilities |
28
|
|
|
{ |
29
|
|
|
return $this->agent->getCapabilities(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function find($identifier, string $class = null) |
36
|
|
|
{ |
37
|
|
|
return $this->agent->find($identifier, $class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function findMany(array $identifiers, string $class = null) |
44
|
|
|
{ |
45
|
|
|
return $this->agent->findMany($identifiers, $class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function persist($object) |
52
|
|
|
{ |
53
|
|
|
$this->dispatch(Events::PRE_PERSIST, $object); |
54
|
|
|
$this->agent->persist($object); |
55
|
|
|
$this->dispatch(Events::POST_PERSIST, $object); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function query(Query $query): \Traversable |
62
|
|
|
{ |
63
|
|
|
return $this->agent->query($query); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function queryCount(Query $query): int |
70
|
|
|
{ |
71
|
|
|
return $this->agent->queryCount($query); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function remove($object) |
78
|
|
|
{ |
79
|
|
|
$this->dispatch(Events::PRE_REMOVE, $object); |
80
|
|
|
$this->agent->remove($object); |
81
|
|
|
$this->dispatch(Events::POST_REMOVE, $object); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function flush() |
88
|
|
|
{ |
89
|
|
|
$this->agent->flush(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function getIdentifier($object) |
96
|
|
|
{ |
97
|
|
|
return $this->agent->getIdentifier($object); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function supports(string $class): bool |
104
|
|
|
{ |
105
|
|
|
return $this->agent->supports($class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function setParent($object, $parent) |
112
|
|
|
{ |
113
|
|
|
return $this->agent->setParent($object, $parent); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getCanonicalClassFqn(string $classFqn): string |
120
|
|
|
{ |
121
|
|
|
return $this->agent->getCanonicalClassFqn($classFqn); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function dispatch(string $eventName, $object) |
125
|
|
|
{ |
126
|
|
|
$this->dispatcher->dispatch($eventName, new ObjectEvent($this->agent, $object)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|