Completed
Push — master ( 1a3a48...6e0e69 )
by Alessandro
09:39 queued 07:07
created

GenericParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 70
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A handleLogItem() 0 11 2
B logMatches() 0 20 5
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 57
    public function __construct(
38
        TestResultFactory $testResultFactory,
39
        TestResultHandlerInterface $testResultContainer,
40
        string $status,
41
        string $messageStartsWith = null
42
    ) {
43 57
        $this->testResultFactory = $testResultFactory;
44 57
        $this->testResultContainer = $testResultContainer;
45 57
        $this->status = $status;
46 57
        $this->messageStartsWith = $messageStartsWith;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 52
    public function handleLogItem(AbstractParaunitProcess $process, \stdClass $logItem)
53
    {
54 52
        if ($this->logMatches($logItem)) {
55 41
            $testResult = $this->testResultFactory->createFromLog($logItem);
56 41
            $this->testResultContainer->handleTestResult($process, $testResult);
57
58 41
            return $testResult;
59
        }
60
61 35
        return null;
62
    }
63
64 44
    protected function logMatches(\stdClass $log): bool
65
    {
66 44
        if (! property_exists($log, 'status')) {
67 3
            return false;
68
        }
69
70 43
        if ($log->status !== $this->status) {
71 33
            return false;
72
        }
73
74 33
        if (null === $this->messageStartsWith) {
75 31
            return true;
76
        }
77
78 16
        if (! property_exists($log, 'message')) {
79
            return false;
80
        }
81
82 16
        return 0 === strpos($log->message, $this->messageStartsWith);
83
    }
84
}
85