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

ConvertCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 2
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 16 1
A execute() 0 20 3
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