Completed
Push — master ( 1758b3...dcd940 )
by Dmitry
02:49
created

Dispatcher::dispatch()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.5801

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 19
cts 26
cp 0.7308
rs 7.5571
c 0
b 0
f 0
cc 9
nc 32
nop 3
crap 10.5801
1
<?php
2
3
namespace Basis;
4
5
use Basis\Context;
6
use Exception;
7
use GuzzleHttp\Client;
8
9
class Dispatcher
10
{
11
    protected $client;
12
    protected $context;
13
    protected $service;
14
15 1
    public function __construct(Client $client, Context $context, Service $service)
16
    {
17 1
        $this->client = $client;
18 1
        $this->context = $context;
19 1
        $this->service = $service;
20 1
    }
21
22 1
    public function dispatch(string $job, array $params = [], string $service = null)
0 ignored issues
show
Coding Style introduced by
dispatch 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...
23
    {
24 1
        if ($service === null) {
25 1
            $service = explode('.', $job)[0];
26
        }
27
28 1
        $host = $this->service->getHost($service)->address;
29 1
        $url = "http://$host/api/" . str_replace('.', '/', $job);
30
31 1
        $context = get_object_vars($this->context);
32
33 1
        if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) {
34
            $context['host'] = $_SERVER['HTTP_X_REAL_IP'];
35
        }
36
37 1
        if (array_key_exists('HTTP_X_SESSION', $_SERVER)) {
38
            $context['session'] = $_SERVER['HTTP_X_SESSION'];
39
        }
40
41 1
        $response = $this->client->post($url, [
42
            'multipart' => [
43
                [
44 1
                    'name' => 'rpc',
45 1
                    'contents' => json_encode([
46 1
                        'context' => $context,
47 1
                        'job'     => $job,
48 1
                        'params'  => $params,
49
                    ])
50
                ]
51
            ]
52
        ]);
53
54 1
        $contents = $response->getBody();
55
56 1
        if (!$contents) {
57
            throw new Exception("Host $host ($service) is unreachable");
58
        }
59
60 1
        $result = json_decode($contents);
61 1
        if (!$result || !$result->success) {
62
            $exception = new Exception($result->message ?: $contents);
63
            if ($result->trace) {
64
                $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...
65
            }
66
            throw $exception;
67
        }
68
69 1
        return $result->data;
70
    }
71
}
72