Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

RetryParser::getOutputContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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