Passed
Push — master ( fa352d...0eb7bd )
by BruceScrutinizer
02:09
created

AbstractValidateNamespaceCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace NamespaceProtector\Command;
4
5
use NamespaceProtector\Config\Config;
6
use NamespaceProtector\Common\FileSystemPath;
7
use Symfony\Component\Console\Command\Command;
8
use NamespaceProtector\OutputDevice\ConsoleDevice;
9
use Symfony\Component\Console\Input\InputArgument;
10
use NamespaceProtector\OutputDevice\GraphicsDevice;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use NamespaceProtector\Result\ResultProcessorInterface;
14
use NamespaceProtector\NamespaceProtectorProcessorFactory;
15
use NamespaceProtector\OutputDevice\OutputDeviceInterface;
16
17
abstract class AbstractValidateNamespaceCommand extends Command
18
{
19
    private const PLOTTER_ARGUMENT = 'plotter';
20
21
    public function __construct(string $name = null)
22
    {
23
        parent::__construct($name);
24
    }
25
26
    protected function configure(): void
27
    {
28
        parent::configure();
29
        $this->setName('validate-namespace')
30
            ->setDescription('Validate namespace')
31
            ->setHelp('Validate if some namespace access to one private namespace');
32
33
        $this
34
            ->addArgument(self::PLOTTER_ARGUMENT, InputArgument::OPTIONAL, 'How show the output?');
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $output->writeln('Boot validate analysis....');
40
        $config = Config::loadFromFile(new FileSystemPath(\getcwd() . '/namespace-protector-config.json'));
41
        $plotter = $input->getArgument(self::PLOTTER_ARGUMENT);
42
        if ($plotter && \is_string($plotter)) {
43
            $config = Config::fromConfigWithOverride($config, ['plotter' => $plotter]);
44
        }
45
46
        $factory = new NamespaceProtectorProcessorFactory();
47
        $namespaceProtectorProcessor = $factory->create($config);
48
49
        $namespaceProtectorProcessor->load();
50
51
        $output->writeln($config->print());
52
        $output->writeln('Load data...');
53
        $output->writeln('Loaded ' . $namespaceProtectorProcessor->getFilesLoaded() . ' files to validate');
54
        $output->writeln('Loaded ' . $namespaceProtectorProcessor->totalSymbolsLoaded() . ' built in symbols');
55
        $output->writeln('Start analysis...');
56
57
        /** @var ResultProcessorInterface $result */
58
        $result = $namespaceProtectorProcessor->process();
59
60
        $plotter = $this->createOutputObject($config, $output);
61
        $plotter->output($result);
62
63
        return self::SUCCESS;
64
    }
65
66
    private function createOutputObject(Config $config, OutputInterface $outputInterface): OutputDeviceInterface
67
    {
68
        if ($config->getPlotter() === Config::PLOTTER_TERMINAL) {
69
            return new ConsoleDevice($outputInterface);
70
        }
71
72
        return new GraphicsDevice();
73
    }
74
}
75