1 | <?php |
||||
2 | /** |
||||
3 | * Copyright (c) 2019 - present |
||||
4 | * updown - UpDownClient.php |
||||
5 | * author: Roberto Belotti - [email protected] |
||||
6 | * web : robertobelotti.com, github.com/biscolab |
||||
7 | * Initial version created on: 15/2/2019 |
||||
8 | * MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE |
||||
9 | */ |
||||
10 | |||||
11 | namespace Biscolab\UpDown\Http; |
||||
12 | |||||
13 | use Biscolab\UpDown\Enum\UpDownRequestMethod; |
||||
14 | use Biscolab\UpDown\Fields\UpDownRequestFields; |
||||
15 | use Biscolab\UpDown\UpDown; |
||||
16 | use GuzzleHttp\Client; |
||||
17 | use GuzzleHttp\Exception\RequestException; |
||||
18 | use GuzzleHttp\Psr7\Response; |
||||
19 | |||||
20 | class UpDownClient |
||||
21 | { |
||||
22 | |||||
23 | /** |
||||
24 | * @var Client |
||||
25 | */ |
||||
26 | protected $client = null; |
||||
27 | |||||
28 | /** |
||||
29 | * GeocoderClient constructor. |
||||
30 | */ |
||||
31 | public function __construct() |
||||
32 | { |
||||
33 | |||||
34 | $this->setClient(new Client()); |
||||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * @param null $client |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
39 | * |
||||
40 | * @return UpDownClient |
||||
41 | */ |
||||
42 | public function setClient($client) |
||||
43 | { |
||||
44 | |||||
45 | $this->client = $client; |
||||
46 | |||||
47 | return $this; |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * @param string $url |
||||
52 | * @param array|null $params |
||||
53 | * |
||||
54 | * @return UpDownResponse |
||||
55 | */ |
||||
56 | public function get(string $url, ?array $params = null): UpDownResponse |
||||
57 | { |
||||
58 | |||||
59 | return $this->makeCall(UpDownRequestMethod::GET, $url, $params); |
||||
0 ignored issues
–
show
It seems like
$params can also be of type null ; however, parameter $params of Biscolab\UpDown\Http\UpDownClient::makeCall() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
60 | |||||
61 | } |
||||
62 | |||||
63 | public function makeCall(string $method, string $url, array $params = []) |
||||
64 | { |
||||
65 | |||||
66 | $client_params = ['http_errors' => false]; |
||||
67 | $query_params = []; |
||||
68 | |||||
69 | switch ($method) { |
||||
70 | case UpDownRequestMethod::POST: |
||||
71 | case UpDownRequestMethod::PUT: |
||||
72 | |||||
73 | if (!empty($params[UpDownRequestFields::API_KEY])) { |
||||
74 | $client_params['headers'] = [ |
||||
75 | 'X-Api-Key' => $params[UpDownRequestFields::API_KEY], |
||||
76 | ]; |
||||
77 | } |
||||
78 | $client_params['form_params'] = $params; |
||||
79 | break; |
||||
80 | default: |
||||
81 | $query_params = $params; |
||||
82 | |||||
83 | } |
||||
84 | $url .= '?' . http_build_query($this->addApiKeyToParams($query_params)); |
||||
85 | |||||
86 | // DO NOT remove event though "http_errors" parameter is set to "false" |
||||
87 | try { |
||||
88 | /** @var Response $res */ |
||||
89 | $res = $this->client->request(strtoupper($method), $url, $client_params); |
||||
90 | } catch (RequestException $e) { |
||||
91 | $res = $e->getResponse(); |
||||
92 | } |
||||
93 | |||||
94 | return new UpDownResponse($res); |
||||
95 | } |
||||
96 | |||||
97 | /** |
||||
98 | * @param array $params |
||||
99 | * |
||||
100 | * @return array |
||||
101 | */ |
||||
102 | protected function addApiKeyToParams(array $params = []): array |
||||
103 | { |
||||
104 | |||||
105 | $params[UpDownRequestFields::API_KEY] = UpDown::instance()->getKey(); |
||||
106 | |||||
107 | return $params; |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * @param string $url |
||||
112 | * @param array|null $params |
||||
113 | * |
||||
114 | * @return UpDownResponse |
||||
115 | */ |
||||
116 | public function post(string $url, ?array $params = null): UpDownResponse |
||||
117 | { |
||||
118 | |||||
119 | return $this->makeCall(UpDownRequestMethod::POST, $url, $params); |
||||
0 ignored issues
–
show
It seems like
$params can also be of type null ; however, parameter $params of Biscolab\UpDown\Http\UpDownClient::makeCall() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
120 | |||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * @param string $url |
||||
125 | * @param array|null $params |
||||
126 | * |
||||
127 | * @return UpDownResponse |
||||
128 | */ |
||||
129 | public function put(string $url, ?array $params = null): UpDownResponse |
||||
130 | { |
||||
131 | |||||
132 | return $this->makeCall(UpDownRequestMethod::PUT, $url, $params); |
||||
0 ignored issues
–
show
It seems like
$params can also be of type null ; however, parameter $params of Biscolab\UpDown\Http\UpDownClient::makeCall() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
133 | |||||
134 | } |
||||
135 | |||||
136 | /** |
||||
137 | * @param string $url |
||||
138 | * @param array|null $params |
||||
139 | * |
||||
140 | * @return UpDownResponse |
||||
141 | */ |
||||
142 | public function delete(string $url, ?array $params = null): UpDownResponse |
||||
143 | { |
||||
144 | |||||
145 | return $this->makeCall(UpDownRequestMethod::DELETE, $url, $params); |
||||
0 ignored issues
–
show
It seems like
$params can also be of type null ; however, parameter $params of Biscolab\UpDown\Http\UpDownClient::makeCall() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
146 | } |
||||
147 | } |