GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#160)
by joseph
22:55
created

ShouldConnectionByRetried::checkAndSleep()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 4
dl 0
loc 18
ccs 0
cts 17
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
4
namespace EdmondsCommerce\DoctrineStaticMeta\EntityManager\RetryConnection;
5
6
/**
7
 * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
8
 */
9
class ShouldConnectionByRetried
10
{
11
    public const KEY_USE_RECONNECT = 'should-reconnect';
12
    public const KEY_RECONNECT_ATTEMPTS = 'reconnect-attempts';
13
    public const KEY_RECONNECT_TIMEOUT = 'reconnect-timeout';
14
    /**
15
     * @var int
16
     */
17
    private $allowedAttempts;
18
    /**
19
     * @var int
20
     */
21
    private $timeout;
22
23
    private function __construct(int $allowedAttempts, int $timeout)
24
    {
25
        $this->allowedAttempts = $allowedAttempts;
26
        $this->timeout         = $timeout;
27
    }
28
29
    public static function createWithConfigParams(array $config): ShouldConnectionByRetried
30
    {
31
        $allowedAttempts = $config['driverOptions'][self::KEY_RECONNECT_ATTEMPTS] ?? 3;
32
        $timeout         = $config['driverOptions'][self::KEY_RECONNECT_TIMEOUT] ?? 5;
33
34
        return new self($allowedAttempts, $timeout);
35
    }
36
37
    public function checkAndSleep(
38
        \Exception $exception,
39
        int $transactionNestingLevel,
40
        int $numberOfAttempts,
41
        bool $ignoreTransactionLevel = false
42
    ): bool {
43
        switch (true) {
44
            case $this->attemptsHaveExceededLimit($numberOfAttempts):
45
            case $this->transactionLevelWillCauseProblems($ignoreTransactionLevel, $transactionNestingLevel):
46
            case $this->exceptionIsNotRelatedToLostConnection($exception):
47
                $retry = false;
48
                break;
49
            default:
50
                sleep($this->timeout);
51
                $retry = true;
52
        }
53
54
        return $retry;
55
    }
56
57
58
    private function attemptsHaveExceededLimit(int $numberOfAttempts): bool
59
    {
60
        return $numberOfAttempts > $this->allowedAttempts;
61
    }
62
63
    private function exceptionIsNotRelatedToLostConnection(\Exception $exception): bool
64
    {
65
        $message = $exception->getMessage();
66
        switch (true) {
67
            case \ts\stringContains($message, 'MySQL server has gone away'):
68
            case \ts\stringContains($message, 'Lost connection to MySQL server during query'):
69
            case \ts\stringContains($message, 'Connection timed out'):
70
                return false;
71
            default:
72
                return true;
73
        }
74
    }
75
76
77
    private function transactionLevelWillCauseProblems(bool $ignoreTransactionLevel, int $transactionNestingLevel): bool
78
    {
79
        if ($ignoreTransactionLevel === true) {
80
            return false;
81
        }
82
83
        return $transactionNestingLevel === 0;
84
    }
85
}
86