Backoff   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 48
rs 10
wmc 6
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B decider() 0 28 5
A delay() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Middleware;
17
18
use GuzzleHttp\Exception\ConnectException;
19
use GuzzleHttp\Exception\RequestException;
20
use GuzzleHttp\Psr7\Request;
21
use GuzzleHttp\Psr7\Response;
22
23
/**
24
 * Class Backoff.
25
 */
26
class Backoff
27
{
28
    /**
29
     * decider.
30
     *
31
     * @return callable
32
     */
33
    public static function decider()
34
    {
35
        return function (
36
            $retries,
37
            Request $request,
38
            Response $response = null,
39
            RequestException $exception = null
40
        ) {
41
            // Limit to 5 retry max
42
            if ($retries >= 3) {
43
                return false;
44
            }
45
46
            // Retry connection exceptions
47
            if ($exception instanceof ConnectException) {
48
                return true;
49
            }
50
51
            if ($response) {
52
                // Retry if we have a serve error
53
                if ($response->getStatusCode() >= 500) {
54
                    return true;
55
                }
56
            }
57
58
            return false;
59
        };
60
    }
61
62
    /**
63
     * delay.
64
     *
65
     * @return callable
66
     */
67
    public static function delay()
68
    {
69
        return function ($numberOfRetries) {
70
            return 1000 * $numberOfRetries;
71
        };
72
    }
73
}
74