Passed
Push — master ( 9bbc25...797142 )
by Luis
02:39
created

GenerateClassDiagramCommand::savingResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Console\Commands;
9
10
use PhUml\Actions\CanGenerateClassDiagram;
11
use PhUml\Actions\GenerateClassDiagram;
12
use PhUml\Parser\CodeFinder;
13
use PhUml\Parser\TokenParser;
14
use PhUml\Processors\DotProcessor;
15
use PhUml\Processors\GraphvizProcessor;
16
use PhUml\Processors\NeatoProcessor;
17
use PhUml\Processors\Processor;
18
use RuntimeException;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class GenerateClassDiagramCommand extends Command implements CanGenerateClassDiagram
26
{
27
    /** @var OutputInterface */
28
    private $output;
29
30
    /** @throws \InvalidArgumentException */
31 24
    protected function configure()
32
    {
33
        $this
34 24
            ->setName('phuml:diagram')
35 24
            ->setDescription('Generate a class diagram scanning the given directory')
36 24
            ->setHelp(<<<HELP
37 24
Example:
38
    php bin/phuml phuml:diagram -r -a -p neato ./src out.png
39
40
    This example will scan the `./src` directory recursively for php files.
41
    It will process them with the option `associations` set to true. After that it 
42
    will be send to the `neato` processor and saved to the file `out.png`.
43
HELP
44
            )
45 24
            ->addOption(
46 24
                'recursive',
47 24
                'r',
48 24
                InputOption::VALUE_NONE,
49 24
                'Look for classes in the given directory recursively'
50
            )
51 24
            ->addOption(
52 24
                'processor',
53 24
                'p',
54 24
                InputOption::VALUE_REQUIRED,
55 24
                'Choose between the neato and dot processors'
56
            )
57 24
            ->addOption(
58 24
                'associations',
59 24
                'a',
60 24
                InputOption::VALUE_NONE,
61 24
                'If present, the Graphviz processor will generate association among classes'
62
            )
63 24
            ->addArgument(
64 24
                'directory',
65 24
                InputArgument::REQUIRED,
66 24
                'The directory to be scanned to generate the class diagram'
67
            )
68 24
            ->addArgument(
69 24
                'output',
70 24
                InputArgument::REQUIRED,
71 24
                'The file name for your class diagram'
72
            );
73 24
    }
74
75
    /**
76
     * @throws \LogicException
77
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
78
     * @throws \RuntimeException
79
     */
80 12
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82 12
        $this->output = $output;
83
84 12
        $directory = $input->getArgument('directory');
85 12
        $diagramFile = $input->getArgument('output');
86 12
        $recursive = (bool)$input->getOption('recursive');
87 12
        $associations = (bool)$input->getOption('associations');
88 12
        $processor = $input->getOption('processor');
89
90 12
        if (!is_dir($directory)) {
91 3
            throw new RuntimeException("'$directory' is not a valid directory");
92
        }
93
94 9
        $action = new GenerateClassDiagram(new TokenParser(), new GraphvizProcessor($associations));
95 9
        $action->attach($this);
96
97 9
        $finder = new CodeFinder();
98 9
        $finder->addDirectory($directory, $recursive);
99
100 9
        if (!\in_array($processor, ['neato', 'dot'], true)) {
101 3
            throw new RuntimeException("Expected processors are neato and dot, '$processor' found");
102
        }
103
104 6
        if ($processor === 'dot') {
105 3
            $action->setImageProcessor(new DotProcessor());
106
        } else {
107 3
            $action->setImageProcessor(new NeatoProcessor());
108
        }
109
110 6
        $output->writeln('[|] Running... (This may take some time)');
111
112 6
        $action->generate($finder, $diagramFile);
113
114 6
        return 0;
115
    }
116
117 6
    public function runningParser(): void
118
    {
119 6
        $this->output->writeln('[|] Parsing class structure');
120 6
    }
121
122 6
    public function runningProcessor(Processor $processor): void
123
    {
124 6
        $this->output->writeln("[|] Running '{$processor->name()}' processor");
125 6
    }
126
127 6
    public function savingResult(): void
128
    {
129 6
        $this->output->writeln('[|] Writing generated data to disk');
130 6
    }
131
}
132