|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\ViewReferenceBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
6
|
|
|
use Doctrine\ORM\Events; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface; |
|
9
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Event\ViewReferenceEvent; |
|
10
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReferenceEvents; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Tracks if a slug changed and re-compute the view cache |
|
14
|
|
|
* ref: victoire_view_reference.event_subscriber. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ViewReferenceSubscriber implements \Doctrine\Common\EventSubscriber |
|
17
|
|
|
{ |
|
18
|
|
|
protected $businessPageBuilder; |
|
19
|
|
|
protected $viewReferenceProvider; |
|
20
|
|
|
protected $viewReferenceHelper; |
|
21
|
|
|
protected $dispatcher; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* ViewReferenceSubscriber constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->dispatcher = $dispatcher; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string[] |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getSubscribedEvents() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
Events::postUpdate, |
|
40
|
|
|
Events::postPersist, |
|
41
|
|
|
Events::preRemove, |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
47
|
|
|
*/ |
|
48
|
|
|
public function postUpdate(LifecycleEventArgs $eventArgs) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->updateViewReference($eventArgs); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
55
|
|
|
*/ |
|
56
|
|
|
public function postPersist(LifecycleEventArgs $eventArgs) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->updateViewReference($eventArgs); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
public function preRemove(LifecycleEventArgs $eventArgs) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$entity = $eventArgs->getEntity(); |
|
67
|
|
|
// if a page is remove we remove his viewRef |
|
68
|
|
|
if ($entity instanceof WebViewInterface) { |
|
69
|
|
|
$event = new ViewReferenceEvent($entity); |
|
70
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* This method dispatch the event that the view must be build/rebuild. |
|
76
|
|
|
* |
|
77
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
private function updateViewReference(LifecycleEventArgs $eventArgs) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$entity = $eventArgs->getEntity(); |
|
82
|
|
|
// if a page is persisted we rebuild his viewRef |
|
83
|
|
|
if ($entity instanceof WebViewInterface) { |
|
84
|
|
|
$event = new ViewReferenceEvent($entity); |
|
85
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.