Completed
Push — master ( 86cfb6...ebc154 )
by Dmitry
03:36
created

Dispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 1
    public function __construct(Client $etcd)
13
    {
14 1
        $this->etcd = $etcd;
15 1
    }
16
17
    public function dispatch($job, $params = [], $host = 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 (!$host) {
20
            $this->etcd->setRoot('jobs/'.$job);
21
22
            try {
23
                $host = $this->etcd->get('service');
24
            } catch (Exception $e) {
25
                $host = null;
26
            }
27
            if (!$host) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $host of type string|null 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...
28
                throw new Exception("No service for job $job");
29
            }
30
        }
31
32
        $content = http_build_query([
33
            'rpc' => json_encode([
34
                'job' => $job,
35
                'params' => $params,
36
            ])
37
        ]);
38
39
40
        $context = stream_context_create([
41
            'http' => [
42
                'method' => 'POST',
43
                'header' => implode([
44
                    'content-type: application/x-www-form-urlencoded',
45
                    'x-real-ip: '.$_SERVER['HTTP_X_REAL_IP'],
46
                    'x-session: '.$_SERVER['HTTP_X_SESSION'],
47
                ], "\r\n"),
48
                'content' => $content,
49
            ],
50
        ]);
51
52
        $contents = file_get_contents("http://$host/api", false, $context);
53
54
        $result = json_decode($contents);
55
        if (!$result || !$result->success) {
56
            throw new Exception($result->message ?: $contents);
57
        }
58
59
        return $result->data;
60
    }
61
}
62