Completed
Push — master ( a78262...4e959e )
by Roni
11:36
created

EntityEventResolver::getSingleIdentity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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 Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
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 ContainerAwareInterface, EventResolverInterface
22
{
23
    use ContainerAwareTrait;
24
25
    protected $eventShortName;
26
27
    /** @var  $event DoctrineEntityEvent */
28
    protected $event;
29
30
    protected $entity;
31
32
    protected $eventName;
33
34
    protected $identity = ['', ''];
35
36
37
    /**
38
     * @param Event|DoctrineEntityEvent $event
39
     * @param $eventName
40
     *
41
     * @return array
42
     */
43
    public function getEventLogInfo(Event $event, $eventName)
44
    {
45
        if (!$event instanceof DoctrineEntityEvent) {
46
            return null;
47
        }
48
49
        $this->initialize($event, $eventName);
50
51
        if ($this->isUpdateEvent() && null === $this->getChangeSets($this->entity)) {
52
            return null;
53
        }
54
55
        $reflectionClass = $this->getReflectionClassFromObject($this->entity);
56
57
        return array(
58
            'description' => $this->getDescription($reflectionClass->getShortName()),
59
            'type'        => $this->getEventType($reflectionClass->getShortName())
60
        );
61
62
    }
63
64
    protected function getSingleIdentity()
65
    {
66
        foreach ($this->event->getIdentity() as $field => $value) {
67
            return [$field, $value];
68
        }
69
70
        return ['', ''];
71
72
    }
73
74
    /**
75
     * @param DoctrineEntityEvent $event
76
     * @param string $eventName
77
     */
78
    private function initialize(DoctrineEntityEvent $event, $eventName)
79
    {
80
        $this->eventShortName = null;
81
        $this->propertiesFound = array();
0 ignored issues
show
Bug introduced by
The property propertiesFound does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
        $this->eventName = $eventName;
83
        $this->event = $event;
84
        $this->entity = $event->getLifecycleEventArgs()->getEntity();
85
        $this->identity = $this->getSingleIdentity();
86
    }
87
88
    private function getIdField()
89
    {
90
        return $this->identity[0];
91
    }
92
93
    private function getIdValue()
94
    {
95
        return $this->identity[1];
96
    }
97
98
    protected function getChangeSets($entity)
99
    {
100
        return $this->isUpdateEvent() ? $this->getUnitOfWork()->getEntityChangeSet($entity) : null;
101
    }
102
103
    protected function isUpdateEvent()
104
    {
105
        return $this->getEventShortName() == 'updated';
106
    }
107
108
109
    /**
110
     * @param string $typeName
111
     * @return string
112
     */
113
    protected function getEventType($typeName)
114
    {
115
        return $typeName . " " . $this->getEventShortName();
116
    }
117
118
    /**
119
     * @param string $shortName
120
     * @return string
121
     */
122
    protected function getDescription($shortName)
123
    {
124
        return sprintf(
125
            '%s has been %s with %s = "%s"',
126
            $shortName,
127
            $this->getEventShortName(),
128
            $this->getIdField(),
129
            $this->getIdValue()
130
        );
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    protected function getEventShortName()
137
    {
138
        if (null === $this->eventShortName) {
139
            $this->eventShortName = DoctrineEvents::getShortEventType($this->getName());
140
        }
141
142
        return $this->eventShortName;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    protected function getName()
149
    {
150
        return $this->eventName;
151
    }
152
153
    /**
154
     * @param $object
155
     * @return \ReflectionClass
156
     */
157
    protected function getReflectionClassFromObject($object)
158
    {
159
        return new \ReflectionClass(get_class($object));
160
    }
161
162
    /**
163
     * @return \Doctrine\ORM\UnitOfWork
164
     */
165
    protected function getUnitOfWork()
166
    {
167
        return $this->container->get('doctrine')->getManager()->getUnitOfWork();
168
    }
169
}
170