Passed
Push — develop ( 4f08b4...f08141 )
by Daniel
05:37
created

RouteAwareListener::preFlush()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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