Completed
Pull Request — master (#433)
by Paul
11:06 queued 04:27
created

LegacyViewTranslationMigrationCommand::execute()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 47
rs 6.7272
cc 7
eloc 32
nc 16
nop 2
1
<?php
2
3
namespace Victoire\Bundle\I18nBundle\Command;
4
5
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Victoire\Bundle\CoreBundle\Entity\Link;
11
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation;
12
use Victoire\Bundle\I18nBundle\Entity\ViewTranslationLegacy;
13
14
class LegacyViewTranslationMigrationCommand extends ContainerAwareCommand
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function configure()
20
    {
21
        parent::configure();
22
23
        $this
24
            ->setName('victoire:legacy:view_translation')
25
            ->setDescription('migrate gedmo view translations into knp translations')
26
            ->addOption('mode', null, InputOption::VALUE_OPTIONAL, 'could be "all" to migrate all, "views" to migrate only views or "articles" to migrate only articles', 'all');
27
    }
28
29
    /**
30
     * Transform Gedmo translations into Knp translations
31
     *
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     *
35
     * @return void
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
40
        $mode = $input->getOption('mode');
41
        $progress = $this->getHelperSet()->get('progress');
42
        $progress->setProgressCharacter('V');
43
        $progress->setEmptyBarCharacter('-');
44
45
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
46
47
        $legacyTranslations = $legacyArticles = [];
48
        if ('all' === $mode || 'views' === $mode) {
49
            $repo = $entityManager->getRepository('Victoire\Bundle\I18nBundle\Entity\ViewTranslationLegacy');
50
            $legacyTranslations = $repo->findAll();
51
        }
52
        if ('all' === $mode || 'articles' === $mode) {
53
            $repoArticle = $entityManager->getRepository('Victoire\Bundle\BlogBundle\Entity\Article');
54
            $legacyArticles = $repoArticle->findAll();
55
        }
56
        $total = count($legacyTranslations) + count($legacyArticles);
57
        $progress->start($output, $total);
58
        /** @var ViewTranslationLegacy $legacyTranslation */
59
        foreach ($legacyTranslations as $legacyTranslation) {
60
            $view = $legacyTranslation->getObject();
61
            $trans = $view->translate($legacyTranslation->getLocale(), false)->{'set'.$legacyTranslation->getField()}($legacyTranslation->getContent());
0 ignored issues
show
Unused Code introduced by
$trans 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...
62
            $entityManager->persist($view);
63
            $view->mergeNewTranslations();
64
65
            $progress->advance();
66
        }
67
68
        foreach ($legacyArticles as $legacyArticle) {
69
            $legacyArticleTranslation = $legacyArticle->translate('fr', false);
70
            $legacyArticleTranslation->setName($legacyArticle->getName());
71
            $legacyArticleTranslation->setSlug($legacyArticle->getSlug());
72
            $legacyArticleTranslation->setDescription($legacyArticle->getDescription());
73
            $entityManager->persist($legacyArticleTranslation);
74
            $legacyArticle->mergeNewTranslations();
75
76
            $progress->advance();
77
        }
78
        
79
        $entityManager->flush();
80
        $progress->finish();
81
82
        $output->writeln(sprintf('<comment>Ok, %s translations migrated !</comment>', count($total)));
83
    }
84
}
85