Completed
Pull Request — master (#94)
by Alessandro
04:33
created

GenericParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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