Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

src/Dispatcher.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
8
class Dispatcher
9
{
10
    protected $client;
11
    protected $service;
12
13 1
    public function __construct(Client $client, Service $service)
14
    {
15 1
        $this->client = $client;
16 1
        $this->service = $service;
17 1
    }
18
19 1
    public function dispatch(string $job, array $params = [], string $service = null)
20
    {
21 1
        if ($service === null) {
22 1
            $service = explode('.', $job)[0];
23
        }
24
25 1
        $host = $this->service->getHost($service)->address;
26
27 1
        $response = $this->client->post("http://$host/api", [
28
            'multipart' => [
29
                [
30 1
                    'name' => 'rpc',
31 1
                    'contents' => json_encode([
32 1
                        'job'    => $job,
33 1
                        'params' => $params,
34
                    ])
35
                ]
36
            ]
37
        ]);
38
39 1
        $contents = $response->getBody();
40
41 1
        if (!$contents) {
42
            throw new Exception("Host $host ($service) is unreachable");
43
        }
44
45 1
        $result = json_decode($contents);
46 1
        if (!$result || !$result->success) {
47
            $exception = new Exception($result->message ?: $contents);
48
            if ($result->trace) {
49
                $exception->remoteTrace = $result->trace;
0 ignored issues
show
The property remoteTrace does not seem to exist in Exception.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
            }
51
            throw $exception;
52
        }
53
54 1
        return $result->data;
55
    }
56
}
57