Completed
Pull Request — master (#351)
by Leny
06:37
created

WidgetRenderer::render()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 9
nc 2
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($templateName, $parameters);
68
    }
69
70
    /**
71
     * render a widget.
72
     *
73
     * @param Widget $widget
74
     * @param View   $view
75
     *
76
     * @return string
77
     */
78
    public function renderContainer(Widget $widget, View $view)
79
    {
80
        $dispatcher = $this->container->get('event_dispatcher');
81
82
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_PRE_RENDER, new WidgetRenderEvent($widget));
83
84
        $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
85
86
        $directive = '';
87
        if ($this->container->get('security.context')->isGranted('ROLE_VICTOIRE')) {
88
            $directive = 'widget';
89
        }
90
91
        $id = 'vic-widget-'.$widget->getId().'-container';
92
93
        $html = sprintf('<div %s widget-map="%s" id="%s" class="vic-widget-container" data-id="%s">', $directive, $widgetMap->getId(), $id, $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...
94
95
        $content = $this->widgetCache->fetch($widget);
96
        if (null === $content) {
97
            $content = $this->render($widget, $view);
98
            $this->widgetCache->save($widget, $content);
99
        }
100
        $html .= $content;
101
        $html .= '</div>'; //close container
102
103
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_RENDER, new WidgetRenderEvent($widget, $html));
104
105
        return $html;
106
    }
107
108
    /**
109
     * prepare a widget to be rendered asynchronously.
110
     *
111
     * @param int $widgetId
112
     *
113
     * @return string
114
     */
115
    public function prepareAsynchronousRender($widgetId)
116
    {
117
        $ngControllerName = 'widget'.$widgetId.'AsynchronousLoadCtrl';
118
        $ngDirectives = sprintf('ng-controller="WidgetAsynchronousLoadController as %s" class="vic-widget" ng-init="%s.init(%d)" ng-bind-html="html"', $ngControllerName, $ngControllerName, $widgetId);
119
        $html = sprintf('<div class="vic-widget-container vic-widget-asynchronous" data-id="%d" %s></div>', $widgetId, $ngDirectives);
120
121
        return $html;
122
    }
123
124
    /**
125
     * render widget unlink action.
126
     *
127
     * @param int  $widgetId
128
     * @param View $view
129
     *
130
     * @return string
131
     */
132
    public function renderUnlinkActionByWidgetId($widgetId, $view)
133
    {
134
        return $this->container->get('victoire_templating')->render(
135
            'VictoireCoreBundle:Widget:widgetUnlinkAction.html.twig',
136
            [
137
                'widgetId' => $widgetId,
138
                'view'     => $view,
139
            ]
140
        );
141
    }
142
143
    /**
144
     * Compute slot options.
145
     *
146
     * @param Slot  $slotId
147
     * @param array $options
148
     *
149
     * @return string
150
     */
151
    public function computeOptions($slotId, $options = [])
152
    {
153
        $slots = $this->container->getParameter('victoire_core.slots');
154
155
        $availableWidgets = $this->container->getParameter('victoire_core.widgets');
156
        $widgets = [];
157
158
        //If the slot is declared in config
159
        if (!empty($slots[$slotId]) && !empty($slots[$slotId]['widgets'])) {
160
            //parse declared widgets
161
            $slotWidgets = array_keys($slots[$slotId]['widgets']);
162
        } elseif (!empty($options['availableWidgets'])) {
163
            $slotWidgets = $options['availableWidgets'];
164
        } else {
165
            //parse all widgets
166
            $slotWidgets = array_keys($availableWidgets);
167
        }
168
169
        foreach ($slotWidgets as $slotWidget) {
170
            $widgetParams = $availableWidgets[$slotWidget];
171
            $widgets[$slotWidget]['params'] = $widgetParams;
172
        }
173
        $slots[$slotId]['availableWidgets'] = $widgets;
174
        if (isset($options['max'])) {
175
            $slots[$slotId]['max'] = $options['max'];
176
        }
177
178
        return $slots[$slotId];
179
    }
180
181
    /**
182
     * Get the extra classes for the css.
183
     *
184
     * @return string The classes
185
     */
186
    public function getExtraCssClass(Widget $widget)
187
    {
188
        $cssClass = 'vic-widget-'.strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget));
189
190
        return $cssClass;
191
    }
192
193
    /**
194
     * Render the CSS style for a Widget.
195
     *
196
     * @param Widget $widget
197
     *
198
     * @return mixed
199
     */
200
    public function renderStyle(Widget $widget)
201
    {
202
        return $this->container->get('victoire_templating')->render(
203
            'VictoireCoreBundle:Widget:style/style.html.twig',
204
            [
205
                'widget'                   => $widget,
206
                'victoire_twig_responsive' => $this->victoireTwigResponsive,
207
            ]
208
        );
209
    }
210
}
211