Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

AbstractParser::createMessageFromLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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 $title;
19
20
    /** @var  string */
21
    protected $status;
22
23
    /** @var  string */
24
    protected $messageStartsWith;
25
26
    /**
27
     * AbstractParser constructor.
28
     *
29
     * @param OutputContainerInterface $outputContainer
30
     * @param string $status The status that the parser should catch
31
     * @param string | null $messageStartsWith The start of the message that the parser should look for
32
     */
33 20
    public function __construct(OutputContainerInterface $outputContainer, $status, $messageStartsWith = null)
34
    {
35 20
        $this->outputContainer = $outputContainer;
36 20
        $this->status = $status;
37 20
        $this->messageStartsWith = $messageStartsWith;
38 20
    }
39
40
    /**
41
     * @return OutputContainerInterface
42
     */
43 20
    public function getOutputContainer()
44
    {
45 20
        return $this->outputContainer;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 15
    public function parsingFoundResult(ProcessResultInterface $process, \stdClass $log)
52
    {
53 15
        if ($log->status != $this->status) {
54 10
            return false;
55
        }
56
57 15
        if ($this->checkMessageStart($log)) {
58 15
            $process->addTestResult($this->getOutputContainer()->getSingleResultMarker());
59 15
            $this->outputContainer->addToOutputBuffer($process, $this->createMessageFromLog($log));
60
61 15
            return true;
62
        }
63
64 7
        return false;
65
    }
66
67
    /**
68
     * @param \stdClass $log
69
     * @return bool
70
     */
71 15
    private function checkMessageStart(\stdClass $log)
72
    {
73 15
        if (is_null($this->messageStartsWith)) {
74 15
            return true;
75
        }
76
77 8
        if ( ! property_exists($log, 'message')) {
78
            return false;
79
        }
80
81 8
        return 0 === strpos($log->message, $this->messageStartsWith);
82
    }
83
84
    /**
85
     * @param \stdClass $log
86
     * @return string
87
     */
88 15
    private function createMessageFromLog(\stdClass $log)
89
    {
90 15
        $stackTrace = '';
91
92 15
        foreach ($log->trace as $step) {
93 8
            $stackTrace .= "\n" . $step->file . ':' . $step->line;
94 15
        }
95
96 15
        return $log->test . "\n" . $log->message . "\n" . $stackTrace;
97
    }
98
}
99