Completed
Pull Request — master (#357)
by Paul
08:47 queued 02:41
created

WidgetRenderer::renderStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

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