Completed
Push — master ( fecb59...e32f72 )
by Paweł
29:36
created

ContainerService::getContainer()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.73

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 16
cts 22
cp 0.7272
rs 8.439
c 0
b 0
f 0
cc 6
eloc 23
nc 7
nop 3
crap 6.73
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\TemplateEngineBundle\Service;
16
17
use SWP\Bundle\TemplateEngineBundle\Container\SimpleContainer;
18
use SWP\Bundle\TemplateEngineBundle\Model\Container;
19
use SWP\Bundle\TemplateEngineBundle\Model\ContainerData;
20
use SWP\Component\Common\Event\HttpCacheEvent;
21
use Symfony\Bridge\Doctrine\RegistryInterface;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
25
class ContainerService
26
{
27
    const OPEN_TAG_TEMPLATE = '<div id="swp_container_{{ id }}" class="swp_container {{ class }}" style="{% if height %}height: {{ height }}px;{% endif %}{% if width %}width: {{width}}px;{% endif %}{{styles}}"{% for value in data %} data-{{value.getKey()}}="{{value.getValue()}}"{% endfor %} >';
28
    const CLOSE_TAG_TEMPLATE = '</div>';
29
30
    protected $serviceContainer;
31
    protected $objectManager;
32
    protected $cacheDir;
33
    protected $debug;
34
    protected $renderer = false;
35
    protected $eventDispatcher;
36
37
    /**
38
     * ContainerService constructor.
39
     *
40
     * @param RegistryInterface        $registry
41
     * @param EventDispatcherInterface $eventDispatcher
42
     * @param ContainerInterface       $serviceContainer
43
     * @param string                   $cacheDir
44
     * @param bool                     $debug
45
     */
46 86
    public function __construct(RegistryInterface $registry, EventDispatcherInterface $eventDispatcher, ContainerInterface $serviceContainer, $cacheDir, $debug = false)
47
    {
48 86
        $this->objectManager = $registry->getEntityManager();
49 86
        $this->cacheDir = $cacheDir.'/twig';
50 86
        $this->debug = $debug;
51 86
        $this->eventDispatcher = $eventDispatcher;
52 86
        $this->serviceContainer = $serviceContainer;
53 86
    }
54
55 2
    public function getContainer($name, array $parameters = [], $createIfNotExists = true)
56
    {
57 2
        $containerEntity = $this->objectManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Container')
58 2
            ->getByName($name)
59 2
            ->getOneOrNullResult();
60
61 2
        if (!$containerEntity && $createIfNotExists) {
62 1
            $containerEntity = $this->createNewContainer($name, $parameters);
63 1
        } elseif (!$containerEntity) {
64 1
            throw new \Exception('Container was not found');
65
        }
66
67 1
        $widgets = [];
68 1
        $containerWidgets = $this->objectManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\ContainerWidget')
69 1
            ->getSortedWidgets(['container' => $containerEntity])
70 1
            ->getResult();
71
72 1
        foreach ($containerWidgets as $containerWidget) {
73
            $widgetModel = $containerWidget->getWidget();
74
            $widgetClass = $widgetModel->getType();
75
76
            if (is_a($widgetClass, '\SWP\Bundle\TemplateEngineBundle\Widget\TemplatingWidgetHandler', true)) {
77
                $widgetHandler = new $widgetClass($widgetModel, $this->serviceContainer->get('templating'));
78
            } else {
79
                $widgetHandler = new $widgetClass($widgetModel);
80
            }
81
82
            $widgets[] = $widgetHandler;
83
        }
84
85 1
        $container = new SimpleContainer($containerEntity, $this->getRenderer());
0 ignored issues
show
Bug introduced by
It seems like $this->getRenderer() targeting SWP\Bundle\TemplateEngin...rService::getRenderer() can also be of type boolean; however, SWP\Bundle\TemplateEngin...ontainer::__construct() does only seem to accept object<Twig_Environment>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86 1
        $container->setWidgets($widgets);
87
88 1
        return $container;
89
    }
90
91 2
    public function getRenderer()
92
    {
93 2
        if ($this->renderer !== false) {
94
            return $this->renderer;
95
        }
96
97 2
        $options = [];
98 2
        if (false === $this->debug) {
99
            $options['cache'] = $this->cacheDir;
100
        }
101
102 2
        $this->renderer = new \Twig_Environment(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Twig_Environment(ne...G_TEMPLATE)), $options) of type object<Twig_Environment> is incompatible with the declared type boolean of property $renderer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
103 2
            new \Twig_Loader_Array([
104 2
                'open_tag' => self::OPEN_TAG_TEMPLATE,
105 2
                'close_tag' => self::CLOSE_TAG_TEMPLATE,
106
            ]), $options
107
        );
108
109 2
        return $this->renderer;
110
    }
111
112 2
    public function createNewContainer($name, array $parameters = [])
113
    {
114 2
        $containerEntity = new Container();
115 2
        $containerEntity->setName($name);
116 2
        foreach ($parameters as $key => $value) {
117
            switch ($key) {
118 2
                case 'height':
119 1
                    $containerEntity->setHeight($value);
120 1
                    break;
121 2
                case 'width':
122 1
                    $containerEntity->setWidth($value);
123 1
                    break;
124 2
                case 'cssClass':
125 1
                    $containerEntity->setCssClass($value);
126 1
                    break;
127 2
                case 'styles':
128 1
                    $containerEntity->setStyles($value);
129 1
                    break;
130 2
                case 'visible':
131 1
                    $containerEntity->setVisible($value);
132 1
                    break;
133 2
                case 'data':
134 2
                    foreach ($value as $dataKey => $dataValue) {
135 2
                        $containerData = new ContainerData($dataKey, $dataValue);
136 2
                        $containerData->setContainer($containerEntity);
137 2
                        $this->objectManager->persist($containerData);
138 2
                        $containerEntity->addData($containerData);
139
                    }
140
            }
141
        }
142 2
        $this->objectManager->persist($containerEntity);
143 2
        $this->objectManager->flush();
144
145 2
        $this->eventDispatcher
146 2
            ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($containerEntity));
147
148 2
        return $containerEntity;
149
    }
150
}
151