Completed
Push — master ( 0b2e6b...2c86a7 )
by Paul
49:42 queued 39:17
created

ImportViewTranslationsCommand::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 45
rs 8.5806
c 1
b 0
f 1
cc 4
eloc 29
nc 3
nop 2
1
<?php
2
3
namespace Victoire\Bundle\I18nBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Helper\ProgressHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Victoire\Bundle\CoreBundle\Entity\View;
12
13
class ImportViewTranslationsCommand extends ContainerAwareCommand
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function configure()
19
    {
20
        parent::configure();
21
22
        $this
23
            ->setName('victoire:i18n:import_view_translations')
24
            ->setDescription('import view translations (csv)')
25
            ->addOption(
26
                'src',
27
                null,
28
                InputOption::VALUE_REQUIRED,
29
                'myFile.tsv, file.csv...'
30
            )
31
            ->addOption(
32
                'property',
33
                null,
34
                InputOption::VALUE_REQUIRED,
35
                'name, description...'
36
            )
37
            ->addOption(
38
                'delimiter',
39
                null,
40
                InputOption::VALUE_OPTIONAL,
41
                '\n...',
42
                '	'
43
            );
44
    }
45
46
    /**
47
     * @param InputInterface  $input
48
     * @param OutputInterface $output
49
     *
50
     * @return void
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $property = $input->getOption('property');
55
        $delimiter = $input->getOption('delimiter');
56
57
        /** @var ProgressHelper $progress */
58
        $progress = $this->getHelperSet()->get('progress');
59
        $progress->setProgressCharacter('V');
60
        $progress->setEmptyBarCharacter('-');
61
62
        /** @var EntityManager $entityManager */
63
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
64
        $repo = $entityManager->getRepository('VictoireCoreBundle:View');
65
66
        $contentsOfFile = file_get_contents($input->getOption('src'));
67
68
        $lines = explode("\n", $contentsOfFile);
69
        $header = array_shift($lines);
70
        $header = explode($delimiter, $header);
71
        array_shift($header);
72
73
        $total = 0;
74
        $progress->start($output, count($lines));
75
76
        foreach ($lines as $line) {
77
            $translations = explode($delimiter, $line);
78
            $id = array_shift($translations);
79
80
            if ($view = $repo->find($id)) {
81
                foreach ($translations as $key => $translation) {
82
                    $locale = rtrim(strtolower($header[$key]), "\r");
83
                    $view->translate($locale)->{'set'.ucfirst($property)}($translation);
84
                }
85
                $view->mergeNewTranslations();
86
                $entityManager->flush();
87
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
88
            }
89
            $progress->advance();
90
            $total++;
91
        }
92
93
        $progress->finish();
94
95
        $output->writeln(sprintf('<comment>Ok, %s translations updated !</comment>', $total));
96
    }
97
}
98