DumpTranslationFilesCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 72
rs 8.5164
c 1
b 0
f 1
ccs 50
cts 50
cp 1
cc 6
eloc 41
nc 24
nop 2
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the AsmTranslationLoaderBundle package.
5
 *
6
 * (c) Marc Aschmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Asm\TranslationLoaderBundle\Command;
13
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Translation\MessageCatalogue;
18
19
/**
20
 * Class DumpTranslationFiles
21
 *
22
 * @package Asm\TranslationLoaderBundle\Command
23
 * @author marc aschmann <[email protected]>
24
 * @author Christian Flothmann <[email protected]>
25
 * @uses Symfony\Component\Console\Input\InputArgument
26
 * @uses Symfony\Component\Console\Input\InputInterface
27
 * @uses Symfony\Component\Console\Input\InputOption
28
 * @uses Symfony\Component\Console\Output\OutputInterface
29
 * @uses Symfony\Component\Translation\MessageCatalogue
30
 */
31
class DumpTranslationFilesCommand extends BaseTranslationCommand
32
{
33
34
    /**
35
     * {@inheritDoc}
36
     */
37 6
    protected function configure()
38
    {
39 6
        $this
40 6
            ->setName('asm:translations:dump')
41 6
            ->setDescription('dump translations from database to files')
42 6
            ->addOption(
43 6
                'format',
44 6
                'f',
45 6
                InputOption::VALUE_OPTIONAL,
46 6
                'File format, default yml ',
47
                'yml'
48 6
            )
49 6
            ->addOption(
50 6
                'locale',
51 6
                'l',
52 6
                InputOption::VALUE_OPTIONAL,
53
                'Specific locale to dump'
54 6
            )
55 6
            ->addOption(
56 6
                'domain',
57 6
                'd',
58 6
                InputOption::VALUE_OPTIONAL,
59 6
                'Specific message domain to dump, requires locale!',
60
                'messages'
61 6
            );
62 6
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 2
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69 2
        $output->writeln(
70
            '<info>--------------------------------------------------------------------------------</info>'
71 2
        );
72 2
        $output->writeln('<info>generating translation files</info>');
73 2
        $output->writeln(
74
            '<info>--------------------------------------------------------------------------------</info>'
75 2
        );
76
77 2
        $translationPath = $this->getKernel()->getRootDir() . '/Resources/translations/';
78
79
        // create directory for translations if it does not exist
80 2
        if (!$this->getFilesystem()->exists($translationPath)) {
81 1
            $this->getFilesystem()->mkdir($translationPath);
82 1
        }
83
84 2
        $translationManager = $this->getTranslationManager();
85 2
        $locale             = $input->getOption('locale');
86 2
        $domain             = $input->getOption('domain');
87
        $criteria           = array(
88 2
            'messageDomain' => $domain,
89 2
        );
90
91 2
        if (null !== $locale) {
92 1
            $criteria['transLocale'] = $locale;
93 1
        }
94
95 2
        $translations = $translationManager->findTranslationsBy($criteria);
96
97
        // create message catalogues first
98 2
        $catalogues = array();
99 2
        foreach ($translations as $translation) {
100 2
            $locale = $translation->getTransLocale();
101
102 2
            if (!isset($catalogues[$locale])) {
103 2
                $catalogues[$locale] = new MessageCatalogue($locale);
104 2
            }
105
106
            /** @var MessageCatalogue $catalogue */
107 2
            $catalogue = $catalogues[$locale];
108
109 2
            $catalogue->set(
110 2
                $translation->getTransKey(),
111 2
                $translation->getTranslation(),
112 2
                $translation->getMessageDomain()
113 2
            );
114 2
        }
115
116
        // dump the generated catalogues
117 2
        foreach ($catalogues as $locale => $catalogue) {
118 2
            $output->writeln(
119 2
                sprintf(
120 2
                    '<comment>generating catalogue for locale %s</comment>',
121
                    $locale
122 2
                )
123 2
            );
124 2
            $this->getTranslationWriter()->writeTranslations(
125 2
                $catalogue,
126 2
                $input->getOption('format'),
127 2
                array('path' => $translationPath)
128 2
            );
129 2
        }
130
131 2
        $output->writeln(
132
            '<info>--------------------------------------------------------------------------------</info>'
133 2
        );
134 2
        $output->writeln('<info>finished!</info>');
135 2
        $output->writeln(
136
            '<info>--------------------------------------------------------------------------------</info>'
137 2
        );
138 2
    }
139
}
140