Completed
Push — master ( bce25e...d1b735 )
by Daniel
03:13
created

EventDispatchingAgent   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 79
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A find() 0 4 1
A save() 0 6 1
A query() 0 4 1
A delete() 0 6 1
A getIdentifier() 0 4 1
A supports() 0 4 1
A setParent() 0 4 1
A dispatch() 0 4 1
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