Completed
Push — master ( 212d18...a5a0ed )
by Benjamin
06:54 queued 04:22
created

TranslationDownloadCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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