Completed
Push — master ( e8e4b5...2e598e )
by Daniel
8s
created

EventDispatchingAgent   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCapabilities() 0 4 1
A find() 0 4 1
A findMany() 0 4 1
A persist() 0 6 1
A query() 0 4 1
A queryCount() 0 4 1
A remove() 0 6 1
A flush() 0 4 1
A getIdentifier() 0 4 1
A supports() 0 4 1
A setParent() 0 4 1
A getCanonicalClassFqn() 0 4 1
A dispatch() 0 4 1
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