Completed
Push — master ( e834bf...05c340 )
by Alessandro
15s
created

GenericParser::logMatches()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 10
cp 0.9
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 1
crap 5.025
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\Parser\JSON;
6
7
use Paraunit\Process\AbstractParaunitProcess;
8
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface;
9
use Paraunit\TestResult\TestResultFactory;
10
11
/**
12
 * Class GenericParser
13
 * @package Paraunit\Parser\JSON
14
 */
15
class GenericParser implements ParserChainElementInterface
16
{
17
    /** @var TestResultFactory */
18
    protected $testResultFactory;
19
20
    /** @var TestResultHandlerInterface */
21
    protected $testResultContainer;
22
23
    /** @var string */
24
    protected $status;
25
26
    /** @var string|null */
27
    protected $messageStartsWith;
28
29
    /**
30
     * GenericParser constructor.
31
     *
32
     * @param TestResultFactory $testResultFactory
33
     * @param TestResultHandlerInterface $testResultContainer
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, if any
36
     */
37 56
    public function __construct(
38
        TestResultFactory $testResultFactory,
39
        TestResultHandlerInterface $testResultContainer,
40
        string $status,
41
        string $messageStartsWith = null
42
    ) {
43 56
        $this->testResultFactory = $testResultFactory;
44 56
        $this->testResultContainer = $testResultContainer;
45 56
        $this->status = $status;
46 56
        $this->messageStartsWith = $messageStartsWith;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 51
    public function handleLogItem(AbstractParaunitProcess $process, \stdClass $logItem)
53
    {
54 51
        if ($this->logMatches($logItem)) {
55 40
            $testResult = $this->testResultFactory->createFromLog($logItem);
56 40
            $this->testResultContainer->handleTestResult($process, $testResult);
57
58 40
            return $testResult;
59
        }
60
61 34
        return null;
62
    }
63
64 43
    protected function logMatches(\stdClass $log): bool
65
    {
66 43
        if (! property_exists($log, 'status')) {
67 3
            return false;
68
        }
69
70 42
        if ($log->status !== $this->status) {
71 32
            return false;
72
        }
73
74 32
        if (null === $this->messageStartsWith) {
75 30
            return true;
76
        }
77
78 15
        if (! property_exists($log, 'message')) {
79
            return false;
80
        }
81
82 15
        return 0 === strpos($log->message, $this->messageStartsWith);
83
    }
84
}
85