22
33namespace W3C \LifecycleEventsBundle \EventListener ;
44
5- use Doctrine \Common \Annotations \Reader ;
65use Doctrine \Common \Util \ClassUtils ;
76use Doctrine \ORM \Event \PreUpdateEventArgs ;
8- use Doctrine \ORM \Mapping \ClassMetadata ;
97use Doctrine \ORM \PersistentCollection ;
108use W3C \LifecycleEventsBundle \Annotation \Change ;
9+ use W3C \LifecycleEventsBundle \Services \AnnotationGetter ;
1110use W3C \LifecycleEventsBundle \Services \LifecycleEventsDispatcher ;
1211
1312/**
@@ -25,20 +24,20 @@ class LifecyclePropertyEventsListener
2524 private $ dispatcher ;
2625
2726 /**
28- * @var Reader
27+ * @var AnnotationGetter
2928 */
30- private $ reader ;
29+ private $ annotationGetter ;
3130
3231 /**
3332 * Constructs a new instance
3433 *
35- * @param LifecycleEventsDispatcher $dispatcher the dispatcher to fed
36- * @param Reader $reader
34+ * @param LifecycleEventsDispatcher $dispatcher the dispatcher to feed
35+ * @param AnnotationGetter $annotationGetter
3736 */
38- public function __construct (LifecycleEventsDispatcher $ dispatcher , Reader $ reader )
37+ public function __construct (LifecycleEventsDispatcher $ dispatcher , AnnotationGetter $ annotationGetter )
3938 {
40- $ this ->dispatcher = $ dispatcher ;
41- $ this ->reader = $ reader ;
39+ $ this ->dispatcher = $ dispatcher ;
40+ $ this ->annotationGetter = $ annotationGetter ;
4241 }
4342
4443 public function preUpdate (PreUpdateEventArgs $ args )
@@ -57,7 +56,8 @@ private function addPropertyChanges(PreUpdateEventArgs $args)
5756 $ classMetadata = $ args ->getEntityManager ()->getClassMetadata ($ realClass );
5857
5958 foreach ($ args ->getEntityChangeSet () as $ property => $ change ) {
60- $ annotation = $ this ->getChangeAnnotation ($ classMetadata , $ property );
59+ /** @var Change $annotation */
60+ $ annotation = $ this ->annotationGetter ->getPropertyAnnotation ($ classMetadata , $ property , Change::class);
6161
6262 if ($ annotation ) {
6363 $ this ->dispatcher ->addPropertyChange (
@@ -89,7 +89,8 @@ private function addCollectionChanges(PreUpdateEventArgs $args)
8989 }
9090
9191 $ property = $ update ->getMapping ()['fieldName ' ];
92- $ annotation = $ this ->getChangeAnnotation ($ classMetadata , $ property );
92+ /** @var Change $annotation */
93+ $ annotation = $ this ->annotationGetter ->getPropertyAnnotation ($ classMetadata , $ property , Change::class);
9394
9495 // Make sure $u belongs to the entity we are working on
9596 if (!isset ($ annotation )) {
@@ -105,29 +106,4 @@ private function addCollectionChanges(PreUpdateEventArgs $args)
105106 );
106107 }
107108 }
108-
109- /**
110- * @param ClassMetadata $classMetadata
111- * @param string $property
112- *
113- * @return Change
114- * @throws \ReflectionException
115- */
116- private function getChangeAnnotation (ClassMetadata $ classMetadata , $ property )
117- {
118- $ reflProperty = $ classMetadata ->getReflectionProperty ($ property );
119-
120- if ($ reflProperty ) {
121- /** @var Change $annotation */
122- $ annotation = $ this ->reader ->getPropertyAnnotation (
123- $ classMetadata ->getReflectionProperty ($ property ),
124- Change::class
125- );
126- return $ annotation ;
127- }
128-
129- throw new \ReflectionException (
130- $ classMetadata ->getName () . '. ' . $ property . ' not found. Could this be a private field of a parent class? '
131- );
132- }
133109}
0 commit comments