Passed
Branch master (a4c265)
by Anton
02:03
created

ExportCommand::perform()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
rs 9.7
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Command\Translator;
11
12
use Spiral\Console\Command;
13
use Spiral\Core\Container\SingletonInterface;
14
use Spiral\Translator\Catalogue\CatalogueManager;
15
use Spiral\Translator\CatalogueInterface;
16
use Spiral\Translator\Config\TranslatorConfig;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Translation\MessageCatalogue;
20
21
final class ExportCommand extends Command implements SingletonInterface
22
{
23
    const NAME        = "i18n:export";
24
    const DESCRIPTION = 'Dump given locale using specified dumper and path';
25
26
    const ARGUMENTS = [
27
        ['locale', InputArgument::REQUIRED, 'Locale to be dumped'],
28
        ['path', InputArgument::REQUIRED, 'Export path']
29
    ];
30
31
    const OPTIONS = [
32
        ['dumper', 'd', InputOption::VALUE_OPTIONAL, 'Dumper name', 'php'],
33
        ['fallback', 'f', InputOption::VALUE_NONE, 'Merge messages from fallback catalogue'],
34
    ];
35
36
    /**
37
     * @param TranslatorConfig $config
38
     * @param CatalogueManager $manager
39
     */
40
    public function perform(TranslatorConfig $config, CatalogueManager $manager)
41
    {
42
        if (!$config->hasDumper($this->option('dumper'))) {
43
            $this->writeln("<fg=red>Undefined dumper '{$this->option('dumper')}'.</fg=red>");
44
45
            return;
46
        }
47
48
        $mc = $this->getMessageCatalogue(
49
            $config,
50
            $manager,
51
            $manager->get($this->argument('locale'))
0 ignored issues
show
Bug introduced by
It seems like $this->argument('locale') can also be of type null and string[]; however, parameter $locale of Spiral\Translator\Catalo...CatalogueManager::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            $manager->get(/** @scrutinizer ignore-type */ $this->argument('locale'))
Loading history...
52
        );
53
54
        if ($this->isVerbose() && !empty($mc->getDomains())) {
55
            $this->sprintf("<info>Exporting domain(s):</info> %s\n",
56
                join(',', $mc->getDomains())
57
            );
58
        }
59
60
        $dumper = $config->getDumper($this->option('dumper'));
61
62
        $dumper->dump($mc, [
63
            'path'           => $this->argument('path'),
64
            'default_locale' => $config->getDefaultLocale(),
65
            'xliff_version'  => '2.0' // forcing default version for xliff dumper only
66
        ]);
67
68
        $this->writeln("Export successfully completed using <info>" . get_class($dumper) . "</info>");
69
        $this->writeln("Output: <comment>" . realpath($this->argument('path')) . "</comment>");
0 ignored issues
show
Bug introduced by
It seems like $this->argument('path') can also be of type string[]; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $this->writeln("Output: <comment>" . realpath(/** @scrutinizer ignore-type */ $this->argument('path')) . "</comment>");
Loading history...
70
    }
71
72
    /**
73
     * @param TranslatorConfig   $config
74
     * @param CatalogueManager   $manager
75
     * @param CatalogueInterface $catalogue
76
     * @return MessageCatalogue
77
     */
78
    protected function getMessageCatalogue(
79
        TranslatorConfig $config,
80
        CatalogueManager $manager,
81
        CatalogueInterface $catalogue
82
    ): MessageCatalogue {
83
        $messageCatalogue = new MessageCatalogue(
84
            $catalogue->getLocale(),
85
            $catalogue->getData()
86
        );
87
88
        if ($this->option('fallback')) {
89
            foreach ($manager->get($config->getFallbackLocale())->getData() as $domain => $messages) {
90
                foreach ($messages as $id => $message) {
91
                    if (!$messageCatalogue->defines($id, $domain)) {
92
                        $messageCatalogue->set($id, $message, $domain);
93
                    }
94
                }
95
            }
96
        }
97
98
        return $messageCatalogue;
99
    }
100
}