Test Failed
Push — develop ( d382d0...28e0cd )
by Daniel
10:59
created

RouteAwareSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\EventSubscriber\EntitySubscriber;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Event\LifecycleEventArgs;
9
use Doctrine\ORM\Event\PreFlushEventArgs;
10
use Doctrine\ORM\Event\PreUpdateEventArgs;
11
use Silverback\ApiComponentBundle\Entity\Route\Route;
12
use Silverback\ApiComponentBundle\Entity\Route\RouteAwareInterface;
13
use Silverback\ApiComponentBundle\Factory\RouteFactory;
14
15
/**
16
 * @author Daniel West <[email protected]>
17
 */
18
class RouteAwareSubscriber implements EntitySubscriberInterface
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
    public function supportsEntity($entity = null): bool
44
    {
45
        return $entity instanceof RouteAwareInterface;
46
    }
47
48
    /**
49
     * @param LifecycleEventArgs $eventArgs
50
     */
51
    public function prePersist(LifecycleEventArgs $eventArgs): void
52
    {
53
        $entity = $eventArgs->getEntity();
54
        $this->prePersistUpdate($entity, $eventArgs->getEntityManager());
55
    }
56
57
    /**
58
     * @param PreUpdateEventArgs $eventArgs
59
     */
60
    public function preUpdate(PreUpdateEventArgs $eventArgs): void
61
    {
62
        $entity = $eventArgs->getEntity();
63
        $this->prePersistUpdate($entity, $eventArgs->getEntityManager());
64
    }
65
66
    /**
67
     * @param mixed $entity
68
     * @param EntityManager $em
69
     */
70
    public function prePersistUpdate($entity, EntityManager $em): void
71
    {
72
        if (
73
            $entity instanceof RouteAwareInterface &&
74
            $route = $this->createPageRoute($entity)
75
        ) {
76
            $em->persist($route);
77
        }
78
    }
79
80
    /**
81
     * @param PreFlushEventArgs $eventArgs
82
     */
83
    public function preFlush(PreFlushEventArgs $eventArgs): void
84
    {
85
        $em = $eventArgs->getEntityManager();
86
        $uow = $em->getUnitOfWork();
87
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
88
            if (
89
                $entity instanceof RouteAwareInterface &&
90
                $route = $this->createPageRoute($entity)
91
            ) {
92
                $pageClassMetaData = $em->getClassMetadata(\get_class($entity));
93
                $uow = $em->getUnitOfWork();
94
                $uow->recomputeSingleEntityChangeSet($pageClassMetaData, $entity);
95
                $em->persist($route);
96
            }
97
        }
98
    }
99
100
    /**
101
     * @param RouteAwareInterface $entity
102
     * @return null|Route
103
     */
104
    private function createPageRoute(RouteAwareInterface $entity): ?Route
105
    {
106
        if (0 === $entity->getRoutes()->count()) {
107
            return $this->routeFactory->createFromRouteAwareEntity($entity);
108
        }
109
        return null;
110
    }
111
}
112