Passed
Push — develop ( e028b5...c8e85b )
by Daniel
04:55
created

RouteAwareSubscriber::preFlush()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.456

Importance

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