CreateLanguageCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[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
declare (strict_types = 1);
13
14
namespace Tardigrades\Command;
15
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Yaml\Yaml;
20
use Tardigrades\SectionField\Service\LanguageManagerInterface;
21
use Tardigrades\SectionField\ValueObject\LanguageConfig;
22
23
class CreateLanguageCommand extends LanguageCommand
24
{
25
    /** @var LanguageManagerInterface */
26
    private $languageManager;
27
28
    public function __construct(
29
        LanguageManagerInterface $languageManager
30
    ) {
31
        $this->languageManager = $languageManager;
32
33
        parent::__construct('sf:create-language');
34
    }
35
36
    protected function configure()
37
    {
38
        $this
39
            ->setDescription('Create language')
40
            ->setHelp('Create language')
41
            ->addArgument('config', InputArgument::REQUIRED, 'The language configuration yml');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $config = $input->getArgument('config');
47
48
        try {
49
            $languageConfig = LanguageConfig::fromArray(
50
                Yaml::parse(file_get_contents($config))
51
            );
52
            $this->languageManager->createByConfig($languageConfig);
53
            $output->writeln('<info>Languages created!</info>');
54
        } catch (\Exception $exception) {
55
            $output->writeln("<error>Invalid config. {$exception->getMessage()}</error>");
56
        }
57
    }
58
}
59