Completed
Push — master ( 7a257e...017c35 )
by Leny
109:58 queued 102:36
created

ViewReferenceSubscriber::rebuildViewsReferenceCache()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.439
cc 5
eloc 17
nc 3
nop 1
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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