RetryParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 71
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A processWillBeRetried() 0 18 4
A containsRetriableError() 0 6 3
A buildRegexPattern() 0 6 1
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\MuteTestResult;
10
11
/**
12
 * Class RetryParser
13
 * @package Paraunit\Parser\JSON
14
 */
15
class RetryParser
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 55
    public function __construct(TestResultHandlerInterface $testResultContainer, int $maxRetryCount = 3)
32
    {
33 55
        $this->testResultContainer = $testResultContainer;
34 55
        $this->maxRetryCount = $maxRetryCount;
35
36
        $patterns = [
37 55
            'The EntityManager is closed',
38
            // MySQL
39
            'Deadlock found',
40
            'Lock wait timeout exceeded',
41
            'SAVEPOINT \w+ does not exist',
42
            // SQLite
43
            'General error: 5 database is locked',
44
        ];
45
46 55
        $this->regexPattern = $this->buildRegexPattern($patterns);
47
    }
48
49 50
    public function processWillBeRetried(AbstractParaunitProcess $process, array $logs): bool
50
    {
51 50
        if ($process->getRetryCount() >= $this->maxRetryCount) {
52 7
            return false;
53
        }
54
55 49
        foreach ($logs as $logItem) {
56 49
            if ($this->containsRetriableError($logItem)) {
57 11
                $process->markAsToBeRetried();
58 11
                $testResult = new MuteTestResult();
59 11
                $this->testResultContainer->handleTestResult($process, $testResult);
60
61 49
                return true;
62
            }
63
        }
64
65 40
        return false;
66
    }
67
68 49
    private function containsRetriableError(\stdClass $log): bool
69
    {
70 49
        return property_exists($log, 'status')
71 49
            && $log->status === 'error'
72 49
            && preg_match($this->regexPattern, $log->message);
73
    }
74
75
    /**
76
     * @param string[] $patterns
77
     * @return string
78
     */
79 55
    private function buildRegexPattern(array $patterns): string
80
    {
81 55
        $regex = implode('|', $patterns);
82
83 55
        return '/' . $regex . '/';
84
    }
85
}
86