Completed
Pull Request — master (#325)
by Paul
09:55
created

WidgetMapMigrationCommand   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 13
Bugs 2 Features 2
Metric Value
wmc 36
c 13
b 2
f 2
lcom 0
cbo 8
dl 0
loc 239
rs 8.8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
D execute() 0 202 35
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
                    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
108
                    foreach ($oldWidgetMap as $key => $item) {
109
                        if ($item['action'] !== 'create') {
110
                            unset($oldWidgetMap[$key]);
111
                            $oldWidgetMap[] = $item;
112
                        }
113
                    }
114
115
                    foreach ($oldWidgetMap as $key => $item) {
116
                        if ($item['positionReference'] != null) {
117
                            foreach ($oldWidgetMap as $_key => $_item) {
118
                                if ($_item['widgetId'] == $item['positionReference']) {
119
                                    array_splice($oldWidgetMap[$_key], 0, 0, [$item]);
120
                                    unset($oldWidgetMap[$key]);
121
                                }
122
                            }
123
                        }
124
                    }
125
126
                    foreach ($oldWidgetMap as $position => $_oldWidgetMap) {
127
                        $output->writeln('==========================');
128
                        $output->writeln($slot);
129
130
                        $widget = $widgetRepo->find($_oldWidgetMap['widgetId']);
131
                        if (!$widget) {
132
                            $output->writeln('widget does not exists');
133
                            continue;
134
                        }
135
                        $widgetMap = new WidgetMap();
136
                        $referencedWidgetMap = null;
137
                        if ($_oldWidgetMap['positionReference'] != 0 && $_oldWidgetMap['positionReference'] != null) {
138
                            $output->writeln('has positionReference');
139
                            $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']);
140
                            $output->writeln($referencedWidget->getId());
141
                            $referencedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($referencedWidget, $view);
142
                            while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) {
143
                                $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER);
144
                            }
145
                            $output->writeln('set parent'.$referencedWidgetMap->getWidget()->getId());
146
                            $widgetMap->setParent($referencedWidgetMap);
147
                            $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
148
                        } else {
149
                            $output->writeln('has no positionReference');
150
                            if ($position == 0) {
151
                                if (!isset($view->getBuiltWidgetMap()[$slot])) {
152
                                    $widgetMap->setPosition(null);
153
                                    $output->writeln('set parent'.null);
154
                                    $widgetMap->setParent(null);
155
                                } else {
156
                                    $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
157
158
                                    $_rootBuilt = null;
159
160
                                    foreach ($view->getBuiltWidgetMap()[$slot] as $_built) {
161
                                        if (!$_built->getParent()) {
162
                                            $_rootBuilt = $_built;
163
                                            break;
164
                                        }
165
                                    }
166
                                    while (null !== $child = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE)) {
167
                                        $_rootBuilt = $_rootBuilt->getChild(WidgetMap::POSITION_BEFORE);
168
                                    }
169
                                    $widgetMap->setParent($_rootBuilt);
170
                                }
171
                            } else {
172
                                $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
173
                                if (!empty(array_slice($widgetMaps, -1))) {
174
                                    $widgetMap->setParent(array_slice($widgetMaps, -1)[0]);
175
                                }
176
                            }
177
                        }
178
179
                        if (WidgetMap::ACTION_OVERWRITE == $_oldWidgetMap['action']) {
180
                            $output->writeln('is overwrite');
181
182
                            /* @var Widget $replacedWidget */
183
                            if ($_oldWidgetMap['replacedWidgetId']) {
184
                                $output->writeln('has replacedWidgetId');
185
                                $replacedWidget = $widgetRepo->find($_oldWidgetMap['replacedWidgetId']);
186
                                $supplicantWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
187
                                $replacedWidgetView = $replacedWidget->getView();
188
                                $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
189
                                $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget);
190
                                // If replaced widgetMap does not exists, this is not an overwrite but a create
191
                                if ($replacedWidgetMap) {
192
                                    $output->writeln('has replacedWidgetMap');
193
                                    $widgetMap->setReplaced($replacedWidgetMap);
194
                                    $output->writeln('replace '.$replacedWidget->getId().' by '.$supplicantWidget->getId());
195
                                    $widgetMap->setWidget($supplicantWidget);
196
                                    $widgetMap->setPosition($replacedWidgetMap->getPosition());
197
                                    $output->writeln('set parent'.($replacedWidgetMap->getParent() ? $replacedWidgetMap->getParent()->getWidget()->getId() : null));
198
                                    $widgetMap->setParent($replacedWidgetMap->getParent());
199
                                }
200
                            } elseif ($referencedWidgetMap) {
201
                                $output->writeln('move');
202
203
                                $this->getContainer()->get('victoire_widget_map.manager')->move($view, [
204
                                    'position'           => WidgetMap::POSITION_AFTER,
205
                                    'slot'               => $slot,
206
                                    'parentWidgetMap' => $referencedWidgetMap,
207
                                    'widgetMap'          => $_oldWidgetMap['widgetId'],
208
                                ]);
209
                            } else {
210
                                $_oldWidgetMap['action'] = WidgetMap::ACTION_CREATE;
211
                            }
212
                        } elseif (WidgetMap::ACTION_DELETE == $_oldWidgetMap['action']) {
213
                            $output->writeln('is delete');
214
                            $replacedWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
215
                            $widgetMap->setPosition(null);
216
                            $output->writeln('set parent'.null);
217
                            $widgetMap->setParent(null);
218
                            $deletedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($replacedWidget, $view);
219
                            if ($deletedWidgetMap) {
220
                                $replacedWidgetView = $replacedWidget->getView();
221
                                $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
222
                                $replacedWidgetMap = $replacedWidgetView->getWidgetMapByWidget($replacedWidget);
223
                                $widgetMap->setReplaced($replacedWidgetMap);
224
225
                                $this->getContainer()->get('victoire_widget_map.manager')->moveChildren(
226
                                    $view,
227
                                    $deletedWidgetMap->getChild(WidgetMap::POSITION_BEFORE),
228
                                    $deletedWidgetMap->getChild(WidgetMap::POSITION_AFTER),
229
                                    $deletedWidgetMap->getParent(),
230
                                    $deletedWidgetMap->getPosition()
231
                                );
232
                            } else {
233
                                continue;
234
                            }
235
                        }
236
237
                        $widgetMap->setAction($_oldWidgetMap['action']);
238
                        $widgetMap->setWidget($widget);
239
                        $widgetMap->setAsynchronous($_oldWidgetMap['asynchronous'] ? true : false);
240
                        $widgetMap->setSlot($slot);
241
                        $output->writeln('add widgetMap for widget '.$widgetMap->getWidget()->getId().' to view '.$view->getId());
242
                        $view->addWidgetMap($widgetMap);
243
                        $em->persist($widgetMap);
244
                        $widgetMaps[] = $widgetMap;
245
246
                        $this->getContainer()->get('victoire_widget_map.builder')->build($view);
247
                    }
248
                }
249
            }
250
            $em->flush();
251
        }
252
    }
253
}
254