Passed
Push — master ( 37d7ff...4e50f6 )
by du
01:47
created

RequestHandler::send()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 35
ccs 20
cts 20
cp 1
rs 8.6666
c 0
b 0
f 0
cc 7
nc 10
nop 2
crap 7
1
<?php
2
3
namespace DucCnzj\Ip;
4
5
use GuzzleHttp\Client;
6
use DucCnzj\Ip\Imp\IpImp;
7
use GuzzleHttp\ClientInterface;
8
use DucCnzj\Ip\Traits\CacheResponse;
9
use DucCnzj\Ip\Imp\RequestHandlerImp;
10
use DucCnzj\Ip\Exceptions\BreakLoopException;
11
use DucCnzj\Ip\Exceptions\ServerErrorException;
12
13
class RequestHandler implements RequestHandlerImp
14
{
15
    use CacheResponse;
16
17
    /**
18
     * @var ClientInterface|null
19
     */
20
    protected $client;
21
22
    /**
23
     * @var array
24
     */
25
    protected $errors = [];
26
27
    /**
28
     * @var int
29
     */
30
    protected $tryTimes = 3;
31
32
    /**
33
     * @return ClientInterface
34
     *
35
     * @author duc <[email protected]>
36
     */
37 7
    public function getClient(): ClientInterface
38
    {
39 7
        return is_null($this->client)
40 7
            ? $this->client = new Client()
41 7
            : $this->client;
42
    }
43
44
    /**
45
     * @return array
46
     */
47 2
    public function getErrors(): array
48
    {
49 2
        return $this->errors;
50
    }
51
52
    /**
53
     * @param array  $providers
54
     * @param string $ip
55
     *
56
     * @return array
57
     * @throws ServerErrorException
58
     *
59
     * @author duc <[email protected]>
60
     */
61 10
    public function send(array $providers, string $ip)
62
    {
63 10
        foreach ($providers as $name => $provider) {
64 10
            if ($info = $this->getCacheStore()->get($this->cacheKey($name, $ip))) {
65 1
                return $info;
66
            }
67
68 10
            for ($time = 1; $time <= $this->getTryTimes(); $time++) {
69
                try {
70
                    /** @var IpImp $provider */
71 10
                    $result = array_merge($provider->send($this->getClient(), $ip), [
72 6
                        'provider' => $name,
73 6
                        'success'  => 1,
74
                    ]);
75
76 6
                    $this->getCacheStore()->put($this->cacheKey($name, $ip), $result);
77
78 6
                    return $result;
79 7
                } catch (ServerErrorException $e) {
80 5
                    $this->logError($name, $e);
81
82 5
                    continue;
83 3
                } catch (BreakLoopException $e) {
84 1
                    $this->logError($name, $e);
85
86 1
                    continue 2;
87 2
                } catch (\Exception $e) {
88 2
                    $this->logError($name, $e);
89
90 2
                    break 2;
91
                }
92
            }
93
        }
94
95 6
        throw new ServerErrorException();
96
    }
97
98
    /**
99
     * @return int
100
     */
101 13
    public function getTryTimes(): int
102
    {
103 13
        return $this->tryTimes;
104
    }
105
106
    /**
107
     * @param int $tryTimes
108
     *
109
     * @return $this
110
     *
111
     * @author duc <[email protected]>
112
     */
113 6
    public function setTryTimes(int $tryTimes)
114
    {
115 6
        $this->tryTimes = $tryTimes;
116
117 6
        return $this;
118
    }
119
120
    /**
121
     * @param string     $name
122
     * @param \Exception $e
123
     *
124
     * @author duc <[email protected]>
125
     */
126 7
    public function logError(string $name, \Exception $e)
127
    {
128 7
        $this->errors[] = "provider: {$name}. " . $e->getMessage();
129 7
    }
130
}
131