Completed
Push — master ( a678c2...6277d2 )
by
unknown
21s queued 14s
created

RetryDomainsMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldRetry() 0 7 3
A __construct() 0 5 1
A send() 0 26 5
1
<?php
2
namespace Qiniu\Http\Middleware;
3
4
use Qiniu\Http\Request;
5
use Qiniu\Http\Response;
6
7
class RetryDomainsMiddleware implements Middleware
8
{
9
    /**
10
     * @var array<string> backup domains.
11
     */
12
    private $backupDomains;
13
14
    /**
15
     * @var numeric max retry times for each backup domains.
0 ignored issues
show
Bug introduced by
The type Qiniu\Http\Middleware\numeric was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
     */
17
    private $maxRetryTimes;
18
19
    /**
20
     * @var callable args response and request; returns bool; If true will retry with backup domains.
21
     */
22
    private $retryCondition;
23
24
    /**
25
     * @param array<string> $backupDomains
26
     * @param numeric $maxRetryTimes
27
     */
28
    public function __construct($backupDomains, $maxRetryTimes = 2, $retryCondition = null)
29
    {
30
        $this->backupDomains = $backupDomains;
31
        $this->maxRetryTimes = $maxRetryTimes;
0 ignored issues
show
Documentation Bug introduced by
It seems like $maxRetryTimes can also be of type integer. However, the property $maxRetryTimes is declared as type Qiniu\Http\Middleware\numeric. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
32
        $this->retryCondition = $retryCondition;
33
    }
34
35
    private function shouldRetry($resp, $req)
36
    {
37
        if (is_callable($this->retryCondition)) {
38
            return call_user_func($this->retryCondition, $resp, $req);
39
        }
40
41
        return !$resp || $resp->needRetry();
42
    }
43
44
    /**
45
     * @param Request $request
46
     * @param callable(Request): Response $next
47
     * @return Response
48
     */
49
    public function send($request, $next)
50
    {
51
        $response = null;
52
        $urlComponents = parse_url($request->url);
53
54
        foreach (array_merge(array($urlComponents["host"]), $this->backupDomains) as $backupDomain) {
55
            $urlComponents["host"] = $backupDomain;
56
            $request->url = \Qiniu\unparse_url($urlComponents);
57
            $retriedTimes = 0;
58
59
            while ($retriedTimes < $this->maxRetryTimes) {
60
                $response = $next($request);
61
62
                $retriedTimes += 1;
63
64
                if (!$this->shouldRetry($response, $request)) {
65
                    return $response;
66
                }
67
            }
68
        }
69
70
        if (!$response) {
71
            $response = $next($request);
72
        }
73
74
        return $response;
75
    }
76
}
77