GenerateGraphCommand::getCommandDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace DependencyAnalyzer\Commands;
5
6
use DependencyAnalyzer\DependencyGraph;
7
use DependencyAnalyzer\Inspector\GraphFormatter\UmlFormatter;
8
use DependencyAnalyzer\Exceptions\InvalidCommandArgumentException;
9
use DependencyAnalyzer\Inspector\RuleViolationDetector\Component;
10
use DependencyAnalyzer\DependencyGraph\StructuralElementPatternMatcher;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class GenerateGraphCommand extends AnalyzeDependencyCommand
16
{
17
    protected $output;
18
    protected $ruleDefinition = [];
19
20
    /**
21
     * @var Component[]
22
     */
23
    protected $components = [];
24
25
    protected function getCommandName(): string
26
    {
27
        return 'graph';
28
    }
29
30
    protected function getCommandDescription(): string
31
    {
32
        return 'generate dependency map graph';
33
    }
34
35
    protected function configure(): void
36
    {
37
        parent::configure();
38
        $this->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'output path of graph image file');
39
        $this->addOption('rule', 'r', InputOption::VALUE_REQUIRED, 'Rule file');
40
    }
41
42
    protected function initialize(InputInterface $input, OutputInterface $output)
43
    {
44
        parent::initialize($input, $output);
45
        $this->output = $input->getOption('output');
46
47
        if ($ruleFile = $input->getOption('rule')) {
48
            if (!is_file($ruleFile)) {
49
                throw new InvalidCommandArgumentException(sprintf('rule is not file "%s".', $ruleFile));
50
            }
51
            $this->ruleDefinition = require_once $ruleFile;
52
            if (!is_array($this->ruleDefinition)) {
53
                throw new InvalidCommandArgumentException(sprintf('rule is invalid file "%s".', $ruleFile));
54
            }
55
56
            foreach (array_values($this->ruleDefinition)[0] as $componentName => $componentsDefinition) {
57
                $component = new Component($componentName, new StructuralElementPatternMatcher($componentsDefinition['define']));
58
                foreach ($componentsDefinition['graph'] ?? [] as $graphOption) {
59
                    $component->setAttribute($graphOption, true);
60
                }
61
                $this->components[] = $component;
62
            }
63
        }
64
    }
65
66
    protected function inspectDependencyGraph(DependencyGraph $graph, OutputInterface $output): int
67
    {
68
        $formatter = new UmlFormatter($graph, $this->components);
69
70
        $outputFile = new \SplFileObject($this->output, 'w');
71
        $outputFile->fwrite($formatter->format());
72
73
        $output->writeln("generated graph: {$this->output}");
74
75
        return 0;
76
    }
77
}
78