Completed
Push — master ( 797142...deecf0 )
by Luis
06:15 queued 02:52
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 36
    protected function configure()
26
    {
27
        $this
28 36
            ->setName('phuml:diagram')
29 36
            ->setDescription('Generate a class diagram scanning the given directory')
30 36
            ->setHelp(<<<HELP
31 36
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 36
            ->addOption(
40 36
                'recursive',
41 36
                'r',
42 36
                InputOption::VALUE_NONE,
43 36
                'Look for classes in the given directory recursively'
44
            )
45 36
            ->addOption(
46 36
                'processor',
47 36
                'p',
48 36
                InputOption::VALUE_REQUIRED,
49 36
                'Choose between the neato and dot processors'
50
            )
51 36
            ->addOption(
52 36
                'associations',
53 36
                'a',
54 36
                InputOption::VALUE_NONE,
55 36
                'If present, the Graphviz processor will generate association among classes'
56
            )
57 36
            ->addArgument(
58 36
                'directory',
59 36
                InputArgument::REQUIRED,
60 36
                'The directory to be scanned to generate the class diagram'
61
            )
62 36
            ->addArgument(
63 36
                'output',
64 36
                InputArgument::REQUIRED,
65 36
                'The file name for your class diagram'
66
            );
67 36
    }
68
69
    /**
70
     * @throws \LogicException
71
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
72
     * @throws \RuntimeException
73
     */
74 12
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76 12
        $directory = $input->getArgument('directory');
77 12
        $diagramFile = $input->getArgument('output');
78 12
        $recursive = (bool)$input->getOption('recursive');
79 12
        $associations = (bool)$input->getOption('associations');
80 12
        $processor = $input->getOption('processor');
81
82 12
        if (!is_dir($directory)) {
83 3
            throw new RuntimeException("'$directory' is not a valid directory");
84
        }
85
86 9
        $action = new GenerateClassDiagram(new TokenParser(), new GraphvizProcessor($associations));
87 9
        $action->attach($this->display);
88
89 9
        $finder = new CodeFinder();
90 9
        $finder->addDirectory($directory, $recursive);
91
92 9
        if (!\in_array($processor, ['neato', 'dot'], true)) {
93 3
            throw new RuntimeException("Expected processors are neato and dot, '$processor' found");
94
        }
95
96 6
        if ($processor === 'dot') {
97 3
            $action->setImageProcessor(new DotProcessor());
98
        } else {
99 3
            $action->setImageProcessor(new NeatoProcessor());
100
        }
101
102 6
        $output->writeln('[|] Running... (This may take some time)');
103
104 6
        $action->generate($finder, $diagramFile);
105
106 6
        return 0;
107
    }
108
}
109