| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the sauls/object-registry-bundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author    Saulius Vaičeliūnas <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link      http://saulius.vaiceliunas.lt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2018 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Sauls\Bundle\ObjectRegistryBundle\EvenListener; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Doctrine\Common\EventSubscriber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Doctrine\ORM\Event\LifecycleEventArgs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Doctrine\ORM\Event\PreUpdateEventArgs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Doctrine\ORM\Events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Event\DoctrineObjectEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Event\GenericDoctrineObjectEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sauls\Bundle\ObjectRegistryBundle\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Sauls\Bundle\ObjectRegistryBundle\Factory\EventNameFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class DoctrineEventsSubscriber implements EventSubscriber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var EventDispatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var EventNameFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $eventNameFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 7 |  |     public function __construct(EventDispatcherInterface $eventDispatcher, EventNameFactoryInterface $eventNameFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 7 |  |         $this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 7 |  |         $this->eventNameFactory = $eventNameFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     public function getSubscribedEvents(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             Events::prePersist => 'prePersist', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             Events::postPersist => 'postPersist', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             Events::preUpdate => 'preUpdate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             Events::postUpdate => 'postUpdate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             Events::preRemove => 'preRemove', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             Events::postRemove => 'postRemove', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |     public function prePersist(LifecycleEventArgs $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         $this->process(DoctrineObjectEvents::PRE_PERSIST, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string                                $eventName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param LifecycleEventArgs|PreUpdateEventArgs $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |     private function process(string $eventName, LifecycleEventArgs $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         $entity = $event->getEntity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 6 |  |         $eventName = $this->eventNameFactory->create($eventName, $entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 6 |  |         $newEvent = new GenericDoctrineObjectEvent($entity, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |         $this->eventDispatcher->dispatch($eventName, $newEvent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |     public function postPersist(LifecycleEventArgs $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         $this->process(DoctrineObjectEvents::POST_PERSIST, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |     public function preUpdate(PreUpdateEventArgs $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $this->process(DoctrineObjectEvents::PRE_UPDATE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |     public function postUpdate(LifecycleEventArgs $event): void | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |         $this->process(DoctrineObjectEvents::POST_UPDATE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |     public function preRemove(LifecycleEventArgs $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         $this->process(DoctrineObjectEvents::PRE_REMOVE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |     public function postRemove(LifecycleEventArgs $event): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |         $this->process(DoctrineObjectEvents::POST_REMOVE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |