RequestHandler::logError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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