Completed
Push — master ( df1f12...4872e5 )
by Lucas
05:42
created

LoadMissingCommand::generateTranslatables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1.0007
Metric Value
dl 0
loc 13
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
crap 1.0007
1
<?php
2
/**
3
 * generate missing translation entities based on english strings
4
 */
5
6
namespace Graviton\I18nBundle\Command;
7
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Graviton\I18nBundle\Document\Translatable;
12
13
/**
14
 * generate missing translation entities based on english strings
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 LoadMissingCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * base strings to translate (en)
24
     *
25
     * @var Translatable[]
26
     */
27
    protected $baseStrings;
28
29
    /**
30
     * set up command
31
     *
32
     * @return void
33
     */
34 1
    protected function configure()
35
    {
36 1
        $this
37 1
            ->setName('graviton:i118n:load:missing')
38 1
            ->setDescription('Generate translatables for strings in en.');
39 1
    }
40
41
    /**
42
     * run command
43
     *
44
     * @param InputInterface  $input  input interface
45
     * @param OutputInterface $output output interface
46
     *
47
     * @return void
48
     */
49 1
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51 1
        $this->generateTranslatables();
52 1
    }
53
54
    /**
55
     * loop over languages
56
     *
57
     * @return void
58
     */
59 1
    private function generateTranslatables()
60
    {
61 1
        $container = $this->getContainer();
62 1
        $languages = $container->get('graviton.i18n.repository.language')->findAll();
63 1
        $this->baseStrings = $container->get('graviton.i18n.repository.translatable')->findBy(array('locale' => 'en'));
64
65 1
        array_walk(
66 1
            $languages,
67
            function ($language) {
68 1
                $this->generateForLanguage($language->getId());
69 1
            }
70 1
        );
71 1
    }
72
73
    /**
74
     * generate strings for languages
75
     *
76
     * @param Language $language language
77
     *
78
     * @return void
79
     */
80 1
    private function generateForLanguage($language)
81
    {
82 1
        if ($language == 'en') {
83 1
            return;
84
        }
85 1
        array_walk(
86 1
            $this->baseStrings,
87 1
            function ($base) use ($language) {
88 1
                $domain = $base->getDomain();
89 1
                $original = $base->getOriginal();
90 1
                $id = implode('-', array($domain, $language, $original));
91
92 1
                $record = new Translatable;
93 1
                $record->setId($id);
94 1
                $record->setDomain($domain);
95 1
                $record->setLocale($language);
96 1
                $record->setOriginal($original);
97
98 1
                $this->getContainer()->get('graviton.i18n.model.translatable')->insertRecord($record);
99 1
            }
100 1
        );
101 1
    }
102
}
103