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

RetryParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
c 5
b 1
f 0
lcom 1
cbo 2
dl 0
loc 85
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getOutputContainer() 0 4 1
A parsingFoundResult() 0 14 4
A isToBeRetried() 0 4 2
A hasNotExceededRetryCount() 0 4 1
A buildRegexPattern() 0 6 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