Completed
Push — symfony-console-application ( 3187e2...c3ee2a )
by Luis
10:39
created

plPhuml::addProcessor()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use PhUml\Parser\CodeFinder;
4
use PhUml\Parser\TokenParser;
5
use PhUml\Processors\InvalidInitialProcessor;
6
use PhUml\Processors\InvalidProcessorChain;
7
use PhUml\Processors\Processor;
8
9
class plPhuml
10
{
11
    /** @var TokenParser */
12
    private $parser;
13
14
    /** @var string[] */
15
    private $files;
16
17
    /** @var Processor[] */
18
    private $processors;
19
20
    /** @var CodeFinder */
21
    private $finder;
22
23
    public function __construct(TokenParser $parser = null, CodeFinder $finder = null)
24
    {
25
        $this->parser = $parser ?? new TokenParser();
26
        $this->finder = $finder ?? new CodeFinder();
27
        $this->processors = [];
28
        $this->files = [];
29
    }
30
31
    public function addDirectory(string $directory, bool $recursive = true): void
32
    {
33
        $this->finder->addDirectory($directory, $recursive);
34
    }
35
36
    /** @return string[] */
37
    public function files(): array
38
    {
39
        return $this->finder->files();
40
    }
41
42
    /**
43
     * @throws InvalidInitialProcessor
44
     * @throws InvalidProcessorChain
45
     */
46
    public function addProcessor(Processor $processor): void
47
    {
48
        if (count($this->processors) === 0 && !$processor->isInitial()) {
49
            throw InvalidInitialProcessor::given($processor);
50
        }
51
        $lastProcessor = end($this->processors);
52
        if (count($this->processors) > 0 && !$lastProcessor->isCompatibleWith($processor)) {
53
            throw InvalidProcessorChain::with($lastProcessor, $processor);
54
        }
55
        $this->processors[] = $processor;
56
    }
57
58
    public function generate($outfile): void
59
    {
60
        echo "[|] Parsing class structure\n";
61
        $structure = $this->parser->parse($this->finder);
62
63
        $input = $structure;
64
        foreach ($this->processors as $processor) {
65
            echo "[|] Running '{$processor->name()}' processor\n";
66
            $input = $processor->process($input);
67
        }
68
69
        echo "[|] Writing generated data to disk\n";
70
        end($this->processors)->writeToDisk($input, $outfile);
71
    }
72
}
73