Completed
Pull Request — master (#375)
by Paul
06:11
created

MigrateWidgetOwningSideCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
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 24
rs 8.9713
cc 3
eloc 14
nc 3
nop 2
1
<?php
2
3
namespace Victoire\Bundle\WidgetBundle\Command;
4
5
use Doctrine\DBAL\Types\Type;
6
use Doctrine\ORM\EntityManager;
7
use Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand;
8
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
9
use Sensio\Bundle\GeneratorBundle\Command\Validators;
10
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Question\ConfirmationQuestion;
16
use Symfony\Component\Console\Question\Question;
17
use Symfony\Component\DependencyInjection\Container;
18
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19
use Victoire\Bundle\WidgetBundle\Generator\WidgetGenerator;
20
21
/**
22
 * Create a new Widget for VictoireCMS.
23
 */
24
class MigrateWidgetOwningSideCommand extends ContainerAwareCommand
25
{
26
    protected $skeletonDirs;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function configure()
32
    {
33
        parent::configure();
34
35
        $this
36
            ->setName('victoire:widget:migrate-owning-side')
37
            ->setDescription('widget is now the owning side of Widget<=>WidgetMap relation');
38
    }
39
40
    /**
41
     *
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        /** @var EntityManager $entityManager */
48
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
49
50
        $widgetMaps = $entityManager->getRepository('VictoireWidgetMapBundle:WidgetMap')->findAll();
51
52
        $progress = $this->getHelper('progress');
53
        $progress->start($output, count($widgetMaps));
54
        foreach ($widgetMaps as $key => $widgetMap) {
55
            $widget = $widgetMap->getWidget();
56
            $widget->setWidgetMap($widgetMap);
57
            $widgetMap->setWidget(null);
58
            if ($key%100 == 0) {
59
                $entityManager->flush();
60
            }
61
            $progress->advance();
62
        }
63
64
        $entityManager->flush();
65
66
        $progress->finish();
67
68
    }
69
70
}
71