Completed
Push — master ( 530ce9...1b2ae5 )
by Paweł
23:42 queued 20:31
created

ContainerService   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 6
dl 0
loc 109
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getContainer() 0 28 5
A getRenderer() 0 21 3
D createNewContainer() 0 38 9
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
namespace SWP\Bundle\TemplateEngineBundle\Service;
15
16
use SWP\Bundle\TemplateEngineBundle\Container\SimpleContainer;
17
use SWP\Bundle\TemplateEngineBundle\Model\Container;
18
use SWP\Bundle\TemplateEngineBundle\Model\ContainerData;
19
use SWP\Component\Common\Event\HttpCacheEvent;
20
21
class ContainerService
22
{
23
    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 %} >';
24
    const CLOSE_TAG_TEMPLATE = '</div>';
25
26
    protected $objectManager;
27
    protected $cacheDir;
28
    protected $debug;
29
    protected $renderer = false;
30
    protected $eventDispatcher;
31
32
    public function __construct($doctrine, $cacheDir, $eventDispatcher, $debug = false)
33
    {
34
        $this->objectManager = $doctrine->getManager();
35
        $this->cacheDir = $cacheDir.'/twig';
36
        $this->debug = $debug;
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    public function getContainer($name, array $parameters = [], $createIfNotExists = true)
41
    {
42
        $containerEntity = $this->objectManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\Container')
43
            ->getByName($name)
44
            ->getOneOrNullResult();
45
46
        if (!$containerEntity && $createIfNotExists) {
47
            $containerEntity = $this->createNewContainer($name, $parameters);
48
        } elseif (!$containerEntity) {
49
            throw new \Exception('Container was not found');
50
        }
51
52
        $widgets = [];
53
        $containerWidgets = $this->objectManager->getRepository('SWP\Bundle\TemplateEngineBundle\Model\ContainerWidget')
54
            ->getSortedWidgets(['container' => $containerEntity])
55
            ->getResult();
56
57
        foreach ($containerWidgets as $containerWidget) {
58
            $widgetModel = $containerWidget->getWidget();
59
            $widgetClass = $widgetModel->getType();
60
            $widgets[] = new $widgetClass($widgetModel);
61
        }
62
63
        $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...
64
        $container->setWidgets($widgets);
65
66
        return $container;
67
    }
68
69
    public function getRenderer()
70
    {
71
        if ($this->renderer !== false) {
72
            return $this->renderer;
73
        }
74
75
        $options = [];
76
        if ($this->debug == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
77
            // not debug turn set cache dir
78
            $options['cache'] = $this->cacheDir;
79
        }
80
81
        $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...
82
            new \Twig_Loader_Array([
83
                'open_tag' => self::OPEN_TAG_TEMPLATE,
84
                'close_tag' => self::CLOSE_TAG_TEMPLATE,
85
            ]), $options
86
        );
87
88
        return $this->renderer;
89
    }
90
91
    public function createNewContainer($name, array $parameters = array())
92
    {
93
        $containerEntity = new Container();
94
        $containerEntity->setName($name);
95
        foreach ($parameters as $key => $value) {
96
            switch ($key) {
97
                case 'height':
98
                    $containerEntity->setHeight($value);
99
                    break;
100
                case 'width':
101
                    $containerEntity->setWidth($value);
102
                    break;
103
                case 'cssClass':
104
                    $containerEntity->setCssClass($value);
105
                    break;
106
                case 'styles':
107
                    $containerEntity->setStyles($value);
108
                    break;
109
                case 'visible':
110
                    $containerEntity->setVisible($value);
111
                    break;
112
                case 'data':
113
                    foreach ($value as $dataKey => $dataValue) {
114
                        $containerData = new ContainerData($dataKey, $dataValue);
115
                        $containerData->setContainer($containerEntity);
116
                        $this->objectManager->persist($containerData);
117
                        $containerEntity->addData($containerData);
118
                    }
119
            }
120
        }
121
        $this->objectManager->persist($containerEntity);
122
        $this->objectManager->flush();
123
124
        $this->eventDispatcher
125
            ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($containerEntity));
126
127
        return $containerEntity;
128
    }
129
}
130