Completed
Pull Request — master (#351)
by Leny
06:31
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\Entity\Widget;
11
use Victoire\Bundle\WidgetMapBundle\Entity\Slot;
12
use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper;
13
14
class WidgetRenderer
15
{
16
    private $container;
17
    private $victoireTwigResponsive;
18
19
    public function __construct(Container $container, $victoireTwigResponsive)
20
    {
21
        $this->container = $container;
22
        $this->victoireTwigResponsive = $victoireTwigResponsive;
23
    }
24
25
    /**
26
     * render the Widget.
27
     *
28
     * @param Widget $widget
29
     * @param View   $view
30
     *
31
     * @return widget show
32
     */
33
    public function render(Widget $widget, View $view)
34
    {
35
        //the mode of display of the widget
36
        $mode = $widget->getMode();
37
38
        //if entty is given and it's not the object, retrive it and set the entity for the widget
39
        if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) {
40
            $widget->setEntity($view->getBusinessEntity());
41
        }
42
43
        //the templating service
44
        $templating = $this->container->get('victoire_templating');
45
46
        //the content of the widget
47
        $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget);
48
49
        //the template displayed is in the widget bundle (with the potential theme)
50
        $showView = 'show'.ucfirst($widget->getTheme());
51
        $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget);
52
53
        return $templating->render($templateName, $parameters);
54
    }
55
56
    /**
57
     * render a widget.
58
     *
59
     * @param Widget $widget
60
     * @param View   $view
61
     *
62
     * @return string
63
     */
64
    public function renderContainer(Widget $widget, View $view)
65
    {
66
        $dispatcher = $this->container->get('event_dispatcher');
67
68
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_PRE_RENDER, new WidgetRenderEvent($widget));
69
70
        $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
71
72
        $directive = '';
73
        if ($this->container->get('security.context')->isGranted('ROLE_VICTOIRE')) {
74
            $directive = 'widget';
75
        }
76
77
        $id = "vic-widget-".$widget->getId()."-container";
78
79
        $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...
80
        $html .= $this->render($widget, $view);
81
        $html .= '</div>';
82
83
        $dispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_RENDER, new WidgetRenderEvent($widget, $html));
84
85
        return $html;
86
    }
87
88
    /**
89
     * prepare a widget to be rendered asynchronously.
90
     *
91
     * @param int $widgetId
92
     *
93
     * @return string
94
     */
95
    public function prepareAsynchronousRender($widgetId)
96
    {
97
        $ngControllerName = 'widget'.$widgetId.'AsynchronousLoadCtrl';
98
        $ngDirectives = sprintf('ng-controller="WidgetAsynchronousLoadController as %s" class="vic-widget" ng-init="%s.init(%d)" ng-bind-html="html"', $ngControllerName, $ngControllerName, $widgetId);
99
        $html = sprintf('<div class="vic-widget-container vic-widget-asynchronous" data-id="%d" %s></div>', $widgetId, $ngDirectives);
100
101
        return $html;
102
    }
103
104
    /**
105
     * render widget unlink action.
106
     *
107
     * @param int  $widgetId
108
     * @param View $view
109
     *
110
     * @return string
111
     */
112
    public function renderUnlinkActionByWidgetId($widgetId, $view)
113
    {
114
        return $this->container->get('victoire_templating')->render(
115
            'VictoireCoreBundle:Widget:widgetUnlinkAction.html.twig',
116
            [
117
                'widgetId' => $widgetId,
118
                'view'     => $view,
119
            ]
120
        );
121
    }
122
123
    /**
124
     * Compute slot options.
125
     *
126
     * @param Slot  $slotId
127
     * @param array $options
128
     *
129
     * @return string
130
     */
131
    public function computeOptions($slotId, $options = [])
132
    {
133
        $slots = $this->container->getParameter('victoire_core.slots');
134
135
        $availableWidgets = $this->container->getParameter('victoire_core.widgets');
136
        $widgets = [];
137
138
        //If the slot is declared in config
139
        if (!empty($slots[$slotId]) && !empty($slots[$slotId]['widgets'])) {
140
            //parse declared widgets
141
            $slotWidgets = array_keys($slots[$slotId]['widgets']);
142
        } elseif (!empty($options['availableWidgets'])) {
143
            $slotWidgets = $options['availableWidgets'];
144
        } else {
145
            //parse all widgets
146
            $slotWidgets = array_keys($availableWidgets);
147
        }
148
149
        foreach ($slotWidgets as $slotWidget) {
150
            $widgetParams = $availableWidgets[$slotWidget];
151
            $widgets[$slotWidget]['params'] = $widgetParams;
152
        }
153
        $slots[$slotId]['availableWidgets'] = $widgets;
154
        if (isset($options['max'])) {
155
            $slots[$slotId]['max'] = $options['max'];
156
        }
157
158
        return $slots[$slotId];
159
    }
160
161
    /**
162
     * Get the extra classes for the css.
163
     *
164
     * @return string The classes
165
     */
166
    public function getExtraCssClass(Widget $widget)
167
    {
168
        $cssClass = 'vic-widget-'.strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget));
169
170
        return $cssClass;
171
    }
172
173
    /**
174
     * Render the CSS style for a Widget.
175
     *
176
     * @param Widget $widget
177
     *
178
     * @return mixed
179
     */
180
    public function renderStyle(Widget $widget)
181
    {
182
        return $this->container->get('victoire_templating')->render(
183
            'VictoireCoreBundle:Widget:style/style.html.twig',
184
            [
185
                'widget'                   => $widget,
186
                'victoire_twig_responsive' => $this->victoireTwigResponsive,
187
            ]
188
        );
189
    }
190
}
191