Completed
Push — master ( 4fe654...d2edea )
by Daniel
04:44 queued 02:31
created

EventDispatchingAgent::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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
    /**
57
     * {@inheritdoc}
58
     */
59
    public function delete($object)
60
    {
61
        $this->dispatch(Events::PRE_DELETE, $object);
62
        $this->agent->delete($object);
63
        $this->dispatch(Events::POST_DELETE, $object);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getIdentifier($object)
70
    {
71
        return $this->agent->getIdentifier($object);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function supports(string $class): bool
78
    {
79
        return $this->agent->supports($class);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function setParent($object, $parent)
86
    {
87
        return $this->agent->setParent($object, $parent);
88
    }
89
90
    private function dispatch(string $eventName, $object)
91
    {
92
        $this->dispatcher->dispatch($eventName, new ObjectEvent($this->agent, $object));
93
    }
94
}
95