TranslationDownloadCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Command;
4
5
use Happyr\TranslationBundle\Http\RequestManager;
6
use Happyr\TranslationBundle\Service\Loco;
7
use Http\Adapter\Guzzle6\Client;
8
use Http\Message\MessageFactory\GuzzleMessageFactory;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * @author Benjamin HUBERT <[email protected]>
15
 */
16
class TranslationDownloadCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function configure()
22
    {
23
        $this->setName('alpixel:cms:translations:download');
24
        $this->setDescription('Download translations from Loco service');
25
    }
26
27
    public function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $container = $this->getContainer();
30
31
        $requestManager = new RequestManager(new Client(), new GuzzleMessageFactory());
32
        $fileSystem = $container->get('happyr.translation.filesystem');
33
34
        $languages = $container->getParameter('enabled_locales');
35
        $domains = ['messages', 'validators', 'routes'];
36
37
        $query = [
38
            'key'      => $container->getParameter('loco_api_key'),
39
            'fallback' => $container->getParameter('default_locale'),
40
            'index'    => 'text',
41
            'format'   => 'symfony',
42
        ];
43
44
        $uri = [];
45
        foreach ($languages as $language) {
46
            foreach ($domains as $domain) {
47
                $realQuery = $query;
48
                if ($domain === 'routes') {
49
                    unset($realQuery['index']);
50
                }
51
                $url = Loco::BASE_URL.'export/locale/'.$language.'.xlf?'.http_build_query(array_merge($realQuery, [
52
                        'filter' => $domain,
53
                    ]));
54
                $uri[$url] = $domain.'.'.$language.'.xlf';
55
            }
56
        }
57
58
        $requestManager->downloadFiles($fileSystem, $uri);
59
        $output->writeln('Translations downloaded');
60
    }
61
}
62