Completed
Push — master ( f1e90d...70992f )
by Saulius
18s
created

DoctrineEventsSubscriber::preUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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