Passed
Push — master ( 865ea9...f5a1ef )
by butschster
29:08 queued 21:20
created

ExportCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 9
eloc 44
c 0
b 0
f 0
dl 0
loc 80
ccs 35
cts 37
cp 0.9459
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A perform() 0 36 4
A getMessageCatalogue() 0 21 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Command\Translator;
6
7
use Spiral\Console\Attribute\Question;
8
use Spiral\Console\Command;
9
use Spiral\Core\Container\SingletonInterface;
10
use Spiral\Translator\Catalogue\CatalogueManager;
11
use Spiral\Translator\CatalogueInterface;
12
use Spiral\Translator\Config\TranslatorConfig;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Translation\MessageCatalogue;
16
17
#[Question(
18
    question: 'What is the locale that you would like to export translations for?',
19
    argument: 'locale'
20
)]
21
#[Question(
22
    question: 'What is the path to where you would like to export the translations?',
23
    argument: 'path'
24
)]
25
final class ExportCommand extends Command implements SingletonInterface
26
{
27
    protected const NAME        = 'i18n:export';
28
    protected const DESCRIPTION = 'Dump given locale using specified dumper and path';
29
    protected const ARGUMENTS   = [
30
        ['locale', InputArgument::REQUIRED, 'Locale to be dumped'],
31
        ['path', InputArgument::REQUIRED, 'Export path'],
32
    ];
33
    protected const OPTIONS     = [
34
        ['dumper', 'd', InputOption::VALUE_OPTIONAL, 'Dumper name', 'php'],
35
        ['fallback', 'f', InputOption::VALUE_NONE, 'Merge messages from fallback catalogue'],
36
    ];
37
38 1
    public function perform(TranslatorConfig $config, CatalogueManager $manager): int
39
    {
40 1
        if (!$config->hasDumper($this->option('dumper'))) {
41
            $this->writeln("<fg=red>Undefined dumper '{$this->option('dumper')}'.</fg=red>");
42
43
            return self::FAILURE;
44
        }
45
46 1
        $mc = $this->getMessageCatalogue(
47 1
            $config,
48 1
            $manager,
49 1
            $manager->get($this->argument('locale'))
50 1
        );
51
52 1
        if ($this->isVerbose() && !empty($mc->getDomains())) {
53 1
            $this->sprintf(
54 1
                "<info>Exporting domain(s):</info> %s\n",
55 1
                \implode(',', $mc->getDomains())
56 1
            );
57
        }
58
59 1
        $dumper = $config->getDumper($this->option('dumper'));
60
61 1
        $dumper->dump(
62 1
            $mc,
63 1
            [
64 1
                'path'           => $this->argument('path'),
65 1
                'default_locale' => $config->getDefaultLocale(),
66 1
                'xliff_version'  => '2.0', // forcing default version for xliff dumper only
67 1
            ]
68 1
        );
69
70 1
        $this->writeln('Export successfully completed using <info>' . $dumper::class . '</info>');
71 1
        $this->writeln('Output: <comment>' . \realpath($this->argument('path')) . '</comment>');
72
73 1
        return self::SUCCESS;
74
    }
75
76 1
    protected function getMessageCatalogue(
77
        TranslatorConfig $config,
78
        CatalogueManager $manager,
79
        CatalogueInterface $catalogue
80
    ): MessageCatalogue {
81 1
        $messageCatalogue = new MessageCatalogue(
82 1
            $catalogue->getLocale(),
83 1
            $catalogue->getData()
84 1
        );
85
86 1
        if ($this->option('fallback')) {
87 1
            foreach ($manager->get($config->getFallbackLocale())->getData() as $domain => $messages) {
88 1
                foreach ($messages as $id => $message) {
89 1
                    if (!$messageCatalogue->defines($id, $domain)) {
90 1
                        $messageCatalogue->set($id, $message, $domain);
91
                    }
92
                }
93
            }
94
        }
95
96 1
        return $messageCatalogue;
97
    }
98
}
99