Passed
Push — 1.1 ( 13f027 )
by Luis
06:39
created

Generator::display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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\Generators;
9
10
use LogicException;
11
use PhUml\Code\Structure;
12
use PhUml\Parser\CodeFinder;
13
use PhUml\Parser\CodeParser;
14
use PhUml\Processors\Processor;
15
16
/**
17
 * All generators will see the console commands as listeners that will provide feedback
18
 * to the end users about their progress
19
 *
20
 * @see ProcessorProgressDisplay for the details about the events that are tracked
21
 */
22
abstract class Generator
23
{
24
    /** @var ProcessorProgressDisplay */
25
    private $display;
26
27
    /** @var CodeParser */
28
    private $parser;
29
30 66
    public function __construct(CodeParser $parser)
31
    {
32 66
        $this->parser = $parser;
33 66
    }
34
35 54
    public function attach(ProcessorProgressDisplay $display): void
36
    {
37 54
        $this->display = $display;
38 54
    }
39
40
    /** @throws LogicException */
41 60
    protected function display(): ProcessorProgressDisplay
42
    {
43 60
        if (!$this->display) {
44 12
            throw new LogicException('No display was attached');
45
        }
46 48
        return $this->display;
47
    }
48
49
    /**
50
     * @throws \LogicException If the command is missing
51
     */
52 48
    protected function parseCode(CodeFinder $finder): Structure
53
    {
54 48
        $this->display()->runningParser();
55 48
        return $this->parser->parse($finder);
56
    }
57
58 48
    protected function save(Processor $processor, string $content, string $path): void
59
    {
60 48
        $this->display()->savingResult();
61 48
        $processor->saveToFile($content, $path);
62 48
    }
63
}
64