Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

AbstractParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.45%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 8
c 6
b 0
f 0
lcom 1
cbo 2
dl 0
loc 77
ccs 21
cts 22
cp 0.9545
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getOutputContainer() 0 4 1
A parsingFoundResult() 0 15 3
A checkMessageStart() 0 12 3
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Printer\OutputContainerInterface;
6
use Paraunit\Process\ProcessResultInterface;
7
8
/**
9
 * Class AbstractParser
10
 * @package Paraunit\Parser
11
 */
12
class AbstractParser implements JSONParserChainElementInterface, OutputContainerBearerInterface
13
{
14
    /** @var  OutputContainerInterface */
15
    protected $outputContainer;
16
17
    /** @var  string */
18
    protected $singleResultMarker;
19
20
    /** @var  string */
21
    protected $title;
22
23
    /** @var  string */
24
    protected $status;
25
26
    /** @var  string */
27
    protected $messageStartsWith;
28
29
    /**
30
     * AbstractParser constructor.
31
     *
32
     * @param OutputContainerInterface $outputContainer
33
     * @param string $singleResultMarker The output of the single test result (.FERW etc)
34
     * @param string $status The status that the parser should catch
35
     * @param string | null $messageStartsWith The start of the message that the parser should look for
36
     */
37 18
    public function __construct(OutputContainerInterface $outputContainer, $singleResultMarker, $status, $messageStartsWith = null)
38
    {
39 18
        $this->outputContainer = $outputContainer;
40 18
        $this->singleResultMarker = $singleResultMarker;
41 18
        $this->status = $status;
42 18
        $this->messageStartsWith = $messageStartsWith;
43 18
    }
44
45
    /**
46
     * @return OutputContainerInterface
47
     */
48 9
    public function getOutputContainer()
49
    {
50 9
        return $this->outputContainer;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 14
    public function parsingFoundResult(ProcessResultInterface $process, \stdClass $log)
57
    {
58 14
        if ($log->status != $this->status) {
59 10
            return false;
60
        }
61
62 14
        if ($this->checkMessageStart($log)) {
63 14
            $process->addTestResult($this->singleResultMarker);
64 14
            $this->outputContainer->addToOutputBuffer($process, $log->message);
65
66 14
            return true;
67
        }
68
69 7
        return false;
70
    }
71
72
    /**
73
     * @param \stdClass $log
74
     * @return bool
75
     */
76 14
    private function checkMessageStart(\stdClass $log)
77
    {
78 14
        if (is_null($this->messageStartsWith)) {
79 14
            return true;
80
        }
81
82 8
        if ( ! property_exists($log, 'message')) {
83
            return false;
84
        }
85
86 8
        return 0 === strpos($log->message, $this->messageStartsWith);
87
    }
88
}
89