Completed
Pull Request — master (#325)
by Paul
09:55
created

WidgetRenderer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 16
c 7
b 1
f 0
lcom 1
cbo 5
dl 0
loc 178
rs 10

8 Methods

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