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   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 75
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A attemptsHaveExceededLimit() 0 3 1
A transactionLevelWillCauseProblems() 0 7 2
A __construct() 0 4 1
A checkAndSleep() 0 18 4
A createWithConfigParams() 0 6 1
A exceptionIsNotRelatedToLostConnection() 0 10 4
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