Completed
Push — master ( d99440...b71151 )
by Hunter
13:25
created

ConvertCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace HSkrasek\OpenAPI\Command;
4
5
use League\Flysystem\FilesystemInterface;
6
use League\Pipeline\Pipeline;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ConvertCommand extends Command
13
{
14
    /**
15
     * @var FilesystemInterface
16
     */
17
    private $filesystem;
18
19
    /**
20
     * @var Pipeline
21
     */
22
    private $pipeline;
23
24
    public function __construct(FilesystemInterface $filesystem, Pipeline $pipeline)
25
    {
26
        parent::__construct();
27
28
        $this->filesystem = $filesystem;
29
        $this->pipeline   = $pipeline;
30
    }
31
32
    protected function configure()
33
    {
34
        $this->setName('convert')
35
            ->setDescription('Convert OpenAPI 3 schema to JSON Schema')
36
            ->setHelp('This command allows you to convert a directory of OpenAPI 3 schema files to JSON Schema files')
37
            ->addArgument(
38
                'input',
39
                InputArgument::REQUIRED,
40
                'The directory containing all the OpenAPI 3 schema files'
41
            )
42
            ->addArgument(
43
                'output',
44
                InputArgument::REQUIRED,
45
                'The directory to output the converted JSON Schema files'
46
            );
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $oasDirectory        = $input->getArgument('input');
52
        $jsonSchemaDirectory = $input->getArgument('output');
53
54
        if (!$this->filesystem->has($oasDirectory)) {
55
            $output->writeln("<error>OpenAPI Schema Directory does not exist ($oasDirectory)</error>");
56
57
            return -1;
58
        }
59
60
        if (!$this->filesystem->has($jsonSchemaDirectory)) {
61
            $output->writeln('<info>JSON Schema output directory does not exist, creating...</info>');
62
            $this->filesystem->createDir($jsonSchemaDirectory);
63
        }
64
65
        $convertedFiles = $this->pipeline->process([$oasDirectory, $jsonSchemaDirectory]);
66
67
        $output->writeln("<info>Successfully converted $convertedFiles schema files</info>");
68
    }
69
}
70