Completed
Push — master ( a5051f...ed61fc )
by Dmitry
02:44
created

Dispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 43
ccs 16
cts 21
cp 0.7619
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C dispatch() 0 35 7
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
8
class Dispatcher
9
{
10 1
    public function __construct(Client $client)
11
    {
12 1
        $this->client = $client;
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13 1
    }
14
15 1
    public function dispatch(string $job, array $params = [], string $service = null)
16
    {
17 1
        if ($service === null) {
18 1
            $service = explode('.', $job)[0];
19
        }
20
21 1
        $response = $this->client->post("http://$service/api", [
22
            'multipart' => [
23
                [
24 1
                    'name' => 'rpc',
25 1
                    'contents' => json_encode([
26 1
                        'job'    => $job,
27 1
                        'params' => $params,
28
                    ])
29
                ]
30
            ]
31
        ]);
32
33 1
        $contents = $response->getBody();
34
35 1
        if (!$contents) {
36
            throw new Exception("Host $service unreachable");
37
        }
38
39 1
        $result = json_decode($contents);
40 1
        if (!$result || !$result->success) {
41
            $exception = new Exception($result->message ?: $contents);
42
            if ($result->trace) {
43
                $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...
44
            }
45
            throw $exception;
46
        }
47
48 1
        return $result->data;
49
    }
50
}
51