Completed
Push — master ( d1a78a...0cf24a )
by Guillaume
02:47
created

Backoff::delay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Hogosha\Monitor\Middleware;
4
5
use GuzzleHttp\Exception\ConnectException;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
10
/**
11
 * Class Backoff.
12
 */
13
class Backoff
14
{
15
    /**
16
     * decider.
17
     *
18
     * @return callable
19
     */
20
    public static function decider()
21
    {
22
        return function (
23
            $retries,
24
            Request $request,
25
            Response $response = null,
26
            RequestException $exception = null
27
        ) {
28
            // Limit to 5 retry max
29
            if ($retries >= 3) {
30
                return false;
31
            }
32
33
            // Retry connection exceptions
34
            if ($exception instanceof ConnectException) {
35
                return true;
36
            }
37
38
            if ($response) {
39
                // Retry if we have a serve error
40
                if ($response->getStatusCode() >= 500) {
41
                    return true;
42
                }
43
            }
44
45
            return false;
46
        };
47
    }
48
49
    /**
50
     * delay.
51
     *
52
     * @return callable
53
     */
54
    public static function delay()
55
    {
56
        return function ($numberOfRetries) {
57
            return 1000 * $numberOfRetries;
58
        };
59
    }
60
}
61