Passed
Push — 8.x-dev ( 640d34...2250ec )
by Arnaud
04:51 queued 16s
created

ShowConfig::printArray()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 2
dl 0
loc 19
ccs 0
cts 13
cp 0
crap 30
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Command;
15
16
use Cecil\Exception\RuntimeException;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputDefinition;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Yaml\Yaml;
23
24
/**
25
 * Shows the configuration.
26
 */
27
class ShowConfig extends AbstractCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('show:config')
36
            ->setDescription('Shows the configuration')
37
            ->setDefinition(
38
                new InputDefinition([
39
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
40
                    new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Set the path to the config file'),
41
                ])
42
            )
43
            ->setHelp('Shows the website\'s configuration in YAML format');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws RuntimeException
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        try {
54
            $output->writeln($this->arrayToYaml($this->getBuilder()->getConfig()->getAsArray()));
55
        } catch (\Exception $e) {
56
            throw new RuntimeException($e->getMessage());
57
        }
58
59
        return 0;
60
    }
61
62
    /**
63
     * Converts an array to YAML.
64
     */
65
    private function arrayToYaml(array $array): string
66
    {
67
        return trim(Yaml::dump($array, 6, 2));
68
    }
69
}
70