Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

GenerateTranslationsCommand::execute()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.551
c 0
b 0
f 0
cc 5
eloc 31
nc 9
nop 2
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Command;
13
14
use Blast\Bundle\CoreBundle\Generator\ArrayToYamlGenerator;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Filesystem\Filesystem;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\DomCrawler\Crawler;
21
use Symfony\Component\Console\Input\InputArgument;
22
23
/**
24
 * Class GenerateAdminCommand.
25
 */
26
class GenerateTranslationsCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function configure()
32
    {
33
        $this
34
            ->setName('blast:generate:translations')
35
            ->setDescription('Generates translation files from XLIFF to YAML')
36
            ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to generate translations for (ex: AcmeDemoBundle)')
37
        ;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function isEnabled()
44
    {
45
        return class_exists('Sensio\\Bundle\\GeneratorBundle\\SensioGeneratorBundle');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $bundle = $input->getArgument('bundle');
54
        $fs = new Filesystem();
55
        $finder = new Finder();
56
        $crawler = new Crawler();
57
58
        $path = $this->getContainer()->get('kernel')->locateResource('@' . $bundle);
59
        $transPath = $path . 'Resources/translations/';
60
61
        if (!$fs->exists($transPath)) {
62
            try {
63
                $fs->mkdir($transPath);
64
            } catch (IOExceptionInterface $e) {
0 ignored issues
show
Bug introduced by
The class Blast\Bundle\CoreBundle\...nd\IOExceptionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
65
                echo sprintf('An error occurred while creating your directory at %s', $e->getPath());
66
            }
67
        }
68
69
        foreach ($finder->files()->in($transPath) as $file) {
70
            $translations = [];
71
72
            $crawler->addXmlContent(file_get_contents($file->getPathName()));
73
            $crawler = $crawler->filter('trans-unit');
74
75
            foreach ($crawler as $transUnit) {
76
                $source = $transUnit
77
                            ->getElementsByTagName('source')
78
                            ->item(0)
79
                            ->nodeValue
80
                        ;
81
                $source = str_replace("'", "''", $source);
82
83
                $target = $transUnit
84
                            ->getElementsByTagName('target')
85
                            ->item(0)
86
                            ->nodeValue
87
                        ;
88
                $target = str_replace("'", "''", $target);
89
90
                $translations[$source] = $target;
91
            }
92
93
            $ymlGenerator = new ArrayToYamlGenerator($file, __DIR__ . '/../Resources/skeleton');
94
            $ymlGenerator->generate($translations, 'Messages.yml.twig');
0 ignored issues
show
Documentation introduced by
$translations is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
        }
96
97
        return 0;
98
    }
99
}
100