Completed
Pull Request — develop (#422)
by Lucas
22:00 queued 16:03
created

CreateTranslationResourcesCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 7
dl 0
loc 89
ccs 36
cts 36
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A configure() 0 8 1
B execute() 0 26 1
1
<?php
2
/**
3
 * create translation resources based on the available resources in the mongodb catalog
4
 */
5
6
namespace Graviton\I18nBundle\Command;
7
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Graviton\I18nBundle\Repository\LanguageRepository;
13
use Graviton\I18nBundle\Repository\TranslatableRepository;
14
15
/**
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class CreateTranslationResourcesCommand extends Command
21
{
22
    /**
23
     * @var LanguageRepository
24
     */
25
    private $languageRepo;
26
27
    /**
28
     * @var TranslatableRepository
29
     */
30
    private $translatableRepo;
31
32
    /**
33
     * @var Filesystem
34
     */
35
    private $filesystem;
36
37
    /**
38
     * @var string
39
     */
40
    private $resourceDir;
41
42
    /**
43
     * @param LanguageRepository     $languageRepo     Language Repository
44
     * @param TranslatableRepository $translatableRepo Translatable Repository
45
     * @param Filesystem             $filesystem       symfony/filesystem tooling
46
     */
47 4
    public function __construct(
48
        LanguageRepository $languageRepo,
49
        TranslatableRepository $translatableRepo,
50
        Filesystem $filesystem
51
    ) {
52 4
        $this->languageRepo = $languageRepo;
53 4
        $this->translatableRepo = $translatableRepo;
54 4
        $this->filesystem = $filesystem;
55 4
        $this->resourceDir = __DIR__.'/../Resources/translations/';
56
57 4
        parent::__construct();
58 4
    }
59
60
    /**
61
     * set up command
62
     *
63
     * @return void
64
     */
65 4
    protected function configure()
66
    {
67 2
        $this
68 4
            ->setName('graviton:i18n:create:resources')
69 4
            ->setDescription(
70 2
                'Create translation resource stub files for all the available languages/domains in the database'
71 2
            );
72 4
    }
73
74
    /**
75
     * run command
76
     *
77
     * @param InputInterface  $input  input interface
78
     * @param OutputInterface $output output interface
79
     *
80
     * @return void
81
     */
82 2
    protected function execute(InputInterface $input, OutputInterface $output)
83
    {
84 2
        $output->writeln("Creating translation resource stubs");
85
86 2
        $languages = $this->languageRepo->findAll();
87 2
        $domains = $this->translatableRepo->createQueryBuilder()
88 2
            ->distinct('domain')
89 2
            ->select('domain')
90 2
            ->getQuery()
91 2
            ->execute()
92 2
            ->toArray();
93
94 2
        array_walk(
95 1
            $languages,
96
            function ($language) use ($output, $domains) {
97 2
                array_walk(
98 1
                    $domains,
99 2
                    function ($domain) use ($output, $language) {
100 2
                        $file = implode('.', [$domain, $language->getId(), 'odm']);
101 2
                        $this->filesystem->touch(implode(DIRECTORY_SEPARATOR, [$this->resourceDir, $file]));
102 2
                        $output->writeln("<info>Generated file $file</info>");
103 2
                    }
104 1
                );
105 2
            }
106 1
        );
107 2
    }
108
}
109