Conditions | 6 |
Paths | 6 |
Total Lines | 31 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 42 |
Changes | 0 |
1 | <?php |
||
21 | public function dispatch(string $job, array $params = [], string $service = null) |
||
22 | { |
||
23 | if ($service === null) { |
||
24 | $service = explode('.', $job)[0]; |
||
25 | } |
||
26 | |||
27 | $response = $this->client->post("http://$service/api", [ |
||
28 | 'multipart' => [ |
||
29 | [ |
||
30 | 'name' => 'rpc', |
||
31 | 'contents' => json_encode([ |
||
32 | 'job' => $job, |
||
33 | 'params' => $params, |
||
34 | ]) |
||
35 | ] |
||
36 | ] |
||
37 | ]); |
||
38 | |||
39 | $contents = $response->getBody(); |
||
40 | |||
41 | if (!$contents) { |
||
42 | throw new Exception("Host $service unreachable"); |
||
43 | } |
||
44 | |||
45 | $result = json_decode($contents); |
||
46 | if (!$result || !$result->success) { |
||
47 | throw new Exception($result->message ?: $contents); |
||
48 | } |
||
49 | |||
50 | return $result->data; |
||
51 | } |
||
52 | } |
||
53 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: