WriteTranslationValuesCommand::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Locastic\SymfonyTranslationBundle\Cli;
6
7
use Locastic\SymfonyTranslationBundle\Provider\TranslationsProviderInterface;
8
use Locastic\SymfonyTranslationBundle\Saver\TranslationValueSaverInterface;
9
use Locastic\SymfonyTranslationBundle\Transformer\TranslationKeyToTranslationTransformerInterface;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
use function sprintf;
15
16
final class WriteTranslationValuesCommand extends Command
17
{
18
    /** @var string */
19
    public static $defaultName = 'locastic:symfony-translation:dump-all';
20
21
    private TranslationsProviderInterface $translationsProvider;
22
23
    private TranslationKeyToTranslationTransformerInterface $translationTransformer;
24
25
    private TranslationValueSaverInterface $translationValueSaver;
26
27
    private string $localeCode;
28
29
    private array $locales;
30
31
    protected OutputInterface $output;
32
33
    public function __construct(
34
        TranslationsProviderInterface $translationsProvider,
35
        TranslationKeyToTranslationTransformerInterface $translationTransformer,
36
        TranslationValueSaverInterface $translationValueSaver,
37
        string $localeCode,
38
        array $locales,
39
        string $name = null
40
    ) {
41
        parent::__construct($name);
42
43
        $this->translationsProvider = $translationsProvider;
44
        $this->translationTransformer = $translationTransformer;
45
        $this->translationValueSaver = $translationValueSaver;
46
        $this->localeCode = $localeCode;
47
        $this->locales = $locales;
48
    }
49
50
    protected function configure(): void
51
    {
52
        $this->setDescription('Dump all translations into files');
53
    }
54
55
    protected function execute(InputInterface $input, OutputInterface $output): int
56
    {
57
        $this->writeln('Starting to dump translations', OutputInterface::VERBOSITY_NORMAL);
58
59
        $translations = $this->translationsProvider->getTranslations($this->localeCode, $this->locales);
60
        $translations = $this->translationTransformer->transformMultiple($translations);
61
62
        $this->translationValueSaver->saveTranslations($translations);
63
64
        return 0;
65
    }
66
67
    protected function initialize(InputInterface $input, OutputInterface $output): void
68
    {
69
        parent::initialize($input, $output);
70
71
        $this->output = $output;
72
    }
73
74
    protected function writeLn(string $message, int $level = OutputInterface::OUTPUT_NORMAL): void
75
    {
76
        $this->output->writeln(sprintf('[%s] %s', date('Y-m-d H:i:s'), $message), $level);
77
    }
78
}
79