Total Complexity | 12 |
Total Lines | 84 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | <?php |
||
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 |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param RouteAwareInterface $entity |
||
88 | * @return null|Route |
||
89 | */ |
||
90 | private function createPageRoute(RouteAwareInterface $entity): ?Route |
||
96 | } |
||
97 | } |
||
98 |