Completed
Pull Request — master (#354)
by Sébastien
06:19
created

WidgetRenderer::renderContainer()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 16
nc 4
nop 2
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Renderer;
4
5
use Symfony\Component\DependencyInjection\Container;
6
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
use Victoire\Bundle\CoreBundle\Event\WidgetRenderEvent;
9
use Victoire\Bundle\CoreBundle\VictoireCmsEvents;
10
use Victoire\Bundle\WidgetBundle\Cache\WidgetCache;
11
use Victoire\Bundle\WidgetBundle\Entity\Widget;
12
use Victoire\Bundle\WidgetMapBundle\Entity\Slot;
13
use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper;
14
15
class WidgetRenderer
16
{
17
    private $container;
18
    private $victoireTwigResponsive;
19
    /**
20
     * @var WidgetCache
21
     */
22
    private $widgetCache;
23
24
    /**
25
     * WidgetRenderer constructor.
26
     *
27
     * @param Container   $container
28
     * @param             $victoireTwigResponsive
29
     * @param Client      $redis
0 ignored issues
show
Bug introduced by
There is no parameter named $redis. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     * @param WidgetCache $widgetCache
31
     */
32
    public function __construct(Container $container, $victoireTwigResponsive, WidgetCache $widgetCache)
33
    {
34
        $this->container = $container;
35
        $this->victoireTwigResponsive = $victoireTwigResponsive;
36
        $this->widgetCache = $widgetCache;
37
    }
38
39
    /**
40
     * render the Widget.
41
     *
42
     * @param Widget $widget
43
     * @param View   $view
44
     *
45
     * @return widget show
46
     */
47
    public function render(Widget $widget, View $view)
48
    {
49
        //the mode of display of the widget
50
        $mode = $widget->getMode();
51
52
        //if entty is given and it's not the object, retrive it and set the entity for the widget
53
        if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) {
54
            $widget->setEntity($view->getBusinessEntity());
55
        }
56
57
        //the templating service
58
        $templating = $this->container->get('victoire_templating');
59
60
        //the content of the widget
61
        $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget);
62
63
        //the template displayed is in the widget bundle (with the potential theme)
64
        $showView = 'show'.ucfirst($widget->getTheme());
65
        $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget);
66
67
        return $templating->render(
68
            $templateName,
69
            $parameters
70
        );
71
    }
72
73
    /**
74
     * render a widget.
75
     *
76
     * @param Widget $widget
77
     * @param View   $view
78
     *
79
     * @return string
80
     */
81
    public function renderContainer(Widget $widget, View $view)
82
    {
83
        $dispatcher = $this->container->get('event_dispatcher');
84
85
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_PRE_RENDER, new WidgetRenderEvent($widget));
86
87
        $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
88
89
        $directive = '';
90
        if ($this->container->get('security.context')->isGranted('ROLE_VICTOIRE')) {
91
            $directive = 'widget';
92
        }
93
94
        $html = sprintf('<div %s widget-map="%s" class="vic-widget-container" data-id="%s">', $directive, $widgetMap->getId(), $widget->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
95
96
        $content = $this->widgetCache->fetch($widget);
97
        if (null === $content) {
98
            $content = $this->render($widget, $view);
99
            $this->widgetCache->save($widget, $content);
100
        }
101
        $html .= $content;
102
        $html .= '</div>';
103
104
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_RENDER, new WidgetRenderEvent($widget, $html));
105
106
        return $html;
107
    }
108
109
    /**
110
     * prepare a widget to be rendered asynchronously.
111
     *
112
     * @param int $widgetId
113
     *
114
     * @return string
115
     */
116
    public function prepareAsynchronousRender($widgetId)
117
    {
118
        $ngControllerName = 'widget'.$widgetId.'AsynchronousLoadCtrl';
119
        $ngDirectives = sprintf('ng-controller="WidgetAsynchronousLoadController as %s" class="vic-widget" ng-init="%s.init(%d)" ng-bind-html="html"', $ngControllerName, $ngControllerName, $widgetId);
120
        $html = sprintf('<div class="vic-widget-container vic-widget-asynchronous" data-id="%d" %s></div>', $widgetId, $ngDirectives);
121
122
        return $html;
123
    }
124
125
    /**
126
     * render widget unlink action.
127
     *
128
     * @param int  $widgetId
129
     * @param View $view
130
     *
131
     * @return string
132
     */
133
    public function renderUnlinkActionByWidgetId($widgetId, $view)
134
    {
135
        return $this->container->get('victoire_templating')->render(
136
            'VictoireCoreBundle:Widget:widgetUnlinkAction.html.twig',
137
            [
138
                'widgetId' => $widgetId,
139
                'view'     => $view,
140
            ]
141
        );
142
    }
143
144
    /**
145
     * Compute slot options.
146
     *
147
     * @param Slot  $slotId
148
     * @param array $options
149
     *
150
     * @return string
151
     */
152
    public function computeOptions($slotId, $options = [])
153
    {
154
        $slots = $this->container->getParameter('victoire_core.slots');
155
156
        $availableWidgets = $this->container->getParameter('victoire_core.widgets');
157
        $widgets = [];
158
159
        //If the slot is declared in config
160
        if (!empty($slots[$slotId]) && !empty($slots[$slotId]['widgets'])) {
161
            //parse declared widgets
162
            $slotWidgets = array_keys($slots[$slotId]['widgets']);
163
        } elseif (!empty($options['availableWidgets'])) {
164
            $slotWidgets = $options['availableWidgets'];
165
        } else {
166
            //parse all widgets
167
            $slotWidgets = array_keys($availableWidgets);
168
        }
169
170
        foreach ($slotWidgets as $slotWidget) {
171
            $widgetParams = $availableWidgets[$slotWidget];
172
            $widgets[$slotWidget]['params'] = $widgetParams;
173
        }
174
        $slots[$slotId]['availableWidgets'] = $widgets;
175
        if (isset($options['max'])) {
176
            $slots[$slotId]['max'] = $options['max'];
177
        }
178
179
        return $slots[$slotId];
180
    }
181
182
    /**
183
     * Get the extra classes for the css.
184
     *
185
     * @return string The classes
186
     */
187
    public function getExtraCssClass(Widget $widget)
188
    {
189
        $cssClass = 'vic-widget-'.strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget));
190
191
        return $cssClass;
192
    }
193
194
    /**
195
     * Render the CSS style for a Widget.
196
     *
197
     * @param Widget $widget
198
     *
199
     * @return mixed
200
     */
201
    public function renderStyle(Widget $widget)
202
    {
203
        return $this->container->get('victoire_templating')->render(
204
            'VictoireCoreBundle:Widget:style/style.html.twig',
205
            [
206
                'widget'                   => $widget,
207
                'victoire_twig_responsive' => $this->victoireTwigResponsive,
208
            ]
209
        );
210
    }
211
}
212