Completed
Push — develop ( e72854...0832ef )
by Daniel
06:46
created

RouteAwareSubscriber::createPageRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
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\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Event\LifecycleEventArgs;
10
use Doctrine\ORM\Event\PreFlushEventArgs;
11
use Doctrine\ORM\Event\PreUpdateEventArgs;
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
64
        $this->prePersistUpdate($entity, $eventArgs->getEntityManager());
65
    }
66
67
    /**
68
     * @param mixed $entity
69
     * @param EntityManager $em
70
     */
71
    public function prePersistUpdate($entity, EntityManager $em): void
72
    {
73
        $this->routeFactory->createPageRoute($entity, $em);
74
    }
75
76
    /**
77
     * @param PreFlushEventArgs $eventArgs
78
     */
79
    public function preFlush(PreFlushEventArgs $eventArgs): void
80
    {
81
        $em = $eventArgs->getEntityManager();
82
        $uow = $em->getUnitOfWork();
83
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
84
            if (
85
                $routes = $this->routeFactory->createPageRoute($entity)
0 ignored issues
show
Unused Code introduced by
The assignment to $routes is dead and can be removed.
Loading history...
86
            ) {
87
                $pageClassMetaData = $em->getClassMetadata(\get_class($entity));
88
                $uow = $em->getUnitOfWork();
89
                $uow->recomputeSingleEntityChangeSet($pageClassMetaData, $entity);
90
            }
91
        }
92
    }
93
}
94