Completed
Push — master ( e75413...39c194 )
by Roni
14:44 queued 13:17
created

DoctrineObjectEventResolver::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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