Completed
Push — master ( 3f2b21...64f10e )
by Alessandro
06:51
created

RetryParser::buildRegexPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Process\ProcessWithResultsInterface;
6
use Paraunit\Process\RetryAwareInterface;
7
use Paraunit\TestResult\Interfaces\TestResultBearerInterface;
8
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface;
9
use Paraunit\TestResult\MuteTestResult;
10
11
/**
12
 * Class RetryParser
13
 * @package Paraunit\Parser
14
 */
15
class RetryParser implements JSONParserChainElementInterface
16
{
17
    /** @var TestResultHandlerInterface */
18
    private $testResultContainer;
19
20
    /** @var  int */
21
    private $maxRetryCount;
22
23
    /** @var  string */
24
    private $regexPattern;
25
26
    /**
27
     * RetryParser constructor.
28
     * @param TestResultHandlerInterface $testResultContainer
29
     * @param int $maxRetryCount
30
     */
31 39
    public function __construct(TestResultHandlerInterface $testResultContainer, $maxRetryCount = 3)
32
    {
33 39
        $this->testResultContainer = $testResultContainer;
34 39
        $this->maxRetryCount = $maxRetryCount;
35
36
        $patterns = array(
37 39
            'The EntityManager is closed',
38
            // MySQL
39 39
            'Deadlock found',
40 39
            'Lock wait timeout exceeded',
41
            // SQLite
42 39
            'General error: 5 database is locked',
43 39
        );
44
45 39
        $this->regexPattern = $this->buildRegexPattern($patterns);
46 39
    }
47
48 35
    public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem)
49
    {
50 35
        if ($this->isRetriable($process) && $this->isToBeRetried($logItem)) {
51
            /** @var RetryAwareInterface | TestResultBearerInterface $process */
52 7
            $process->markAsToBeRetried();
53 7
            $testResult = new MuteTestResult();
54 7
            $this->testResultContainer->handleTestResult($process, $testResult);
55
56 7
            return $testResult;
57
        }
58
59 31
        return null;
60
    }
61
62
    /**
63
     * @param TestResultBearerInterface $process
64
     * @return bool
65
     */
66 35
    private function isRetriable(TestResultBearerInterface $process)
67
    {
68 35
        return $process instanceof RetryAwareInterface && $process->getRetryCount() < $this->maxRetryCount;
69
    }
70
71
    /**
72
     * @param \stdClass $log
73
     * @return bool
74
     */
75 34
    private function isToBeRetried(\stdClass $log)
76
    {
77 34
        return property_exists($log, 'status') && $log->status === 'error' && preg_match($this->regexPattern, $log->message);
78
    }
79
80
    /**
81
     * @param array | string[] $patterns
82
     * @return string
83
     */
84 39
    private function buildRegexPattern(array $patterns)
85
    {
86 39
        $regex = implode('|', $patterns);
87
88 39
        return '/' . $regex . '/';
89
    }
90
}
91