Completed
Push — master ( 23df4e...777761 )
by Roni
19:44
created

DoctrineSubscriber::isConfigured()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Subscriber;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\ORM\Event\LifecycleEventArgs;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Xiidea\EasyAuditBundle\Events\DoctrineEntityEvent;
19
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
20
21
class DoctrineSubscriber implements ContainerAwareInterface, EventSubscriber
22
{
23
    use ContainerAwareTrait;
24
25
    /** @var \Doctrine\Common\Annotations\Reader */
26
    private $annotationReader;
27
28
    /**
29
     * @var array
30
     */
31
    private $entities;
32
33
    public function __construct($entities = array())
34
    {
35
        $this->entities = $entities;
36
    }
37
38
    public function getSubscribedEvents()
39
    {
40
        return array(
41
            'postPersist',
42
            'postUpdate',
43
            'preRemove',
44
        );
45
    }
46
47
    public function postPersist(LifecycleEventArgs $args)
48
    {
49
        $this->handleEvent(DoctrineEvents::ENTITY_CREATED, $args);
50
    }
51
52
    public function postUpdate(LifecycleEventArgs $args)
53
    {
54
        $this->handleEvent(DoctrineEvents::ENTITY_UPDATED, $args);
55
    }
56
57
    public function preRemove(LifecycleEventArgs $args)
58
    {
59
        $this->handleEvent(DoctrineEvents::ENTITY_DELETED, $args);
60
    }
61
62
    /**
63
     * @param string $eventName
64
     * @param LifecycleEventArgs $args
65
     */
66
    private function handleEvent($eventName, LifecycleEventArgs $args)
67
    {
68
        if (true === $this->isConfiguredToTrack($args->getEntity(), $eventName)) {
69
            $this->container->get('event_dispatcher')->dispatch($eventName,
70
                new DoctrineEntityEvent($args)
71
            );
72
        }
73
    }
74
75
    /**
76
     * @param $entity
77
     * @param string $eventName
78
     * @return bool
79
     */
80
    private function isConfiguredToTrack($entity, $eventName = '')
81
    {
82
        $class = get_class($entity);
83
        $eventType = DoctrineEvents::getShortEventType($eventName);
84
85
        if (null !== $track = $this->isAnnotatedEvent($entity, $eventType)) {
86
            return $track;
87
        }
88
89
        if (!$this->isConfigured($class)) {
90
            return FALSE;
91
        }
92
93
        if ($this->shouldTrackAllEventType($class)) {
94
            return TRUE;
95
        }
96
97
        return $this->shouldTrackEventType($eventType, $class);
98
    }
99
100
    /**
101
     * @param $entity
102
     * @param string $eventType
103
     * @return bool|null
104
     */
105
    protected function isAnnotatedEvent($entity, $eventType)
106
    {
107
        $metaData = $this->hasAnnotation($entity);
108
109
        if (!$metaData) {
110
            return null;
111
        }
112
113
        return empty($metaData->events) || in_array($eventType, $metaData->events);
114
    }
115
116
    /**
117
     * @param $entity
118
     * @return null|object
119
     */
120
    protected function hasAnnotation($entity)
121
    {
122
        $reflection = $this->getReflectionClassFromObject($entity);
123
124
        return $this
125
            ->getAnnotationReader()
126
            ->getClassAnnotation($reflection, 'Xiidea\EasyAuditBundle\Annotation\ORMSubscribedEvents');
127
128
    }
129
130
    /**
131
     * @return \Doctrine\Common\Annotations\Reader
132
     */
133
    protected function getAnnotationReader()
134
    {
135
        return $this->annotationReader;
136
    }
137
138
    /**
139
     * @param $object
140
     * @return \ReflectionClass
141
     */
142
    protected function getReflectionClassFromObject($object)
143
    {
144
        $class = get_class($object);
145
146
        return new \ReflectionClass($class);
147
    }
148
149
    /**
150
     * @param string $eventType
151
     * @param string $class
152
     * @return bool
153
     */
154
    private function shouldTrackEventType($eventType, $class)
155
    {
156
        return (is_array($this->entities[$class]) && in_array($eventType, $this->entities[$class]));
157
    }
158
159
    /**
160
     * @param string $class
161
     * @return bool
162
     */
163
    private function shouldTrackAllEventType($class)
164
    {
165
        return empty($this->entities[$class]);
166
    }
167
168
    /**
169
     * @param string $class
170
     * @return bool
171
     */
172
    protected function isConfigured($class)
173
    {
174
        return isset($this->entities[$class]);
175
    }
176
177
    /**
178
     * @param \Doctrine\Common\Annotations\Reader $annotationReader
179
     */
180
    public function setAnnotationReader($annotationReader = null)
181
    {
182
        $this->annotationReader = $annotationReader;
183
    }
184
}
185