Completed
Push — master ( 91b512...bab6b4 )
by Martin
13:02 queued 07:59
created

YamlConvertCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This is part of the webuni/composer-yaml-plugin package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\ComposerYamlPlugin;
14
15
use Composer\Command\BaseCommand;
16
use Composer\Json\JsonFile;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Yaml\Yaml;
21
22
final class YamlConvertCommand extends BaseCommand
23
{
24
    private $formats = [
25
        'yaml' => 'yaml',
26
        'yml' => 'yaml',
27
        'json' => 'json',
28
    ];
29
30 7
    protected function configure()
31
    {
32
        $this
33 7
            ->setName('yaml-convert')
34 7
            ->setDescription('Converts a composer.yml to json or vice-versa')
35 7
            ->addArgument('input', InputArgument::OPTIONAL, 'The input file', 'composer.yml')
36 7
            ->addArgument('output', InputArgument::OPTIONAL, 'The output file', 'composer.json')
37
        ;
38 7
    }
39
40 6
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $data = [
43 6
            'input' => $input->getArgument('input'),
44 6
            'output' => $input->getArgument('output'),
45
        ];
46
47 6
        if (!is_file($data['input'])) {
48 3
            throw new \InvalidArgumentException(sprintf('The input file "%s" does not exist.', $data['input']));
49
        }
50
51 3
        $formats = [];
52 3
        foreach ($data as $type => $file) {
53 3
            $format = strtolower(pathinfo($file, PATHINFO_EXTENSION));
54 3
            if (!isset($this->formats[$format])) {
55
                throw new \InvalidArgumentException(sprintf(
56
                    'Invalid %s format "%s", must be one of: %s',
57
                    $type, $format, implode(', ', array_keys($this->formats))
58
                ));
59
            }
60
61 3
            $formats[$type] = $this->formats[$format];
62
        }
63
64 3
        if ($formats['input'] === $formats['output']) {
65 1
            throw new \InvalidArgumentException('Input format is same as output format.');
66
        }
67
68 2
        $content = file_get_contents($data['input']);
69
70 2
        if ('json' === $formats['input']) {
71 1
            $converted = Yaml::dump(JsonFile::parseJson($content));
72
        } else {
73 1
            $converted = JsonFile::encode(Yaml::parse($content));
74
        }
75
76 2
        file_put_contents($data['output'], $converted);
77 2
        $output->writeln(sprintf('Converted "%s" to "%s"', $data['input'], $data['output']));
78 2
    }
79
}
80