Conditions | 7 |
Paths | 8 |
Total Lines | 35 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 8.0504 |
Changes | 0 |
1 | <?php |
||
15 | 1 | public function dispatch(string $job, array $params = [], string $service = null) |
|
16 | { |
||
17 | 1 | if ($service === null) { |
|
18 | 1 | $service = explode('.', $job)[0]; |
|
19 | } |
||
20 | |||
21 | 1 | $response = $this->client->post("http://$service/api", [ |
|
22 | 'multipart' => [ |
||
23 | [ |
||
24 | 1 | 'name' => 'rpc', |
|
25 | 1 | 'contents' => json_encode([ |
|
26 | 1 | 'job' => $job, |
|
27 | 1 | 'params' => $params, |
|
28 | ]) |
||
29 | ] |
||
30 | ] |
||
31 | ]); |
||
32 | |||
33 | 1 | $contents = $response->getBody(); |
|
34 | |||
35 | 1 | if (!$contents) { |
|
36 | throw new Exception("Host $service unreachable"); |
||
37 | } |
||
38 | |||
39 | 1 | $result = json_decode($contents); |
|
40 | 1 | if (!$result || !$result->success) { |
|
41 | $exception = new Exception($result->message ?: $contents); |
||
42 | if ($result->trace) { |
||
43 | $exception->remoteTrace = $result->trace; |
||
44 | } |
||
45 | throw $exception; |
||
46 | } |
||
47 | |||
48 | 1 | return $result->data; |
|
49 | } |
||
50 | } |
||
51 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: