Completed
Push — master ( 5d51b4...87fdd9 )
by Paul
10s
created

ViewReferenceSubscriber::updateViewReference()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 14
nc 6
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\I18nBundle\Entity\ViewTranslation;
10
use Victoire\Bundle\ViewReferenceBundle\Event\ViewReferenceEvent;
11
use Victoire\Bundle\ViewReferenceBundle\ViewReferenceEvents;
12
13
/**
14
 * Tracks if a slug changed and re-compute the view cache
15
 * ref: victoire_view_reference.event_subscriber.
16
 */
17
class ViewReferenceSubscriber implements \Doctrine\Common\EventSubscriber
18
{
19
    protected $businessPageBuilder;
20
    protected $viewReferenceProvider;
21
    protected $viewReferenceHelper;
22
    protected $dispatcher;
23
24
    /**
25
     * ViewReferenceSubscriber constructor.
26
     *
27
     * @param EventDispatcherInterface $dispatcher
28
     */
29
    public function __construct(EventDispatcherInterface $dispatcher)
30
    {
31
        $this->dispatcher = $dispatcher;
32
    }
33
34
    /**
35
     * @return string[]
36
     */
37
    public function getSubscribedEvents()
38
    {
39
        return [
40
            Events::postUpdate,
41
            Events::postPersist,
42
            Events::preRemove,
43
        ];
44
    }
45
46
    /**
47
     * @param LifecycleEventArgs $eventArgs
48
     */
49
    public function postUpdate(LifecycleEventArgs $eventArgs)
50
    {
51
        $this->updateViewReference($eventArgs);
52
    }
53
54
    /**
55
     * @param LifecycleEventArgs $eventArgs
56
     */
57
    public function postPersist(LifecycleEventArgs $eventArgs)
58
    {
59
        $this->updateViewReference($eventArgs);
60
    }
61
62
    /**
63
     * @param LifecycleEventArgs $eventArgs
64
     */
65
    public function preRemove(LifecycleEventArgs $eventArgs)
66
    {
67
        $entity = $eventArgs->getEntity();
68
        // if a page is remove we remove his viewRef
69
        if ($entity instanceof WebViewInterface) {
70
            $event = new ViewReferenceEvent($entity);
71
            $this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event);
72
        }
73
    }
74
75
    /**
76
     * This method dispatch the event that the view must be build/rebuild.
77
     *
78
     * @param LifecycleEventArgs $eventArgs
79
     */
80
    private function updateViewReference(LifecycleEventArgs $eventArgs)
81
    {
82
        $entity = $eventArgs->getEntity();
83
        $view = null;
84
        $translations = [];
85
        if ($entity instanceof WebViewInterface) {
86
            $translations = $entity->getTranslations();
87
            $view = $entity;
88
        } elseif ($entity instanceof ViewTranslation && $entity->getTranslatable() instanceof WebViewInterface) {
89
            $translations = [$entity];
90
            $view = $entity->getTranslatable();
91
        }
92
93
        // if a page is persisted we rebuild his viewRef
94
        foreach ($translations as $translation) {
95
            $view->setCurrentLocale($translation->getLocale());
96
            $event = new ViewReferenceEvent($view);
0 ignored issues
show
Bug introduced by
It seems like $view defined by null on line 83 can be null; however, Victoire\Bundle\ViewRefe...nceEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
97
            $this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event);
98
        }
99
    }
100
}
101