ValidateCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 12 2
A __construct() 0 5 1
A configure() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\Console\Command;
6
7
use Dandelion\Configuration\ConfigurationValidatorInterface;
8
use Exception;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
use function sprintf;
14
15
class ValidateCommand extends Command
16
{
17
    public const NAME = 'validate';
18
    public const DESCRIPTION = 'Validates dandelion.json.';
19
20
    /**
21
     * @var \Dandelion\Configuration\ConfigurationValidatorInterface
22
     */
23
    protected $configurationValidator;
24
25
    /**
26
     * @param \Dandelion\Configuration\ConfigurationValidatorInterface $configurationValidator
27
     */
28
    public function __construct(
29
        ConfigurationValidatorInterface $configurationValidator
30
    ) {
31
        parent::__construct();
32
        $this->configurationValidator = $configurationValidator;
33
    }
34
35
    /**
36
     * @return void
37
     */
38
    protected function configure(): void
39
    {
40
        parent::configure();
41
42
        $this->setName(static::NAME);
43
        $this->setDescription(static::DESCRIPTION);
44
    }
45
46
47
    /**
48
     * @param \Symfony\Component\Console\Input\InputInterface $input
49
     * @param \Symfony\Component\Console\Output\OutputInterface $output
50
     *
51
     * @return int
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output): int
54
    {
55
        try {
56
            $this->configurationValidator->validate();
57
        } catch (Exception $e) {
58
            $output->writeln('Configuration is invalid.');
59
            $output->writeln(sprintf('<fg=red>%s</>', $e->getMessage()));
60
            return 1;
61
        }
62
63
        $output->writeln('Configuration is valid.');
64
        return 0;
65
    }
66
}
67