Completed
Pull Request — master (#325)
by Paul
07:18
created

WidgetMapMigrationCommand::execute()   D

Complexity

Conditions 35
Paths 10

Size

Total Lines 206
Code Lines 148

Duplication

Lines 0
Ratio 0 %

Importance

Changes 12
Bugs 2 Features 2
Metric Value
c 12
b 2
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 Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Victoire\Bundle\CoreBundle\Entity\View;
11
use Victoire\Bundle\TemplateBundle\Entity\Template;
12
use Victoire\Bundle\WidgetBundle\Entity\Widget;
13
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
14
15
class WidgetMapMigrationCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function configure()
21
    {
22
        parent::configure();
23
24
        $this
25
            ->setName('victoire:widget-map:migrate')
26
            ->addOption('view', null, InputArgument::OPTIONAL, 'view id', null)
27
            ->setDescription('persists widget map as a tree ofWidgetMap objects');
28
    }
29
30
    /**
31
     * Takes each view widgetmap array and convert it to persisted WidgetMaps.
32
     *
33
     * sort widget in inversed order to generate a reversed tree like:
34
     *       4
35
     *     3 ┴
36
     *   2 ┴
37
     * 1 ┴
38
     *
39
     * Then add the overwrited widgetmaps as:
40
     *       4
41
     *     3 ┴ 7
42
     *   2 ┴ 5
43
     * 1 ┴   ┴ 6
44
     *         ┴ 8
45
     *
46
     * @param InputInterface  $input
47
     * @param OutputInterface $output
48
     *
49
     * @return void
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
54
55
        $widgetRepo = $em->getRepository('VictoireWidgetBundle:Widget');
56
        $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...
57
58
        if ($viewId = $input->getOption('view')) {
59
            $views = [$em->getRepository('VictoireCoreBundle:View')->find($viewId)];
60
        } else {
61
            $templateRepo = $em->getRepository('VictoireTemplateBundle:Template');
62
            $rootTemplates = $templateRepo->getInstance()
63
                ->where('template.template IS NULL')
64
                ->getQuery()
65
                ->getResult();
66
            $templates = [];
67
            $recursiveGetTemplates = function($template) use (&$recursiveGetTemplates, &$templates) {
68
                    array_push($templates, $template);
69
                    foreach ($template->getInheritors() as $template) {
70
                        if ($template instanceof Template) {
71
                            $recursiveGetTemplates($template);
72
                        }
73
                    }
74
            };
75
76
            foreach ($rootTemplates as $rootTemplate) {
77
                $recursiveGetTemplates($rootTemplate);
78
            }
79
80
            $pageRepo = $em->getRepository('VictoirePageBundle:BasePage');
81
            $pages = $pageRepo->findAll();
82
            $errorRepo = $em->getRepository('VictoireTwigBundle:ErrorPage');
83
            $errorPages = $errorRepo->findAll();
84
85
            $views = array_merge($templates, array_merge($pages, $errorPages));
86
        }
87
88
        /** @var View $view */
89
        foreach ($views as $view) {
90
            $this->getContainer()->get('victoire_widget_map.builder')->build($view);
91
            $widgets = [];
92
            foreach ($view->getWidgets() as $widget) {
93
                $widgets[$widget->getId()] = $widget;
94
            }
95
96
            $oldWidgetMaps = $view->getWidgetMap();
97
            if (!empty($oldWidgetMaps)) {
98
                foreach ($oldWidgetMaps as $slot => $oldWidgetMap) {
99
                    $widgetMaps = [];
100
                    $output->writeln(var_export($oldWidgetMap));
101
                    usort($oldWidgetMap, function($a, $b) {
102
                        if ($b['position'] - $a['position'] == 0) {
103
                            return 1;
104
                        }
105
106
                        return $b['position'] - $a['position'];
107
                    });
108
                    $output->writeln(var_export($oldWidgetMap));
109
110
                    foreach ($oldWidgetMap as $key => $item) {
111
                        if ($item['action'] !== 'create') {
112
                            unset($oldWidgetMap[$key]);
113
                            $oldWidgetMap[] = $item;
114
                        }
115
                    }
116
117
                    foreach ($oldWidgetMap as $key => $item) {
118
                        if ($item['positionReference'] != null) {
119
                            foreach ($oldWidgetMap as $_key => $_item) {
120
                                if ($_item['widgetId'] == $item['positionReference']) {
121
                                    array_splice($oldWidgetMap[$_key], 0, 0, [$item]);
122
                                    unset($oldWidgetMap[$key]);
123
                                }
124
                            }
125
                        }
126
                    }
127
128
                    $output->writeln(var_export($oldWidgetMap));
129
                    foreach ($oldWidgetMap as $position => $_oldWidgetMap) {
130
                        $output->writeln('==========================');
131
                        $output->writeln($slot);
132
                        $output->writeln(var_export($_oldWidgetMap));
133
134
                        $widget = $widgetRepo->find($_oldWidgetMap['widgetId']);
135
                        if (!$widget) {
136
                            $output->writeln('widget does not exists');
137
                            continue;
138
                        }
139
                        $widgetMap = new WidgetMap();
140
                        $referencedWidgetMap = null;
141
                        if ($_oldWidgetMap['positionReference'] != 0 && $_oldWidgetMap['positionReference'] != null) {
142
                            $output->writeln('has positionReference');
143
                            $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']);
144
                            $output->writeln($referencedWidget->getId());
145
                            $referencedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($referencedWidget, $view);
146
                            while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) {
147
                                $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER);
148
                            }
149
                            $output->writeln('set parent'.$referencedWidgetMap->getWidget()->getId());
150
                            $widgetMap->setParent($referencedWidgetMap);
151
                            $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
152
                        } else {
153
                            $output->writeln('has no positionReference');
154
                            if ($position == 0) {
155
                                if (!isset($view->getBuiltWidgetMap()[$slot])) {
156
                                    $widgetMap->setPosition(null);
157
                                    $output->writeln('set parent'.null);
158
                                    $widgetMap->setParent(null);
159
                                } else {
160
                                    $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
161
162
                                    $_rootBuilt = null;
163
164
                                    foreach ($view->getBuiltWidgetMap()[$slot] as $_built) {
165
                                        if (!$_built->getParent()) {
166
                                            $_rootBuilt = $_built;
167
                                            break;
168
                                        }
169
                                    }
170
                                    while (null !== $child = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE)) {
171
                                        $_rootBuilt = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE);
172
                                    }
173
                                    $widgetMap->setParent($_rootBuilt);
174
                                }
175
                            } else {
176
                                $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
177
                                if (!empty(array_slice($widgetMaps, -1))) {
178
                                    $widgetMap->setParent(array_slice($widgetMaps, -1)[0]);
179
                                }
180
                            }
181
                        }
182
183
                        if (WidgetMap::ACTION_OVERWRITE == $_oldWidgetMap['action']) {
184
                            $output->writeln('is overwrite');
185
186
                            /* @var Widget $replacedWidget */
187
                            if ($_oldWidgetMap['replacedWidgetId']) {
188
                                $output->writeln('has replacedWidgetId');
189
                                $replacedWidget = $widgetRepo->find($_oldWidgetMap['replacedWidgetId']);
190
                                $supplicantWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
191
                                $replacedWidgetView = $replacedWidget->getView();
192
                                $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
193
                                $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget);
194
                                // If replaced widgetMap does not exists, this is not an overwrite but a create
195
                                if ($replacedWidgetMap) {
196
                                    $output->writeln('has replacedWidgetMap');
197
                                    $widgetMap->setReplaced($replacedWidgetMap);
198
                                    $output->writeln('replace '.$replacedWidget->getId().' by '.$supplicantWidget->getId());
199
                                    $widgetMap->setWidget($supplicantWidget);
200
                                    $widgetMap->setPosition($replacedWidgetMap->getPosition());
201
                                    $output->writeln('set parent'.($replacedWidgetMap->getParent() ? $replacedWidgetMap->getParent()->getWidget()->getId() : null));
202
                                    $widgetMap->setParent($replacedWidgetMap->getParent());
203
                                }
204
                            } elseif ($referencedWidgetMap) {
205
                                $output->writeln('move');
206
207
                                $this->getContainer()->get('victoire_widget_map.manager')->move($view, [
208
                                    'position'           => WidgetMap::POSITION_AFTER,
209
                                    'slot'               => $slot,
210
                                    'parentWidgetMap' => $referencedWidgetMap,
211
                                    'widgetMap'          => $_oldWidgetMap['widgetId'],
212
                                ]);
213
                            } else {
214
                                $_oldWidgetMap['action'] = WidgetMap::ACTION_CREATE;
215
                            }
216
                        } elseif (WidgetMap::ACTION_DELETE == $_oldWidgetMap['action']) {
217
                            $output->writeln('is delete');
218
                            $replacedWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
219
                            $widgetMap->setPosition(null);
220
                            $output->writeln('set parent'.null);
221
                            $widgetMap->setParent(null);
222
                            $deletedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($replacedWidget, $view);
223
                            if ($deletedWidgetMap) {
224
                                $replacedWidgetView = $replacedWidget->getView();
225
                                $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
226
                                $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget);
227
                                $widgetMap->setReplaced($replacedWidgetMap);
228
229
                                $this->getContainer()->get('victoire_widget_map.manager')->moveChildren(
230
                                    $view,
231
                                    $deletedWidgetMap->getChild(WidgetMap::POSITION_BEFORE),
232
                                    $deletedWidgetMap->getChild(WidgetMap::POSITION_AFTER),
233
                                    $deletedWidgetMap->getParent(),
234
                                    $deletedWidgetMap->getPosition()
235
                                );
236
                            } else {
237
                                continue;
238
                            }
239
                        }
240
241
                        $widgetMap->setAction($_oldWidgetMap['action']);
242
                        $widgetMap->setWidget($widget);
243
                        $widgetMap->setAsynchronous($_oldWidgetMap['asynchronous'] ? true : false);
244
                        $widgetMap->setSlot($slot);
245
                        $output->writeln('add widgetMap for widget '.$widgetMap->getWidget()->getId().' to view '.$view->getId());
246
                        $view->addWidgetMap($widgetMap);
247
                        $em->persist($widgetMap);
248
                        $widgetMaps[] = $widgetMap;
249
250
                        $this->getContainer()->get('victoire_widget_map.builder')->build($view);
251
                    }
252
                }
253
            }
254
            $em->flush();
255
        }
256
    }
257
}
258