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