Completed
Push — master ( d308b8...b73d2a )
by Alessandro
08:43
created

GenericParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 74
rs 10

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