Completed
Pull Request — master (#325)
by Paul
08:40
created

WidgetMapManager::overwrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 11
nc 1
nop 3
1
<?php
2
3
namespace Victoire\Bundle\WidgetMapBundle\Manager;
4
5
use Doctrine\Orm\EntityManager;
6
use Victoire\Bundle\CoreBundle\Entity\View;
7
use Victoire\Bundle\WidgetBundle\Entity\Widget;
8
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder;
9
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
10
11
class WidgetMapManager
12
{
13
    private $em;
14
    private $builder;
15
16
    public function __construct(EntityManager $em, WidgetMapBuilder $builder)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
17
    {
18
        $this->em = $em;
19
        $this->builder = $builder;
20
    }
21
22
    /**
23
     * @param string $slotId
24
     */
25
    public function insert(Widget $widget, View $view, $slotId, $position, $widgetReference)
26
    {
27
        $parent = null;
28
        if ($widgetReference) {
29
            $parent = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find($widgetReference);
30
        }
31
        //create the new widget map
32
        $widgetMapEntry = new WidgetMap();
33
        $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE);
34
        $widgetMapEntry->setWidget($widget);
35
        $widgetMapEntry->setSlot($slotId);
36
        $widgetMapEntry->setPosition($position);
37
        $widgetMapEntry->setParent($parent);
0 ignored issues
show
Bug introduced by
It seems like $parent defined by $this->em->getRepository...>find($widgetReference) on line 29 can also be of type object; however, Victoire\Bundle\WidgetMa...\WidgetMap::setParent() does only seem to accept null|object<Victoire\Bun...undle\Entity\WidgetMap>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
38
39
        $view->addWidgetMap($widgetMapEntry);
40
    }
41
42
    /**
43
     * moves a widget in a view.
44
     *
45
     * @param View  $view
46
     * @param array $sortedWidget
47
     *
48
     * @returns void
49
     */
50
    public function move(View $view, $sortedWidget)
51
    {
52
        /** @var WidgetMap $widgetMapReference */
53
        $widgetMapReference = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['widgetMapReference']);
54
        $position = $sortedWidget['position'];
55
        $slot = $sortedWidget['slot'];
56
        /** @var WidgetMap $widgetMap */
57
        $widgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['widgetMap']);
58
59
        $originalParent = $widgetMap->getParent();
60
        $originalPosition = $widgetMap->getPosition();
61
62
        $children = $widgetMap->getChildren($view);
63
        $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
64
        $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
65
66
        $widgetMapReferenceChildren = $this->getChildrenByView($widgetMapReference);
67
68
        $widgetMap = $this->moveWidgetMap($view, $widgetMap, $widgetMapReference, $position, $slot);
0 ignored issues
show
Documentation introduced by
$widgetMapReference is of type object<Victoire\Bundle\W...undle\Entity\WidgetMap>, 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...
69
70
        // If the moved widgetMap has someone at both his before and after, arbitrary move UP the before side
71
        // and find the first place after the before widgetMap hierarchy to place the after widgetMap.
72 View Code Duplication
        if ($beforeChild && $afterChild) {
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...
73
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
0 ignored issues
show
Documentation introduced by
$originalParent is of type object<Victoire\Bundle\W...\Entity\WidgetMap>|null, 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...
Documentation introduced by
$originalPosition 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...
74
75
            $child = $beforeChild;
76
            while ($child->getChild(WidgetMap::POSITION_AFTER)) {
77
                $child = $child->getChild(WidgetMap::POSITION_AFTER);
78
            }
79
            if ($afterChild->getId() !== $child->getId()) {
80
                $this->moveWidgetMap($view, $afterChild, $child);
81
            }
82
        } elseif ($beforeChild) {
83
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
0 ignored issues
show
Documentation introduced by
$originalParent is of type object<Victoire\Bundle\W...\Entity\WidgetMap>|null, 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...
Documentation introduced by
$originalPosition 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...
84
        } elseif ($afterChild) {
85
            $this->moveWidgetMap($view, $afterChild, $originalParent, $originalPosition);
0 ignored issues
show
Documentation introduced by
$originalParent is of type object<Victoire\Bundle\W...\Entity\WidgetMap>|null, 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...
Documentation introduced by
$originalPosition 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...
86
        }
87
88
        foreach ($widgetMapReferenceChildren['views'] as $_view) {
89
            if ($_view->getId() !== $view->getId()) {
90 View Code Duplication
                if (isset($widgetMapReferenceChildren['before'][$_view->getId()])) {
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...
91
                    $widgetMapReferenceChildren['before'][$_view->getId()]->setParent($widgetMap);
92
                }
93 View Code Duplication
                if (isset($widgetMapReferenceChildren['after'][$_view->getId()])) {
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...
94
                    $widgetMapReferenceChildren['after'][$_view->getId()]->setParent($widgetMap);
95
                }
96
            }
97
        }
98
    }
99
100
    /**
101
     * Delete the widget from the view.
102
     *
103
     * @param View   $view
104
     * @param Widget $widget
105
     *
106
     * @throws \Exception Widget map does not exists
107
     */
108
    public function delete(View $view, Widget $widget)
