Completed
Push — master ( 0e338e...cdc67b )
by Alessandro
09:41 queued 04:45
created

AbstractParser::handleLogItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Process\ProcessWithResultsInterface;
6
use Paraunit\TestResult\TestResultFactory;
7
8
/**
9
 * Class AbstractParser
10
 * @package Paraunit\Parser
11
 */
12
class AbstractParser implements JSONParserChainElementInterface
13
{
14
    /** @var  TestResultFactory */
15
    protected $testResultFactory;
16
17
    /** @var  string */
18
    protected $status;
19
20
    /** @var  string */
21
    protected $messageStartsWith;
22
23
    /**
24
     * AbstractParser constructor.
25
     *
26
     * @param TestResultFactory $testResultFactory
27
     * @param string $status The status that the parser should catch
28
     * @param string | null $messageStartsWith The start of the message that the parser should look for, if any
29
     */
30 50
    public function __construct(TestResultFactory $testResultFactory, $status, $messageStartsWith = null)
31
    {
32 50
        $this->testResultFactory = $testResultFactory;
33 50
        $this->status = $status;
34 50
        $this->messageStartsWith = $messageStartsWith;
35 50
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 42
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
41
    {
42 42
        if ($this->logMatches($logItem)) {
43 31
            return $this->testResultFactory->createFromLog($logItem);
44
        }
45
46 29
        return null;
47
    }
48
49
    /**
50
     * @param \stdClass $log
51
     * @return bool
52
     */
53 34
    protected function logMatches(\stdClass $log)
54
    {
55 34
        if ( ! property_exists($log, 'status')) {
56 2
            return false;
57
        }
58
59 33
        if ($log->status != $this->status) {
60 27
            return false;
61
        }
62
63 23
        if (is_null($this->messageStartsWith)) {
64 21
            return true;
65
        }
66
67 12
        if ( ! property_exists($log, 'message')) {
68
            return false;
69
        }
70
71 12
        return 0 === strpos($log->message, $this->messageStartsWith);
72
    }
73
}
74