Completed
Push — master ( e9e666...e79deb )
by Roni
01:22
created

EntityEventResolver::setDoctrine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\Resolver;
13
14
use Doctrine\Bundle\DoctrineBundle\Registry;
15
use Doctrine\Common\Util\ClassUtils;
16
use Symfony\Component\EventDispatcher\Event;
17
use Xiidea\EasyAuditBundle\Events\DoctrineEntityEvent;
18
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
19
20
/** Custom Event Resolver Example Class */
21
class EntityEventResolver implements EventResolverInterface
22
{
23
    protected $eventShortName;
24
25
    /** @var  $event DoctrineEntityEvent */
26
    protected $event;
27
28
    protected $entity;
29
30
    protected $eventName;
31
32
    protected $identity = ['', ''];
33
34
    /**
35
     * @var Registry
36
     */
37
    protected $doctrine;
38
39
40
    /**
41
     * @param Event|DoctrineEntityEvent $event
42
     * @param $eventName
43
     *
44
     * @return array
45
     * @throws \ReflectionException
46
     */
47
    public function getEventLogInfo(Event $event, $eventName)
48
    {
49
        if (!$event instanceof DoctrineEntityEvent) {
50
            return null;
51
        }
52
53
        $this->initialize($event, $eventName);
54
55
        if ($this->isUpdateEvent() && null === $this->getChangeSets($this->entity)) {
56
            return null;
57
        }
58
59
        $reflectionClass = $this->getReflectionClassFromObject($this->entity);
60
61
        return array(
62
            'description' => $this->getDescription($reflectionClass->getShortName()),
63
            'type'        => $this->getEventType($reflectionClass->getShortName())
64
        );
65
66
    }
67
68
    protected function getSingleIdentity()
69
    {
70
        foreach ($this->event->getIdentity() as $field => $value) {
71
            return [$field, $value];
72
        }
73
74
        return ['', ''];
75
76
    }
77
78
    /**
79
     * @param DoctrineEntityEvent $event
80
     * @param string $eventName
81
     */
82
    private function initialize(DoctrineEntityEvent $event, $eventName)
83
    {
84
        $this->eventShortName = null;
85
        $this->eventName = $eventName;
86
        $this->event = $event;
87
        $this->entity = $event->getLifecycleEventArgs()->getEntity();
88
        $this->identity = $this->getSingleIdentity();
89
    }
90
91
    private function getIdField()
92
    {
93
        return $this->identity[0];
94
    }
95
96
    private function getIdValue()
97
    {
98
        return $this->identity[1];
99
    }
100
101
    protected function getChangeSets($entity)
102
    {
103
        return $this->isUpdateEvent() ? $this->getUnitOfWork()->getEntityChangeSet($entity) : null;
104
    }
105
106
    protected function isUpdateEvent()
107
    {
108
        return $this->getEventShortName() == 'updated';
109
    }
110
111
112
    /**
113
     * @param string $typeName
114
     * @return string
115
     */
116
    protected function getEventType($typeName)
117
    {
118
        return $typeName . " " . $this->getEventShortName();
119
    }
120
121
    /**
122
     * @param string $shortName
123
     * @return string
124
     */
125
    protected function getDescription($shortName)
126
    {
127
        return sprintf(
128
            '%s has been %s with %s = "%s"',
129
            $shortName,
130
            $this->getEventShortName(),
131
            $this->getIdField(),
132
            $this->getIdValue()
133
        );
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    protected function getEventShortName()
140
    {
141
        if (null === $this->eventShortName) {
142
            $this->eventShortName = DoctrineEvents::getShortEventType($this->getName());
143
        }
144
145
        return $this->eventShortName;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    protected function getName()
152
    {
153
        return $this->eventName;
154
    }
155
156
    /**
157
     * @param $object
158
     * @return \ReflectionClass
159
     * @throws \ReflectionException
160
     */
161
    protected function getReflectionClassFromObject($object)
162
    {
163
        return new \ReflectionClass(ClassUtils::getClass($object));
164
    }
165
166
    /**
167
     * @return \Doctrine\ORM\UnitOfWork
168
     */
169
    protected function getUnitOfWork()
170
    {
171
        return $this->getDoctrine()->getManager()->getUnitOfWork();
172
    }
173
174
    /**
175
     * @return \Doctrine\Bundle\DoctrineBundle\Registry|object
176
     */
177
    protected function getDoctrine()
178
    {
179
        return $this->doctrine;
180
    }
181
182
    /**
183
     * @param Registry $doctrine
184
     */
185
    public function setDoctrine($doctrine)
186
    {
187
        $this->doctrine = $doctrine;
188
    }
189
}
190