Passed
Pull Request — master (#46)
by Théo
02:11
created

Validate::execute()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.7449
c 0
b 0
f 0
cc 6
eloc 31
nc 12
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Console\Command;
16
17
use Exception;
18
use KevinGH\Box\Json\Json;
19
use KevinGH\Box\Json\JsonValidationException;
20
use Symfony\Component\Console\Exception\RuntimeException;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
final class Validate extends Configurable
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure(): void
31
    {
32
        parent::configure();
33
34
        $this->setName('validate');
35
        $this->setDescription('Validates the configuration file');
36
        $this->setHelp(
37
            <<<'HELP'
38
The <info>%command.name%</info> command will validate the configuration file
39
and report any errors found, if any.
40
<comment>
41
  This command relies on a configuration file for loading
42
  PHAR packaging settings. If a configuration file is not
43
  specified through the <info>--configuration|-c</info> option, one of
44
  the following files will be used (in order): <info>box.json,
45
  box.json.dist</info>
46
</comment>
47
HELP
48
        );
49
        $this->addArgument(
50
            'file',
51
            InputArgument::OPTIONAL,
52
            'The configuration file. (default: box.json, box.json.dist)'
53
        );
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output): int
60
    {
61
        try {
62
            $this->getConfig($input);
63
64
            $output->writeln(
65
                '<info>The configuration file passed validation.</info>'
66
            );
67
68
            return 0;
69
        } catch (Exception $exception) {
70
            // Continue
71
        }
72
73
        if ($output->isVerbose()) {
74
            throw new RuntimeException(
75
                sprintf(
76
                    'The configuration file failed validation: %s',
77
                    $exception->getMessage()
78
                ),
79
                $exception->getCode(),
80
                $exception
81
            );
82
        }
83
84
        if ($exception instanceof JsonValidationException) {
85
            $output->writeln(
86
                sprintf(
87
                    '<error>The configuration file failed validation: "%s" does not match the expected JSON '
88
                    .'schema:</error>',
89
                    $exception->getValidatedFile()
90
                )
91
            );
92
93
            $output->writeln('');
94
95
            foreach ($exception->getErrors() as $error) {
96
                $output->writeln("<comment>  - $error</comment>");
97
            }
98
        } else {
99
            $errorMessage = isset($exception)
100
                ? sprintf('The configuration file failed validation: %s', $exception->getMessage())
101
                : 'The configuration file failed validation.'
102
            ;
103
104
            $output->writeln(
105
                sprintf(
106
                    '<error>%s</error>',
107
                    $errorMessage
108
                )
109
            );
110
        }
111
112
        return 1;
113
    }
114
}
115