Completed
Push — master ( 772acc...7ea003 )
by Dmitry
11:15
created

Dispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use LinkORB\Component\Etcd\Client;
7
8
class Dispatcher
9
{
10
    private $etcd;
11
12
    public function __construct(Client $etcd)
13
    {
14
        $this->etcd = $etcd;
15
    }
16
17
    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...
18
    {
19
        if (!$service) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $service of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
20
            $service = explode('.', $job)[0];
21
        }
22
23
        $content = http_build_query([
24
            'rpc' => json_encode([
25
                'job'    => $job,
26
                'params' => $params,
27
            ])
28
        ]);
29
30
31
        $context = stream_context_create([
32
            'http' => [
33
                'method'  => 'POST',
34
                'content' => $content,
35
                'header'  => implode([
36
                    'content-type: application/x-www-form-urlencoded',
37
                    'x-real-ip: '.$_SERVER['HTTP_X_REAL_IP'],
38
                    'x-session: '.$_SERVER['HTTP_X_SESSION'],
39
                ], "\r\n"),
40
                'ignore_errors' => '1'
41
            ],
42
        ]);
43
44
        $contents = file_get_contents("http://$service/api", false, $context);
45
46
        if (!$contents) {
47
            throw new Exception("Host $service unreachable");
48
        }
49
50
        $result = json_decode($contents);
51
        if (!$result || !$result->success) {
52
            throw new Exception($result->message ?: $contents);
53
        }
54
55
        return $result->data;
56
    }
57
}
58