109
    {
110
        $this->builder->build($view);
111
112
        $widgetMap = $view->getWidgetMapByWidget($widget);
113
        $slot = $widgetMap->getSlot();
114
115
        $originalParent = $widgetMap->getParent();
116
        $originalPosition = $widgetMap->getPosition();
117
118
        $children = $widgetMap->getChildren($view);
119
        $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
120
        $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
121
122
        //we remove the widget from the current view
123
        if ($widgetMap->getView() === $view) {
124
            //remove the widget map from the slot
125
            $view->removeWidgetMap($widgetMap);
126
        } else {
127
            //the widget is owned by another view (a parent)
128
            //so we add a new widget map that indicates we delete this widget
129
            $replaceWidgetMap = new WidgetMap();
130
            $replaceWidgetMap->setAction(WidgetMap::ACTION_DELETE);
131
            $replaceWidgetMap->setWidget($widget);
132
            $replaceWidgetMap->setSlot($slot);
133
            $replaceWidgetMap->setReplaced($widgetMap);
134
135
            $view->addWidgetMap($replaceWidgetMap);
136
        }
137
138
        $this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);
139
    }
140
141
    /**
142
     * the widget is owned by another view (a parent)
143
     * so we add a new widget map that indicates we delete this widget.
144
     *
145
     * @param View      $view
146
     * @param WidgetMap $originalWidgetMap
147
     * @param Widget    $widgetCopy
148
     *
149
     * @throws \Exception
150
     */
151
    public function overwrite(View $view, WidgetMap $originalWidgetMap, Widget $widgetCopy)
152
    {
153
        $widgetMap = new WidgetMap();
154
        $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
155
        $widgetMap->setReplaced($originalWidgetMap);
156
        $widgetMap->setWidget($widgetCopy);
157
        $widgetMap->setView($view);
158
        $widgetMap->setSlot($originalWidgetMap->getSlot());
159
        $widgetMap->setPosition($originalWidgetMap->getPosition());
160
        $widgetMap->setAsynchronous($widgetCopy->isAsynchronous());
161
        $widgetMap->setParent($originalWidgetMap->getParent());
162
163
        $view->addWidgetMap($widgetMap);
164
    }
165
166
    /**
167
     * If the moved widgetMap has someone at both his before and after, arbitrary move UP the before side
168
     * and find the first place after the before widgetMap hierarchy to place the after widgetMap.
169
     *
170
     * @param View $view
171
     * @param $beforeChild
172
     * @param $afterChild
173
     * @param $originalParent
174
     * @param $originalPosition
175
     */
176
    public function moveChildren(View $view, $beforeChild, $afterChild, $originalParent, $originalPosition)
177
    {
178 View Code Duplication
        if ($beforeChild && $afterChild) {
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...
179
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
180
181
            $child = $beforeChild;
182
            while ($child->getChild(WidgetMap::POSITION_AFTER)) {
183
                $child = $child->getChild(WidgetMap::POSITION_AFTER);
184
            }
185
            if ($afterChild->getId() !== $child->getId()) {
186
                $this->moveWidgetMap($view, $afterChild, $child);
187
            }
188
        } elseif ($beforeChild) {
189
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
190
        } elseif ($afterChild) {
191
            $this->moveWidgetMap($view, $afterChild, $originalParent, $originalPosition);
192
        }
193
    }
194
195
    protected function cloneWidgetMap(WidgetMap $widgetMap, View $view)
196
    {
197
        $originalWidgetMap = $widgetMap;
198
        $widgetMap = clone $widgetMap;
199
        $widgetMap->setId(null);
200
        $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
201
        $widgetMap->setReplaced($originalWidgetMap);
202
        $originalWidgetMap->addSubstitute($widgetMap);
203
        $view->addWidgetMap($widgetMap);
204
        $this->em->persist($widgetMap);
205
206
        return $widgetMap;
207
    }
208
209
    protected function moveWidgetMap(View $view, WidgetMap $widgetMap, $parent = false, $position = false, $slot = false)
210
    {
211
        if ($widgetMap->getView() !== $view) {
212
            $widgetMap = $this->cloneWidgetMap($widgetMap, $view);
213
        }
214
215
        if ($parent !== false) {
216
            if ($oldParent = $widgetMap->getParent()) {
217
                $oldParent->removeChild($widgetMap);
218
            }
219
            $widgetMap->setParent($parent);
0 ignored issues
show
Documentation introduced by
$parent is of type boolean, but the function expects a null|object<Victoire\Bun...undle\Entity\WidgetMap>.

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...
220
            if ($parent) {
221
                $parent->addChild($widgetMap);
0 ignored issues
show
Bug introduced by
The method addChild cannot be called on $parent (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
222
            }
223
        }
224
        if ($position !== false) {
225
            $widgetMap->setPosition($position);
0 ignored issues
show
Documentation introduced by
$position is of type boolean, but the function expects a string.

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...
226
        }
227
        if ($slot !== false) {
228
            $widgetMap->setSlot($slot);
0 ignored issues
show
Documentation introduced by
$slot is of type boolean, but the function expects a string.

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...
229
        }
230
231
        return $widgetMap;
232
    }
233
234
    public function getChildrenByView(WidgetMap $widgetMap)
235
    {
236
        $beforeChilds = $widgetMap->getChilds(WidgetMap::POSITION_BEFORE);
237
        $afterChilds = $widgetMap->getChilds(WidgetMap::POSITION_AFTER);
238
239
        $childrenByView['views'] = [];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$childrenByView was never initialized. Although not strictly required by PHP, it is generally a good practice to add $childrenByView = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
240
        $childrenByView['before'] = [];
241
        $childrenByView['after'] = [];
242
        foreach ($beforeChilds as $beforeChild) {
243
            $view = $beforeChild->getView();
244
            $childrenByView['views'][] = $view;
245
            $childrenByView['before'][$view->getId()] = $beforeChild;
246
        }
247
        foreach ($afterChilds as $afterChild) {
248
            $view = $afterChild->getView();
249
            $childrenByView['views'][] = $view;
250
            $childrenByView['after'][$view->getId()] = $afterChild;
251
        }
252
253
        return $childrenByView;
254
    }
255
}
256