Completed
Push — develop ( f05cec...11f198 )
by Daniel
07:33
created

RouteAwareSubscriber::preUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\EventSubscriber\Doctrine;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8
use Doctrine\ORM\Event\PreFlushEventArgs;
9
use Doctrine\ORM\Event\PreUpdateEventArgs;
10
use Silverback\ApiComponentBundle\Entity\Route\Route;
11
use Silverback\ApiComponentBundle\Entity\Route\RouteAwareInterface;
12
use Silverback\ApiComponentBundle\Factory\Entity\Route\RouteFactory;
13
14
/**
15
 *
16
 * @author Daniel West <[email protected]>
17
 */
18
class RouteAwareSubscriber implements EventSubscriber
19
{
20
    /**
21
     * @var RouteFactory
22
     */
23
    private $routeFactory;
24
25
    public function __construct(
26
        RouteFactory $routeFactory
27
    ) {
28
        $this->routeFactory = $routeFactory;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function getSubscribedEvents(): array
35
    {
36
        return [
37
            'prePersist',
38
            'preUpdate',
39
            'preFlush'
40
        ];
41
    }
42
43
    /**
44
     * @param LifecycleEventArgs $eventArgs
45
     * @throws \Doctrine\ORM\ORMException
46
     */
47
    public function prePersist(LifecycleEventArgs $eventArgs): void
48
    {
49
        $entity = $eventArgs->getEntity();
50
        $this->prePersistUpdate($entity, $eventArgs->getEntityManager());
51
    }
52
53
    /**
54
     * @param PreUpdateEventArgs $eventArgs
55
     * @throws \Doctrine\ORM\ORMException
56
     */
57
    public function preUpdate(PreUpdateEventArgs $eventArgs): void
58
    {
59
        $entity = $eventArgs->getEntity();
60
        $this->prePersistUpdate($entity, $eventArgs->getEntityManager());
61
    }
62
63
    /**
64
     * @param mixed $entity
65
     * @param EntityManager $em
66
     * @throws \Doctrine\ORM\ORMException
67
     */
68
    public function prePersistUpdate($entity, EntityManager $em): void
69
    {
70
        if (
71
            $entity instanceof RouteAwareInterface &&
72
            $route = $this->createPageRoute($entity)
73
        ) {
74
            $em->persist($route);
75
        }
76
    }
77
78
    /**
79
     * @param PreFlushEventArgs $eventArgs
80
     * @throws \Doctrine\ORM\ORMException
81
     */
82
    public function preFlush(PreFlushEventArgs $eventArgs): void
83
    {
84
        $em = $eventArgs->getEntityManager();
85
        $uow = $em->getUnitOfWork();
86
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
87
            if (
88
                $entity instanceof RouteAwareInterface &&
89
                $route = $this->createPageRoute($entity)
90
            ) {
91
                $pageClassMetaData = $em->getClassMetadata(\get_class($entity));
92
                $uow = $em->getUnitOfWork();
93
                $uow->recomputeSingleEntityChangeSet($pageClassMetaData, $entity);
94
                $em->persist($route);
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param RouteAwareInterface $entity
101
     * @return null|Route
102
     */
103
    private function createPageRoute(RouteAwareInterface $entity): ?Route
104
    {
105
        if (0 === $entity->getRoutes()->count()) {
106
            return $this->routeFactory->createFromRouteAwareEntity($entity);
107
        }
108
        return null;
109
    }
110
}
111