Completed
Pull Request — master (#433)
by Paul
13:09 queued 06:21
created

ViewReferenceSubscriber::updateViewReference()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 13
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->getTranslatable();
0 ignored issues
show
Bug introduced by
The method getTranslatable() does not seem to exist on object<Victoire\Bundle\C...ntity\WebViewInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
            $view = $entity;
88
        } elseif ($entity instanceof ViewTranslation && $view = $entity->getTranslatable() instanceof WebViewInterface) {
89
            $translations = [$entity];
90
        }
91
92
        // if a page is persisted we rebuild his viewRef
93
        foreach ($translations as $translation) {
94
            $view->setCurrentLocale($translation->getLocale());
95
            $event = new ViewReferenceEvent($view);
0 ignored issues
show
Bug introduced by
It seems like $view can also be of type boolean or null; however, Victoire\Bundle\ViewRefe...nceEvent::__construct() does only seem to accept object<Victoire\Bundle\C...ntity\WebViewInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
            $this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event);
97
        }
98
    }
99
}
100