1 | <?php |
||||
2 | |||||
3 | namespace Multicoin\Api\Service; |
||||
4 | |||||
5 | use Exception; |
||||
6 | use Http\Client\Common\Exception\ClientErrorException; |
||||
7 | use Http\Client\Common\HttpMethodsClient; |
||||
8 | use Http\Client\Common\Plugin\BaseUriPlugin; |
||||
9 | use Http\Client\Common\PluginClient; |
||||
10 | use Http\Client\HttpClient; |
||||
11 | use Http\Discovery\HttpClientDiscovery; |
||||
12 | use Http\Discovery\MessageFactoryDiscovery; |
||||
13 | use Http\Discovery\UriFactoryDiscovery; |
||||
14 | use Http\Message\RequestFactory; |
||||
15 | use Http\Message\UriFactory; |
||||
16 | use Illuminate\Support\Collection; |
||||
17 | |||||
18 | class ApiClient |
||||
19 | { |
||||
20 | protected $client; |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
21 | /** |
||||
22 | * @var HttpClient |
||||
23 | */ |
||||
24 | protected $httpClient; |
||||
0 ignored issues
–
show
|
|||||
25 | |||||
26 | protected $plugins = []; |
||||
0 ignored issues
–
show
|
|||||
27 | /** |
||||
28 | * @var RequestFactory |
||||
29 | */ |
||||
30 | private $requestFactory; |
||||
0 ignored issues
–
show
|
|||||
31 | |||||
32 | /** |
||||
33 | * @var UriFactory |
||||
34 | */ |
||||
35 | private $uriFactory; |
||||
0 ignored issues
–
show
|
|||||
36 | |||||
37 | public function __construct( |
||||
38 | string $baseUrl, |
||||
39 | array $plugins = [], |
||||
40 | bool $replace = true, |
||||
41 | HttpClient $httpClient = null, |
||||
42 | RequestFactory $requestFactory = null |
||||
43 | ) { |
||||
44 | $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
||||
45 | $this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
||||
46 | $this->uriFactory = new BaseUriPlugin( |
||||
0 ignored issues
–
show
It seems like
new Http\Client\Common\P...'replace' => $replace)) of type Http\Client\Common\Plugin\BaseUriPlugin is incompatible with the declared type Http\Message\UriFactory of property $uriFactory .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||||
47 | UriFactoryDiscovery::find()->createUri($baseUrl), |
||||
48 | ['replace' => $replace] |
||||
49 | ); |
||||
50 | $this->addPlugins(array_merge($plugins, [$this->uriFactory])); |
||||
51 | $this->client = $this->getHttpClient(); |
||||
52 | } |
||||
53 | |||||
54 | public function addPlugins($plugin) |
||||
55 | { |
||||
56 | $this->plugins = array_merge($this->plugins, $plugin); |
||||
57 | $this->client = $this->getHttpClient(); |
||||
58 | |||||
59 | return $this; |
||||
60 | } |
||||
61 | |||||
62 | public function getHttpClient() |
||||
63 | { |
||||
64 | return new HttpMethodsClient($this->getPluginClient(), $this->requestFactory); |
||||
65 | } |
||||
66 | |||||
67 | public function getPluginClient() |
||||
68 | { |
||||
69 | return new PluginClient($this->httpClient, $this->plugins); |
||||
70 | } |
||||
71 | |||||
72 | public function doGet(string $url): ?Collection |
||||
73 | { |
||||
74 | try { |
||||
75 | $request = $this->client->get($url); |
||||
76 | $response = $request->getBody()->getContents(); |
||||
77 | } catch (ClientErrorException $exception) { |
||||
78 | throw new \Exception($exception); |
||||
79 | throw new Exception($exception->getMessage()." Error Processing Request for [$url]", 1); |
||||
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $url instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
![]() ThrowNode is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
80 | |||||
81 | return collect([ |
||||
82 | 'error' => [ |
||||
83 | 'code' => $exception->getCode(), |
||||
84 | 'reason' => $exception->getMessage(), |
||||
85 | ], |
||||
86 | ]); |
||||
87 | } |
||||
88 | |||||
89 | return $this->responseResult($response); |
||||
90 | } |
||||
91 | |||||
92 | public function doPost(string $url, array $data = []): ?Collection |
||||
93 | { |
||||
94 | try { |
||||
95 | $request = $this->client->post($url, $data); |
||||
96 | $response = $request->getBody()->getContents(); |
||||
97 | } catch (ClientErrorException $exception) { |
||||
98 | throw new \Exception($exception); |
||||
99 | // throw new Exception($exception->getMessage()." Error Processing Request for [$url]", 1); |
||||
100 | |||||
101 | return collect([ |
||||
0 ignored issues
–
show
return collect(array('er...eption->getMessage()))) is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
102 | 'error' => [ |
||||
103 | 'code' => $exception->getCode(), |
||||
104 | 'reason' => $exception->getMessage(), |
||||
105 | ], |
||||
106 | ]); |
||||
107 | } |
||||
108 | |||||
109 | return $this->responseResult($response); |
||||
110 | } |
||||
111 | |||||
112 | protected function responseResult(?string $response): ?Collection |
||||
113 | { |
||||
114 | $data = json_decode($response, true); |
||||
0 ignored issues
–
show
It seems like
$response can also be of type null ; however, parameter $json of json_decode() does only seem to accept string , 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
![]() |
|||||
115 | |||||
116 | if (isset($data['error'])) { |
||||
117 | throw new Exception($data['error']['message']); |
||||
118 | } |
||||
119 | |||||
120 | return collect($data); |
||||
121 | } |
||||
122 | } |
||||
123 |