| Conditions | 9 |
| Paths | 32 |
| Total Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 19 |
| CRAP Score | 10.5801 |
| Changes | 0 | ||
| 1 | <?php |
||
| 22 | 1 | public function dispatch(string $job, array $params = [], string $service = null) |
|
|
|
|||
| 23 | { |
||
| 24 | 1 | if ($service === null) { |
|
| 25 | 1 | $service = explode('.', $job)[0]; |
|
| 26 | } |
||
| 27 | |||
| 28 | 1 | $host = $this->service->getHost($service)->address; |
|
| 29 | 1 | $url = "http://$host/api/" . str_replace('.', '/', $job); |
|
| 30 | |||
| 31 | 1 | $context = get_object_vars($this->context); |
|
| 32 | |||
| 33 | 1 | if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) { |
|
| 34 | $context['host'] = $_SERVER['HTTP_X_REAL_IP']; |
||
| 35 | } |
||
| 36 | |||
| 37 | 1 | if (array_key_exists('HTTP_X_SESSION', $_SERVER)) { |
|
| 38 | $context['session'] = $_SERVER['HTTP_X_SESSION']; |
||
| 39 | } |
||
| 40 | |||
| 41 | 1 | $response = $this->client->post($url, [ |
|
| 42 | 'multipart' => [ |
||
| 43 | [ |
||
| 44 | 1 | 'name' => 'rpc', |
|
| 45 | 1 | 'contents' => json_encode([ |
|
| 46 | 1 | 'context' => $context, |
|
| 47 | 1 | 'job' => $job, |
|
| 48 | 1 | 'params' => $params, |
|
| 49 | ]) |
||
| 50 | ] |
||
| 51 | ] |
||
| 52 | ]); |
||
| 53 | |||
| 54 | 1 | $contents = $response->getBody(); |
|
| 55 | |||
| 56 | 1 | if (!$contents) { |
|
| 57 | throw new Exception("Host $host ($service) is unreachable"); |
||
| 58 | } |
||
| 59 | |||
| 60 | 1 | $result = json_decode($contents); |
|
| 61 | 1 | if (!$result || !$result->success) { |
|
| 62 | $exception = new Exception($result->message ?: $contents); |
||
| 63 | if ($result->trace) { |
||
| 64 | $exception->remoteTrace = $result->trace; |
||
| 65 | } |
||
| 66 | throw $exception; |
||
| 67 | } |
||
| 68 | |||
| 69 | 1 | return $result->data; |
|
| 70 | } |
||
| 71 | } |
||
| 72 |
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: