Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

ContainerService   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 157
Duplicated Lines 13.38 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 9.85%

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 12
dl 21
loc 157
rs 10
ccs 7
cts 71
cp 0.0985

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B createContainer() 7 41 8
A updateContainer() 7 25 5
C linkUnlinkWidget() 7 41 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
82
                    break;
83
                case 'styles':
84
                    $container->setStyles($value);
85
86
                    break;
87
                case 'visible':
88
                    $container->setVisible($value);
89
90
                    break;
91
                case 'data':
92 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...
93
                        /** @var ContainerDataInterface $containerData */
94
                        $containerData = $containerDataFactory->create($dataKey, $dataValue);
95
                        $containerData->setContainer($container);
96
                        $this->entityManager->persist($containerData);
97
                        $container->addData($containerData);
98
                    }
99
            }
100
        }
101
        $this->entityManager->persist($container);
102
        $this->entityManager->flush();
103
104
        $this->eventDispatcher
105
            ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($container));
106
107
        return $container;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function updateContainer(ContainerInterface $container, array $extraData): ContainerInterface
114
    {
115
        /** @var ContainerDataFactoryInterface $containerDataFactory */
116
        $containerDataFactory = $this->serviceContainer->get('swp.factory.container_data');
117
        if (!empty($extraData) && is_array($extraData)) {
118
            // Remove old containerData's
119
            foreach ($container->getData() as $containerData) {
120
                $this->entityManager->remove($containerData);
121
            }
122
123
            // Apply new containerData's
124 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...
125
                /** @var ContainerDataInterface $containerData */
126
                $containerData = $containerDataFactory->create($key, $value);
127
                $containerData->setContainer($container);
128
                $this->entityManager->persist($containerData);
129
                $container->addData($containerData);
130
            }
131
        }
132
133
        $this->entityManager->flush();
134
        $this->entityManager->refresh($container);
135
136
        return $container;
137
    }
138
139
    /**
140
     * @param mixed              $object
141
     * @param ContainerInterface $container
142
     * @param Request            $request
143
     *
144
     * @throws \Exception
145
     */
146
    public function linkUnlinkWidget($object, ContainerInterface $container, Request $request)
147
    {
148
        $containerWidget = $this->serviceContainer->get('swp.repository.container_widget')
149
            ->findOneBy([
150
                'widget' => $object,
151
                'container' => $container,
152
            ]);
153
154
        if ('LINK' === $request->getMethod()) {
155
            $position = false;
156 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...
157
                foreach ($notConvertedLinks as $link) {
158
                    if (isset($link['resourceType']) && 'widget-position' == $link['resourceType']) {
159
                        $position = $link['resource'];
160
                    }
161
                }
162
            }
163
164
            if (false === $position && $containerWidget) {
165
                throw new ConflictHttpException('WidgetModel is already linked to container');
166
            }
167
168
            if (!$containerWidget) {
169
                $containerWidget = $this->serviceContainer->get('swp.factory.container_widget')->create($container, $object);
170
                $this->entityManager->persist($containerWidget);
171
            }
172
173
            if (false !== $position) {
174
                $containerWidget->setPosition($position);
175
            }
176
            $container->addWidget($containerWidget);
177
            $this->entityManager->flush();
178
        } elseif ('UNLINK' === $request->getMethod()) {
179
            if (!$container->getWidgets()->contains($containerWidget)) {
180
                throw new ConflictHttpException('WidgetModel is not linked to container');
181
            }
182
            $this->entityManager->remove($containerWidget);
183
        }
184
185
        return $container;
186
    }
187
}
188