Completed
Push — dot-language-processor ( 82cdb7...13d982 )
by Luis
14:10
created

GenerateClassDiagramCommand::runningProcessor()   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 1
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\GenerateClassDiagram;
11
use PhUml\Parser\CodeFinder;
12
use PhUml\Parser\TokenParser;
13
use PhUml\Processors\DotProcessor;
14
use PhUml\Processors\GraphvizProcessor;
15
use PhUml\Processors\NeatoProcessor;
16
use RuntimeException;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class GenerateClassDiagramCommand extends GeneratorCommand
23
{
24
    /** @throws \InvalidArgumentException */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('phuml:diagram')
29
            ->setDescription('Generate a class diagram scanning the given directory')
30
            ->setHelp(<<<HELP
31
Example:
32
    php bin/phuml phuml:diagram -r -a -p neato ./src out.png
33
34
    This example will scan the `./src` directory recursively for php files.
35
    It will process them with the option `associations` set to true. After that it 
36
    will be send to the `neato` processor and saved to the file `out.png`.
37
HELP
38
            )
39
            ->addOption(
40
                'recursive',
41
                'r',
42
                InputOption::VALUE_NONE,
43
                'Look for classes in the given directory recursively'
44
            )
45
            ->addOption(
46
                'processor',
47
                'p',
48
                InputOption::VALUE_REQUIRED,
49
                'Choose between the neato and dot processors'
50
            )
51
            ->addOption(
52
                'associations',
53
                'a',
54
                InputOption::VALUE_NONE,
55
                'If present, the Graphviz processor will generate association among classes'
56
            )
57
            ->addArgument(
58
                'directory',
59
                InputArgument::REQUIRED,
60
                'The directory to be scanned to generate the class diagram'
61
            )
62
            ->addArgument(
63
                'output',
64
                InputArgument::REQUIRED,
65
                'The file name for your class diagram'
66
            );
67
    }
68
69
    /**
70
     * @throws \LogicException
71
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
72
     * @throws \RuntimeException
73
     */
74
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        $directory = $input->getArgument('directory');
77
        $diagramFile = $input->getArgument('output');
78
        $recursive = (bool)$input->getOption('recursive');
79
        $associations = (bool)$input->getOption('associations');
80
        $processor = $input->getOption('processor');
81
82
        if (!is_dir($directory)) {
83
            throw new RuntimeException("'$directory' is not a valid directory");
84
        }
85
86
        $action = new GenerateClassDiagram(new TokenParser(), new GraphvizProcessor($associations));
87
        $action->attach($this->display);
88
89
        $finder = new CodeFinder();
90
        $finder->addDirectory($directory, $recursive);
91
92
        if (!\in_array($processor, ['neato', 'dot'], true)) {
93
            throw new RuntimeException("Expected processors are neato and dot, '$processor' found");
94
        }
95
96
        if ($processor === 'dot') {
97
            $action->setImageProcessor(new DotProcessor());
98
        } else {
99
            $action->setImageProcessor(new NeatoProcessor());
100
        }
101
102
        $output->writeln('[|] Running... (This may take some time)');
103
104
        $action->generate($finder, $diagramFile);
105
106
        return 0;
107
    }
108
}
109