Passed
Push — master ( 10b845...c93f04 )
by du
01:52
created

RequestHandler::send()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6
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\Imp\RequestHandlerImp;
9
use DucCnzj\Ip\Exceptions\ServerErrorException;
10
use DucCnzj\Ip\Exceptions\NetworkErrorException;
0 ignored issues
show
Bug introduced by
The type DucCnzj\Ip\Exceptions\NetworkErrorException 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...
11
use DucCnzj\Ip\Exceptions\InvalidArgumentException;
12
13
class RequestHandler implements RequestHandlerImp
14
{
15
    /**
16
     * @var ClientInterface|null
17
     */
18
    protected $client;
19
20
    /**
21
     * @var array
22
     */
23
    protected $errors = [];
24
25
    /**
26
     * @var int
27
     */
28
    protected $tryTimes = 3;
29
30
    /**
31
     * @return ClientInterface
32
     *
33
     * @author duc <[email protected]>
34
     */
35 3
    public function getClient(): ClientInterface
36
    {
37 3
        return is_null($this->client)
38 3
            ? $this->client = new Client()
39 3
            : $this->client;
40
    }
41
42
    /**
43
     * @return array
44
     */
45 1
    public function getErrors(): array
46
    {
47 1
        return $this->errors;
48
    }
49
50
    /**
51
     * @param array  $providers
52
     * @param string $ip
53
     *
54
     * @return array
55
     * @throws NetworkErrorException
56
     * @throws ServerErrorException
57
     *
58
     * @author duc <[email protected]>
59
     */
60 6
    public function send(array $providers, string $ip)
61
    {
62 6
        foreach ($providers as $name => $provider) {
63 6
            for ($time = 1; $time <= $this->getTryTimes(); $time++) {
64
                try {
65
                    /** @var IpImp $provider */
66 6
                    return array_merge($provider->send($this->getClient(), $ip), [
67 3
                        'provider' => $name,
68 3
                        'success'  => 1,
69
                    ]);
70 6
                } catch (ServerErrorException $e) {
71 4
                    $this->errors[] = $e->getMessage();
72
73 4
                    continue;
74 3
                } catch (InvalidArgumentException $exception) {
75 2
                    $this->errors[] = $exception->getMessage();
76
77 2
                    continue 2;
78 1
                } catch (\Exception $e) {
79 1
                    $this->errors[] = $e->getMessage();
80
81 1
                    break 2;
82
                }
83
            }
84
        }
85
86 4
        throw new ServerErrorException();
87
    }
88
89
    /**
90
     * @return int
91
     */
92 8
    public function getTryTimes(): int
93
    {
94 8
        return $this->tryTimes;
95
    }
96
97
    /**
98
     * @param int $tryTimes
99
     *
100
     * @return $this
101
     *
102
     * @author duc <[email protected]>
103
     */
104 4
    public function setTryTimes(int $tryTimes)
105
    {
106 4
        $this->tryTimes = $tryTimes;
107
108 4
        return $this;
109
    }
110
}
111