Passed
Pull Request — master (#44)
by Martin
04:50
created

DoctrineObjectEventResolver::getDoctrine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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\Util\ClassUtils;
15
use Symfony\Component\EventDispatcher\Event;
16
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
17
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;
18
19
/** Custom Event Resolver Example Class */
20
class DoctrineObjectEventResolver implements EventResolverInterface
21
{
22
    protected $eventShortName;
23
24
    /** @var $event DoctrineObjectEvent */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $event at position 0 could not be parsed: Unknown type name '$event' at position 0 in $event.
Loading history...
25
    protected $event;
26
27
    protected $entity;
28
29
    protected $eventName;
30
31
    protected $identity = ['', ''];
32
33
    /**
34
     * @var \Doctrine\ORM\EntityManager
35
     */
36
    protected $manager;
37
38
    protected $changeSetGetterMethods = [
39
        'getEntityChangeSet',
40
        'getDocumentChangeSet',
41
    ];
42
43
    /**
44
     * @param Event|DoctrineObjectEvent $event
45
     * @param $eventName
46
     *
47
     * @return array
48
     *
49
     * @throws \ReflectionException
50
     */
51
    public function getEventLogInfo(Event $event, $eventName)
52
    {
53
        if (!$event instanceof DoctrineObjectEvent) {
54
            return null;
55
        }
56
57
        $this->initialize($event, $eventName);
58
59
        if ($this->isUpdateEvent() && null === $this->getChangeSets($this->entity)) {
60
            return null;
61
        }
62
63
        $reflectionClass = $this->getReflectionClassFromObject($this->entity);
64
65
        return array(
66
            'description' => $this->getDescription($reflectionClass->getShortName()),
67
            'type' => $this->getEventType($reflectionClass->getShortName()),
68
        );
69
    }
70
71
    protected function getSingleIdentity()
72
    {
73
        foreach ($this->event->getIdentity() as $field => $value) {
74
            return [$field, $value];
75
        }
76
77
        return ['', ''];
78
    }
79
80
    /**
81
     * @param DoctrineObjectEvent $event
82
     * @param string              $eventName
83
     */
84
    private function initialize(DoctrineObjectEvent $event, $eventName)
85
    {
86
        $this->eventShortName = null;
87
        $this->eventName = $eventName;
88
        $this->event = $event;
89
        $this->entity = $event->getLifecycleEventArgs()->getObject();
90
        $this->manager = $event->getLifecycleEventArgs()->getObjectManager();
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
0 ignored issues
show
Bug introduced by
The type Doctrine\ODM\MongoDB\UnitOfWork was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
182
     */
183
    protected function getUnitOfWork()
184
    {
185
        return $this->manager->getUnitOfWork();
186
    }
187
}
188