Completed
Pull Request — master (#387)
by Paul
06:29
created

MigrateWidgetOwningSideCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 14
nc 3
nop 2
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Create a new Widget for VictoireCMS.
12
 */
13
class MigrateWidgetOwningSideCommand extends ContainerAwareCommand
14
{
15
    protected $skeletonDirs;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function configure()
21
    {
22
        parent::configure();
23
24
        $this
25
            ->setName('victoire:widget:migrate-owning-side')
26
            ->setDescription('widget is now the owning side of Widget<=>WidgetMap relation');
27
    }
28
29
    /**
30
     * @param InputInterface  $input
31
     * @param OutputInterface $output
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        /** @var EntityManager $entityManager */
36
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
37
38
        $widgetMaps = $entityManager->getRepository('VictoireWidgetMapBundle:WidgetMap')->findAll();
39
40
        $progress = $this->getHelper('progress');
41
        $progress->start($output, count($widgetMaps));
42
        foreach ($widgetMaps as $key => $widgetMap) {
43
            $widget = $widgetMap->getWidget();
44
            $widget->setWidgetMap($widgetMap);
45
            $widgetMap->setWidget(null);
46
            if ($key % 100 == 0) {
47
                $entityManager->flush();
48
            }
49
            $progress->advance();
50
        }
51
52
        $entityManager->flush();
53
54
        $progress->finish();
55
    }
56
}
57