Completed
Push — master ( 7b108e...bec163 )
by Paul
11s
created

MigrateWidgetOwningSideCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 5
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 23 3
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