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