Completed
Push — master ( 3d485f...407dff )
by Paweł
66:49
created

RendererService::getContainerRenderer()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Template Engine Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\TemplatesSystemBundle\Service;
16
17
use SWP\Bundle\TemplatesSystemBundle\Container\ContainerRenderer;
18
use SWP\Bundle\TemplatesSystemBundle\Factory\ContainerRendererFactory;
19
use SWP\Bundle\TemplatesSystemBundle\Provider\ContainerProviderInterface;
20
use SWP\Bundle\TemplatesSystemBundle\Widget\TemplatingWidgetHandler;
21
use Symfony\Component\DependencyInjection\ContainerInterface as ServiceContainerInterface;
22
23
/**
24
 * Class RendererService.
25
 */
26
class RendererService implements RendererServiceInterface
27
{
28
    /**
29
     * @var ServiceContainerInterface
30
     */
31
    protected $serviceContainer;
32
33
    /**
34
     * @var string
35
     */
36
    protected $cacheDir;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $debug;
42
43
    /**
44
     * @var ContainerServiceInterface
45
     */
46
    protected $templateContainerService;
47
48
    /**
49
     * @var ContainerProviderInterface
50
     */
51
    protected $containerProvider;
52
53
    /**
54
     * @var ContainerRendererFactory
55
     */
56
    protected $containerRendererFactory;
57
58
    /**
59
     * RendererService constructor.
60
     *
61
     * @param ServiceContainerInterface  $serviceContainer
62
     * @param string                     $cacheDir
63
     * @param bool                       $debug
64
     * @param ContainerServiceInterface  $templateContainerService
65
     * @param ContainerProviderInterface $containerProvider
66
     * @param ContainerRendererFactory   $containerRendererFactory
67
     */
68
    public function __construct(
69
        ServiceContainerInterface $serviceContainer,
70
        string $cacheDir,
71
        bool $debug,
72
        ContainerServiceInterface $templateContainerService,
73
        ContainerProviderInterface $containerProvider,
74
        ContainerRendererFactory $containerRendererFactory
75
    ) {
76
        $this->cacheDir = $cacheDir;
77
        $this->debug = $debug;
78
        $this->serviceContainer = $serviceContainer;
79
        $this->templateContainerService = $templateContainerService;
80
        $this->containerProvider = $containerProvider;
81
        $this->containerRendererFactory = $containerRendererFactory;
82
    }
83
84
    /**
85
     * @param string $name
86
     * @param array  $parameters
87
     * @param bool   $createIfNotExists
88
     *
89
     * @return ContainerRenderer
90
     *
91
     * @throws \Exception
92
     */
93
    public function getContainerRenderer($name, array $parameters = [], $createIfNotExists = true)
94
    {
95
        $container = $this->containerProvider->getOneByName($name);
96
        if (!$container && $createIfNotExists) {
97
            $container = $this->templateContainerService->createContainer($name, $parameters);
98
        } elseif (!$container) {
99
            throw new \Exception('Container was not found');
100
        }
101
102
        $containerRenderer = $this->containerRendererFactory->create($container, null, $this->cacheDir, $this->debug);
0 ignored issues
show
Documentation introduced by
$this->cacheDir is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
        $widgets = $this->initializeWidgets($this->containerProvider->getContainerWidgets($container));
104
        $containerRenderer->setWidgets($widgets);
105
106
        return $containerRenderer;
107
    }
108
109
    /**
110
     * @param $containerWidgets
111
     *
112
     * @return array
113
     */
114
    private function initializeWidgets($containerWidgets)
115
    {
116
        $widgets = [];
117
        foreach ($containerWidgets as $widget) {
118
            $widgetModel = $widget->getWidget();
119
            $widgetClass = $widgetModel->getType();
120
121
            if (is_a($widgetClass, TemplatingWidgetHandler::class, true)) {
122
                $widgetHandler = new $widgetClass($widgetModel, $this->serviceContainer);
123
            } else {
124
                $widgetHandler = new $widgetClass($widgetModel);
125
            }
126
127
            $widgets[] = $widgetHandler;
128
        }
129
130
        return $widgets;
131
    }
132
}
133