Conditions | 5 |
Paths | 4 |
Total Lines | 35 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 30 |
Changes | 0 |
1 | <?php |
||
17 | public function dispatch($job, $params = [], $host = null) |
||
|
|||
18 | { |
||
19 | if (!$host) { |
||
20 | $host = explode('.', $job)[0]; |
||
21 | } |
||
22 | |||
23 | $content = http_build_query([ |
||
24 | 'rpc' => json_encode([ |
||
25 | 'job' => $job, |
||
26 | 'params' => $params, |
||
27 | ]) |
||
28 | ]); |
||
29 | |||
30 | |||
31 | $context = stream_context_create([ |
||
32 | 'http' => [ |
||
33 | 'method' => 'POST', |
||
34 | 'header' => implode([ |
||
35 | 'content-type: application/x-www-form-urlencoded', |
||
36 | 'x-real-ip: '.$_SERVER['HTTP_X_REAL_IP'], |
||
37 | 'x-session: '.$_SERVER['HTTP_X_SESSION'], |
||
38 | ], "\r\n"), |
||
39 | 'content' => $content, |
||
40 | ], |
||
41 | ]); |
||
42 | |||
43 | $contents = file_get_contents("http://$host/api", false, $context); |
||
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: