Completed
Pull Request — master (#325)
by Paul
08:12 queued 49s
created

WidgetMapBuilder::getAvailablePosition()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 28
rs 6.7272
cc 7
eloc 17
nc 13
nop 1
1
<?php
2
3
namespace Victoire\Bundle\WidgetMapBundle\Builder;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\CoreBundle\Entity\View;
7
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
8
9
/**
10
 * View WidgetMap builder.
11
 *
12
 * ref: victoire_widget_map.builder
13
 */
14
class WidgetMapBuilder
15
{
16
    /**
17
     * @param View          $view
18
     * @param EntityManager $em
19
     * @param bool          $updatePage
20
     *
21
     * @return array
22
     */
23
    public function build(View $view, EntityManager $em = null, $updatePage = true)
0 ignored issues
show
Unused Code introduced by
The parameter $em is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        $widgetMaps = $view->getWidgetMaps()->toArray();
26
        $template = clone $view;
27
        $builtWidgetMap = [];
28
29
        while (null !== $template = $template->getTemplate()) {
30
            foreach ($template->getWidgetMaps()->toArray() as $item) {
31
                $widgetMaps[] = $item;
32
            }
33
        }
34
35
        $slots = [];
36
        /** @var WidgetMap $widgetMapItem */
37
        foreach ($widgetMaps as $widgetMapItem) {
38
            $id = $widgetMapItem->getId();
39
            if ($widgetMapItem->getReplaced()) {
40
                $id = $widgetMapItem->getReplaced()->getId();
41
            }
42
            if (empty($slots[$widgetMapItem->getSlot()][$id])
43
                || !empty($slots[$widgetMapItem->getSlot()][$id])
44
                && $widgetMapItem->getAction() !== WidgetMap::ACTION_CREATE) {
45
                $slots[$widgetMapItem->getSlot()][$id] = $widgetMapItem;
46
            }
47
        }
48
49
        foreach ($slots as $slot => $widgetMaps) {
50
            foreach ($widgetMaps as $key => $widgetMap) {
51
                if ($widgetMap->getAction() == WidgetMap::ACTION_DELETE) {
52
                    unset($slots[$slot][$key]);
53
                }
54
            }
55
        }
56
57
        foreach ($slots as $slot => $widgetMaps) {
58
            $mainWidgetMap = null;
59
            $builtWidgetMap[$slot] = [];
60
61
            /** @var WidgetMap $_widgetMap */
62
            foreach ($widgetMaps as $_widgetMap) {
63
                if (!$_widgetMap->getParent()) {
64
                    $mainWidgetMap = $_widgetMap;
65
                    break;
66
                }
67
            }
68
69
            if ($mainWidgetMap) {
70
                $builtWidgetMap[$slot][] = $mainWidgetMap;
71
                /*
72
                 * @param WidgetMap $currentWidgetMap
73
                 */
74
                $orderizeWidgetMap = function ($currentWidgetMap, $builtWidgetMap) use ($slot, &$orderizeWidgetMap, $widgetMaps, $view) {
75
                    $children = $currentWidgetMap->getChildren($view);
76
                    foreach ($children as $child) {
77
                        if (in_array($child, $widgetMaps)
78
                        ) {
79
                            $offset = array_search($currentWidgetMap, $builtWidgetMap[$slot]) + ($child->getPosition() == WidgetMap::POSITION_AFTER ? 1 : 0);
80
                            array_splice($builtWidgetMap[$slot], $offset, 0, [$child]);
81
                            $builtWidgetMap = $orderizeWidgetMap($child, $builtWidgetMap);
82
                        }
83
                    }
84
85
                    return $builtWidgetMap;
86
                };
87
                $builtWidgetMap = $orderizeWidgetMap($mainWidgetMap, $builtWidgetMap);
88
            }
89
        }
90
        $_builtWidgetMap = $builtWidgetMap;
91
        $builtWidgetMap = [];
92
        foreach ($_builtWidgetMap as $slot => $__builtWidgetMap) {
93
            foreach ($__builtWidgetMap as $key => $item) {
94
                $builtWidgetMap[$slot][$item->getWidget()->getId()] = $item;
95
            }
96
        }
97
98
        if ($updatePage) {
99
            $view->setBuiltWidgetMap($builtWidgetMap);
0 ignored issues
show
Documentation introduced by
$builtWidgetMap is of type array, 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...
100
        }
101
102
        return $builtWidgetMap;
103
    }
104
105
    public function getAvailablePosition(View $view)
106
    {
107
        $widgetMaps = $view->getBuiltWidgetMap();
108
109
        $availablePositions = [];
110
        foreach ($widgetMaps as $slot => $widgetMap) {
111
            foreach ($widgetMap as $_widgetMap) {
112
                $availablePositions[$slot][$_widgetMap->getId()]['id'] = $_widgetMap->getId();
113
                $availablePositions[$slot][$_widgetMap->getId()][WidgetMap::POSITION_BEFORE] = true;
114
                $availablePositions[$slot][$_widgetMap->getId()][WidgetMap::POSITION_AFTER] = true;
115
                if ($_widgetMap->getReplaced()) {
116
                    $availablePositions[$slot][$_widgetMap->getId()]['replaced'] = $_widgetMap->getReplaced()->getId();
117
                }
118
            }
119
            /** @var WidgetMap $_widgetMap */
120
            foreach ($widgetMap as $_widgetMap) {
121
                if ($_widgetMap->getParent()) {
122
                    if ($substitute = $_widgetMap->getParent()->getSubstituteForView($view)) {
123
                        $availablePositions[$slot][$substitute->getId()][$_widgetMap->getPosition()] = false;
124
                    } else {
125
                        $availablePositions[$slot][$_widgetMap->getParent()->getId()][$_widgetMap->getPosition()] = false;
126
                    }
127
                }
128
            }
129
        }
130
131
        return $availablePositions;
132
    }
133
}
134