Completed
Pull Request — master (#37)
by Alessandro
02:23
created

AbstractParser::checkMessageStart()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.0416
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 49
    public function __construct(TestResultFactory $testResultFactory, $status, $messageStartsWith = null)
31
    {
32 49
        $this->testResultFactory = $testResultFactory;
33 49
        $this->status = $status;
34 49
        $this->messageStartsWith = $messageStartsWith;
35 49
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 41
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
41
    {
42 41
        if ($this->logMatches($logItem)) {
43 31
            return $this->testResultFactory->createFromLog($logItem);
44
        }
45
46 28
        return null;
47
    }
48
49
    /**
50
     * @param \stdClass $log
51
     * @return bool
52
     */
53 33
    protected function logMatches(\stdClass $log)
54
    {
55 33
        if ( ! property_exists($log, 'status')) {
56 2
            return false;
57
        }
58
59 32
        if ($log->status != $this->status) {
60 26
            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