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

WidgetMapMigrationCommand::execute()   D

Complexity

Conditions 35
Paths 10

Size

Total Lines 206
Code Lines 148

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 2
Metric Value
c 9
b 1
f 2
dl 0
loc 206
rs 4.297
cc 35
eloc 148
nc 10
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Victoire\Bundle\WidgetMapBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Victoire\Bundle\CoreBundle\Entity\View;
10
use Victoire\Bundle\TemplateBundle\Entity\Template;
11
use Victoire\Bundle\WidgetBundle\Entity\Widget;
12
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
13
14
class WidgetMapMigrationCommand extends ContainerAwareCommand
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function configure()
20
    {
21
        parent::configure();
22
23
        $this
24
            ->setName('victoire:widget-map:migrate')
25
            ->addOption('view', null, InputArgument::OPTIONAL, 'view id', null)
26
            ->setDescription('persists widget map as a tree ofWidgetMap objects');
27
    }
28
29
    /**
30
     * Takes each view widgetmap array and convert it to persisted WidgetMaps.
31
     *
32
     * sort widget in inversed order to generate a reversed tree like:
33
     *       4
34
     *     3 ┴
35
     *   2 ┴
36
     * 1 ┴
37
     *
38
     * Then add the overwrited widgetmaps as:
39
     *       4
40
     *     3 ┴ 7
41
     *   2 ┴ 5
42
     * 1 ┴   ┴ 6
43
     *         ┴ 8
44
     *
45
     * @param InputInterface  $input
46
     * @param OutputInterface $output
47
     *
48
     * @return void
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
53
54
        $widgetRepo = $em->getRepository('VictoireWidgetBundle:Widget');
55
        $widgetMapRepo = $em->getRepository('VictoireWidgetMapBundle:WidgetMap');
0 ignored issues
show
Unused Code introduced by
$widgetMapRepo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
        if ($viewId = $input->getOption('view')) {
58
            $views = [$em->getRepository('VictoireCoreBundle:View')->find($viewId)];
59
        } else {
60
            $templateRepo = $em->getRepository('VictoireTemplateBundle:Template');
61
            $rootTemplates = $templateRepo->getInstance()
62
                ->where('template.template IS NULL')
63
                ->getQuery()
64
                ->getResult();
65
            $templates = [];
66
            $recursiveGetTemplates = function ($template) use (&$recursiveGetTemplates, &$templates) {
67
                    array_push($templates, $template);
68
                    foreach ($template->getInheritors() as $template) {
69
                        if ($template instanceof Template) {
70
                            $recursiveGetTemplates($template);
71
                        }
72
                    }
73
            };
74
75
            foreach ($rootTemplates as $rootTemplate) {
76
                $recursiveGetTemplates($rootTemplate);
77
            }
78
79
            $pageRepo = $em->getRepository('VictoirePageBundle:BasePage');
80
            $pages = $pageRepo->findAll();
81
            $errorRepo = $em->getRepository('VictoireTwigBundle:ErrorPage');
82
            $errorPages = $errorRepo->findAll();
83
84
            $views = array_merge($templates, array_merge($pages, $errorPages));
85
        }
86
87
        /** @var View $view */
88
        foreach ($views as $view) {
89
            $this->getContainer()->get('victoire_widget_map.builder')->build($view);
90
            $widgets = [];
91
            foreach ($view->getWidgets() as $widget) {
92
                $widgets[$widget->getId()] = $widget;
93
            }
94
95
            $oldWidgetMaps = $view->getWidgetMap();
96
            if (!empty($oldWidgetMaps)) {
97
                foreach ($oldWidgetMaps as $slot => $oldWidgetMap) {
98
                    $widgetMaps = [];
99
                    var_dump($oldWidgetMap);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($oldWidgetMap); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
100
                    usort($oldWidgetMap, function ($a, $b) {
101
                        if ($b['position'] - $a['position'] == 0) {
102
                            return 1;
103
                        }
104
105
                        return $b['position'] - $a['position'];
106
                    });
107
                    var_dump($oldWidgetMap);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($oldWidgetMap); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
108
109
                    foreach ($oldWidgetMap as $key => $item) {
110
                        if ($item['action'] !== 'create') {
111
                            unset($oldWidgetMap[$key]);
112
                            $oldWidgetMap[] = $item;
113
                        }
114
                    }
115
116
                    foreach ($oldWidgetMap as $key => $item) {
117
                        if ($item['positionReference'] != null) {
118
                            foreach ($oldWidgetMap as $_key => $_item) {
119
                                if ($_item['widgetId'] == $item['positionReference']) {
120
                                    array_splice($oldWidgetMap[$_key], 0, 0, [$item]);
121
                                    unset($oldWidgetMap[$key]);
122
                                }
123
                            }
124
                        }
125
                    }
126
127
                    var_dump($oldWidgetMap);
128
                    foreach ($oldWidgetMap as $position => $_oldWidgetMap) {
129
                        var_dump('==========================');
130
                        var_dump($slot);
131
                        var_dump($_oldWidgetMap);
132
133
                        $widget = $widgetRepo->find($_oldWidgetMap['widgetId']);
134
                        if (!$widget) {
135
                            var_dump('widget does not exists');
136
                            continue;
137
                        }
138
                        $widgetMap = new WidgetMap();
139
                        $referencedWidgetMap = null;
140
                        if ($_oldWidgetMap['positionReference'] != 0 && $_oldWidgetMap['positionReference'] != null) {
141
                            var_dump('has positionReference');
142
                            $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']);
143
                            var_dump($referencedWidget->getId());
144
                            $referencedWidgetMap = $view->getWidgetMapByWidget($referencedWidget);
145
                            while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) {
146
                                $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER);
147
                            }
148
                            var_dump('set parent'.$referencedWidgetMap->getWidget()->getId());
149
                            $widgetMap->setParent($referencedWidgetMap);
150
                            $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
151
                        } else {
152
                            var_dump('has no positionReference');
153
                            if ($position == 0) {
154
                                if (!isset($view->getBuiltWidgetMap()[$slot])) {
155
                                    $widgetMap->setPosition(null);
156
                                    var_dump('set parent'.null);
157
                                    $widgetMap->setParent(null);
158
                                } else {
159
                                    $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
160
161
                                    $_rootBuilt = null;
162
163
                                    foreach ($view->getBuiltWidgetMap()[$slot] as $_built) {
164
                                        if (!$_built->getParent()) {
165
                                            $_rootBuilt = $_built;
166
                                            break;
167
                                        }
168
                                    }
169
                                    while (null !== $child = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE)) {
170
                                        $_rootBuilt = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE);
171
                                    }
172
                                    $widgetMap->setParent($_rootBuilt);
173
                                }
174
                            } else {
175
                                $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
176
                                if (!empty(array_slice($widgetMaps, -1))) {
177
                                    $widgetMap->setParent(array_slice($widgetMaps, -1)[0]);
178
                                }
179
                            }
180
                        }
