Completed
Push — master ( 1159c9...571cff )
by Dmitry
03:45
created

Dispatcher::dispatch()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 26
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 6
nop 3
crap 42
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
8
class Dispatcher
9
{
10
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
11
    {
12
        $this->client = new 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
            'headers' => [
14
                'content-type: multipart/form-data',
15
                'x-real-ip: '.$_SERVER['HTTP_X_REAL_IP'],
16
                'x-session: '.$_SERVER['HTTP_X_SESSION'],
17
            ]
18
        ]);
19
    }
20
21
    public function dispatch(string $job, array $params = [], string $service = null)
22
    {
23
        if ($service === null) {
24
            $service = explode('.', $job)[0];
25
        }
26
27
        $response = $this->client->post("http://$service/api", [
28
            'multipart' => [
29
                [
30
                    'name' => 'rpc',
31
                    'contents' => json_encode([
32
                        'job'    => $job,
33
                        'params' => $params,
34
                    ])
35
                ]
36
            ]
37
        ]);
38
39
        $contents = $response->getBody();
40
41
        if (!$contents) {
42
            throw new Exception("Host $service unreachable");
43
        }
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