Completed
Branch output_parsers_refactor (69b7d7)
by Alessandro
02:50
created

AbstractParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 81
ccs 29
cts 30
cp 0.9667
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getOutputContainer() 0 4 1
A storeParsedBlocks() 0 17 2
A parsingFoundSomething() 0 10 2
A parse() 0 7 1
A assertConstantExist() 0 6 2
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Printer\OutputContainer;
6
use Paraunit\Process\ProcessResultInterface;
7
8
/**
9
 * Class AbstractParser
10
 * @package Paraunit\Parser
11
 */
12
abstract class AbstractParser implements ProcessOutputParserChainElementInterface, OutputContainerBearerInterface
13
{
14
    /** @var  OutputContainer */
15
    protected $outputContainer;
16
17
    /**
18
     * AbstractParser constructor.
19
     */
20 37
    public function __construct()
21
    {
22 37
        $this->assertConstantExist('TAG');
23 37
        $this->assertConstantExist('TITLE');
24 37
        $this->assertConstantExist('PARSING_REGEX');
25
26 37
        $this->outputContainer = new OutputContainer(static::TAG, static::TITLE);
27 37
    }
28
29
    /**
30
     * @return OutputContainer
31
     */
32 26
    public function getOutputContainer()
33
    {
34 26
        return $this->outputContainer;
35
    }
36
37
    /**
38
     * @param ProcessResultInterface $process
39
     * @return bool
40
     */
41 24
    protected function storeParsedBlocks(ProcessResultInterface $process)
42
    {
43 24
        $parsedBlob = $this->parse($process);
44
45 24
        if (isset($parsedBlob[1])) {
46 10
            $parsedBlocks = preg_split('/^\d+\) /m', $parsedBlob[1]);
47
            // il primo è sempre vuoto a causa dello split
48 10
            unset($parsedBlocks[0]);
49
50 10
            $this->outputContainer->addToOutputBuffer($parsedBlocks);
51 10
            $this->outputContainer->addFileName($process->getFilename());
52
53 10
            return true;
54
        }
55
56 18
        return false;
57
    }
58
59
    /**
60
     * @param ProcessResultInterface $process
61
     * @return bool
62
     */
63 18
    protected function parsingFoundSomething(ProcessResultInterface $process)
64
    {
65 18
        if (count($this->parse($process)) > 0) {
66 4
            $this->outputContainer->addFileName($process->getFilename());
67
68 4
            return true;
69
        }
70
71 15
        return false;
72
    }
73
74 37
    private function parse(ProcessResultInterface $process)
75
    {
76 37
        $parsedBlob = array();
77 37
        preg_match(static::PARSING_REGEX, $process->getOutput(), $parsedBlob);
78
79 37
        return $parsedBlob;
80
    }
81
82
    /**
83
     * @param $constantName
84
     * @throws \Exception
85
     */
86 37
    private function assertConstantExist($constantName)
87
    {
88 37
        if ( ! defined('static::' . $constantName)) {
89
            throw new \Exception('Missing ' .$constantName . ' constant in ' . get_class($this));
90
        }
91 37
    }
92
}
93