Completed
Push — master ( 692aa1...25e5a7 )
by Paweł
60:22
created

ContainerService::getNotConvertedLinks()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 4
Ratio 17.39 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 4
loc 23
ccs 0
cts 0
cp 0
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 1
crap 20
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 Doctrine\ORM\EntityManagerInterface;
18
use SWP\Bundle\TemplatesSystemBundle\Factory\ContainerDataFactoryInterface;
19
use SWP\Component\Common\Request\RequestParser;
20
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerDataInterface;
21
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerInterface;
22
use SWP\Component\Common\Event\HttpCacheEvent;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\DependencyInjection\ContainerInterface as ServiceContainerInterface;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
27
28
/**
29
 * Class ContainerService.
30
 */
31
class ContainerService implements ContainerServiceInterface
32
{
33
    /**
34
     * @var ServiceContainerInterface
35
     */
36
    protected $serviceContainer;
37
38
    /**
39
     * @var EntityManagerInterface
40
     */
41
    protected $entityManager;
42
43
    /**
44
     * @var EventDispatcherInterface
45
     */
46 99
    protected $eventDispatcher;
47
48 99
    /**
49 99
     * ContainerService constructor.
50 99
     *
51 99
     * @param EntityManagerInterface    $entityManager
52 99
     * @param EventDispatcherInterface  $eventDispatcher
53 99
     * @param ServiceContainerInterface $serviceContainer
54
     */
55
    public function __construct(
56
        EntityManagerInterface $entityManager,
57
        EventDispatcherInterface $eventDispatcher,
58
        ServiceContainerInterface $serviceContainer
59
    ) {
60
        $this->entityManager = $entityManager;
61
        $this->eventDispatcher = $eventDispatcher;
62
        $this->serviceContainer = $serviceContainer;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function createContainer($name, array $parameters = [], ContainerInterface $container = null): ContainerInterface
69
    {
70
        if (null === $container) {
71
            /** @var ContainerInterface $containerEntity */
72
            $container = $this->serviceContainer->get('swp.factory.container')->create();
73
        }
74
75
        $containerDataFactory = $this->serviceContainer->get('swp.factory.container_data');
76
        $container->setName($name);
77
        foreach ($parameters as $key => $value) {
78
            switch ($key) {
79
                case 'cssClass':
80
                    $container->setCssClass($value);
81
                    break;
82
                case 'styles':
83
                    $container->setStyles($value);
84
                    break;
85
                case 'visible':
86
                    $container->setVisible($value);
87
                    break;
88
                case 'data':
89 View Code Duplication
                    foreach ($value as $dataKey => $dataValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
                        /** @var ContainerDataInterface $containerData */
91
                        $containerData = $containerDataFactory->create($dataKey, $dataValue);
92
                        $containerData->setContainer($container);
93
                        $this->entityManager->persist($containerData);
94
                        $container->addData($containerData);
95
                    }
96
            }
97
        }
98
        $this->entityManager->persist($container);
99
        $this->entityManager->flush();
100
101
        $this->eventDispatcher
102
            ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($container));
103
104
        return $container;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function updateContainer(ContainerInterface $container, array $extraData): ContainerInterface
111
    {
112
        /** @var ContainerDataFactoryInterface $containerDataFactory */
113
        $containerDataFactory = $this->serviceContainer->get('swp.factory.container_data');
114
        if (!empty($extraData) && is_array($extraData)) {
115
            // Remove old containerData's
116
            foreach ($container->getData() as $containerData) {
117
                $this->entityManager->remove($containerData);
118
            }
119
120
            // Apply new containerData's
121 View Code Duplication
            foreach ($extraData as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
                /** @var ContainerDataInterface $containerData */
123
                $containerData = $containerDataFactory->create($key, $value);
124
                $containerData->setContainer($container);
125
                $this->entityManager->persist($containerData);
126
                $container->addData($containerData);
127
            }
128
        }
129
130
        $this->entityManager->flush();
131
        $this->entityManager->refresh($container);
132
133
        return $container;
134
    }
135
136
    /**
137
     * @param mixed              $object
138
     * @param ContainerInterface $container
139
     * @param Request            $request
140
     *
141
     * @throws \Exception
142
     */
143
    public function linkUnlinkWidget($object, ContainerInterface $container, Request $request)
144
    {
145
        $containerWidget = $this->serviceContainer->get('swp.repository.container_widget')
146
            ->findOneBy([
147
                'widget' => $object,
148
                'container' => $container,
149
            ]);
150
151
        if ($request->getMethod() === 'LINK') {
152
            $position = false;
153 View Code Duplication
            if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
                foreach ($notConvertedLinks as $link) {
155
                    if (isset($link['resourceType']) && $link['resourceType'] == 'widget-position') {
156
                        $position = $link['resource'];
157
                    }
158
                }
159
            }
160
161
            if ($position === false && $containerWidget) {
162
                throw new ConflictHttpException('WidgetModel is already linked to container');
163
            }
164
165
            if (!$containerWidget) {
166
                $containerWidget = $this->serviceContainer->get('swp.factory.container_widget')->create($container, $object);
167
                $this->entityManager->persist($containerWidget);
168
            }
169
170
            if ($position !== false) {
171
                $containerWidget->setPosition($position);
172
            }
173
            $container->addWidget($containerWidget);
174
            $this->entityManager->flush();
175
        } elseif ($request->getMethod() === 'UNLINK') {
176
            if (!$container->getWidgets()->contains($containerWidget)) {
177
                throw new ConflictHttpException('WidgetModel is not linked to container');
178
            }
179
            $this->entityManager->remove($containerWidget);
180
        }
181
182
        return $container;
183
    }
184
}
185