Completed
Push — master ( 6fbd2d...3c210e )
by Dmitry
02:59
created

Dispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 49
ccs 18
cts 23
cp 0.7826
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C dispatch() 0 37 7
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);
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
Bug introduced by
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