ConfigValidateCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 61
ccs 23
cts 25
cp 0.92
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 20 1
A execute() 0 23 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * neuralyzer : Data Anonymization Library and CLI Tool
7
 *
8
 * PHP Version 7.2
9
 *
10
 * @author Emmanuel Dyan
11
 * @author Rémi Sauvat
12
 *
13
 * @copyright 2020 Emmanuel Dyan
14
 *
15
 * @package edyan/neuralyzer
16
 *
17
 * @license GNU General Public License v2.0
18
 *
19
 * @link https://github.com/edyan/neuralyzer
20
 */
21
22
namespace Edyan\Neuralyzer\Console\Commands;
23
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Yaml\Yaml;
29
30
/**
31
 * Command to validate a config file
32
 */
33
class ConfigValidateCommand extends Command
34
{
35
    /**
36
     * Set the command shortcut to be used in configuration
37
     *
38
     * @var string
39
     */
40
    protected $command = 'config:validate';
41
42
    protected function configure(): void
43 28
    {
44
        // First command : Test the DB Connexion
45
        $this->setName($this->command)
46 28
            ->setDescription(
47 28
                'Validate a configuration for the Anonymizer'
48 28
            )->setHelp(
49 28
                'This command will validate that the configuration provided is valid' . PHP_EOL .
50 28
                "Usage: neuralyzer <info>{$this->command}</info> -f neuralyzer.yml"
51 28
            )->addOption(
52 28
                'file',
53 28
                'f',
54 28
                InputOption::VALUE_REQUIRED,
55 28
                'File',
56 28
                'neuralyzer.yml'
57 28
            )->addOption(
58
                'dump',
59 28
                'D',
60
                InputOption::VALUE_NONE,
61
                'Dump full configuration'
62
            );
63
    }
64
65
    /**
66
     * Execute the command
67
     *
68
     * @throws \Edyan\Neuralyzer\Exception\NeuralyzerConfigurationException
69
     * @throws \Edyan\Neuralyzer\Exception\NeuralyzerException
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output): int
72 3
    {
73
        try {
74
            $reader = new \Edyan\Neuralyzer\Configuration\Reader($input->getOption('file'));
75 3
        } catch (\Symfony\Component\Config\Definition\Exception\InvalidConfigurationException $e) {
76 2
            throw new \Edyan\Neuralyzer\Exception\NeuralyzerConfigurationException($e->getMessage());
77 1
        } catch (\Symfony\Component\Config\Exception\FileLocatorFileNotFoundException $e) {
78 1
            throw new \Edyan\Neuralyzer\Exception\NeuralyzerException($e->getMessage());
79 1
        }
80
81
        if (! empty($reader->getDepreciationMessages())) {
82 1
            foreach ($reader->getDepreciationMessages() as $message) {
83
                $output->writeln("<comment>WARNING : ${message}</comment>");
84
            }
85
        }
86
87
        $output->writeln('<info>Your config is valid !</info>');
88 1
89 1
        if ($input->getOption('dump') === true) {
90
            $output->writeln(Yaml::dump($reader->getConfigValues(), 4));
91
        }
92
93
        return Command::SUCCESS;
94
    }
95
}
96