YamlConvertCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 19
cts 19
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 8
nop 2
crap 6
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 9
    protected function configure()
31
    {
32
        $this
33 9
            ->setName('yaml-convert')
34 9
            ->setDescription('Converts a composer.yaml to json or vice-versa')
35 9
            ->addArgument('input', InputArgument::OPTIONAL, 'The input file')
36 9
            ->addArgument('output', InputArgument::OPTIONAL, 'The output file')
37
        ;
38 9
    }
39
40 8
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42 8
        $files = $this->getFiles([
43 8
            'input' => $input->getArgument('input'),
44 8
            'output' => $input->getArgument('output'),
45
        ]);
46
47 8
        $from = $this->getFormat($files['input'], 'input');
48
49 7
        if (!is_file($files['input'])) {
50 2
            throw new \InvalidArgumentException(sprintf('The input file "%s" does not exist.', $files['input']));
51
        }
52
53 5
        if (null === $files['output']) {
54 1
            $files['output'] = 'json' === $from ? 'composer.yaml' : 'composer.json';
55
        } else {
56 4
            $to = $this->getFormat($files['output'], 'output');
57
58 4
            if ($from === $to) {
59 1
                throw new \InvalidArgumentException(sprintf('Input format "%s" is same as output format.', $from));
60
            }
61
        }
62
63 4
        $content = file_get_contents($files['input']);
64
65 4
        if ('json' === $from) {
66 2
            $converted = Yaml::dump(JsonFile::parseJson($content));
67
        } else {
68 2
            $converted = JsonFile::encode(Yaml::parse($content))."\n";
69
        }
70
71 4
        file_put_contents($files['output'], $converted);
72 4
        $output->writeln(sprintf('Converted "%s" to "%s"', $files['input'], $files['output']));
73 4
    }
74
75 8
    private function getFiles(array $data)
76
    {
77 8
        if ((null === $data['input']) && (null === $data['output'])) {
78 3
            $data['output'] = 'composer.json';
79
        }
80
81 8
        if ((null === $data['input']) && is_file('composer.yml')) {
82 2
            $data['input'] = 'composer.yml';
83
        }
84
85 8
        if (null === $data['input']) {
86 2
            $data['input'] = 'composer.yaml';
87
        }
88
89 8
        return $data;
90
    }
91
92 8
    private function getFormat($file, $type)
93
    {
94 8
        $format = strtolower(pathinfo($file, PATHINFO_EXTENSION));
95 8
        if (!isset($this->formats[$format])) {
96 1
            throw new \InvalidArgumentException(sprintf(
97 1
                'Invalid %s format "%s", must be one of: %s.',
98 1
                $type, $format, implode(', ', array_keys($this->formats))
99
            ));
100
        }
101
102 7
        return $this->formats[$format];
103
    }
104
}
105