181
182
                        if (WidgetMap::ACTION_OVERWRITE == $_oldWidgetMap['action']) {
183
                            var_dump('is overwrite');
184
185
                            /* @var Widget $replacedWidget */
186
                            if ($_oldWidgetMap['replacedWidgetId']) {
187
                                var_dump('has replacedWidgetId');
188
                                $replacedWidget = $widgetRepo->find($_oldWidgetMap['replacedWidgetId']);
189
                                $supplicantWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
190
                                $replacedWidgetView = $replacedWidget->getView();
191
                                $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
192
                                $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget);
193
                                // If replaced widgetMap does not exists, this is not an overwrite but a create
194
                                if ($replacedWidgetMap) {
195
                                    var_dump('has replacedWidgetMap');
196
                                    $widgetMap->setReplaced($replacedWidgetMap);
197
                                    var_dump('replace '.$replacedWidget->getId().' by '.$supplicantWidget->getId());
198
                                    $widgetMap->setWidget($supplicantWidget);
199
                                    $widgetMap->setPosition($replacedWidgetMap->getPosition());
200
                                    var_dump('set parent'.($replacedWidgetMap->getParent() ? $replacedWidgetMap->getParent()->getWidget()->getId() : null));
201
                                    $widgetMap->setParent($replacedWidgetMap->getParent());
202
                                }
203
                            } elseif ($referencedWidgetMap) {
204
                                var_dump('move');
205
206
                                $this->getContainer()->get('victoire_widget_map.manager')->move($view, [
207
                                    'position'           => WidgetMap::POSITION_AFTER,
208
                                    'slot'               => $slot,
209
                                    'widgetMapReference' => $referencedWidgetMap,
210
                                    'widgetMap'          => $_oldWidgetMap['widgetId'],
211
                                ]);
212
                            } else {
213
                                $_oldWidgetMap['action'] = WidgetMap::ACTION_CREATE;
214
                            }
215
                        } elseif (WidgetMap::ACTION_DELETE == $_oldWidgetMap['action']) {
216
                            var_dump('is delete');
217
                            $replacedWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
218
                            $widgetMap->setPosition(null);
219
                            var_dump('set parent'.null);
220
                            $widgetMap->setParent(null);
221
                            $deletedWidgetMap = $view->getWidgetMapByWidget($replacedWidget);
222
                            if ($deletedWidgetMap) {
223
                                $replacedWidgetView = $replacedWidget->getView();
224
                                $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
225
                                $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget);
226
                                $widgetMap->setReplaced($replacedWidgetMap);
227
228
                                $this->getContainer()->get('victoire_widget_map.manager')->moveChildren(
229
                                    $view,
230
                                    $deletedWidgetMap->getChild(WidgetMap::POSITION_BEFORE),
231
                                    $deletedWidgetMap->getChild(WidgetMap::POSITION_AFTER),
232
                                    $deletedWidgetMap->getParent(),
233
                                    $deletedWidgetMap->getPosition()
234
                                );
235
                            } else {
236
                                continue;
237
                            }
238
                        }
239
240
                        $widgetMap->setAction($_oldWidgetMap['action']);
241
                        $widgetMap->setWidget($widget);
242
                        $widgetMap->setAsynchronous($_oldWidgetMap['asynchronous'] ? true : false);
243
                        $widgetMap->setSlot($slot);
244
                        var_dump('add widgetMap for widget '.$widgetMap->getWidget()->getId().' to view '.$view->getId());
245
                        $view->addWidgetMap($widgetMap);
246
                        $em->persist($widgetMap);
247
                        $widgetMaps[] = $widgetMap;
248
249
                        $this->getContainer()->get('victoire_widget_map.builder')->build($view);
250
                    }
251
                }
252
            }
253
            $em->flush();
254
        }
255
    }
256
}
257