Completed
Push — master ( 0a610c...eedd5f )
by Dmitry
04:37
created

Dispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 54
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C dispatch() 0 44 7
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($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
20
        if(!$host) {
21
22
            $this->etcd->setRoot('jobs');
23
24
            $service = $this->etcd->get($job);
25
            if(!$service) {
26
                throw new Exception("No service for job $job");
27
            }
28
29
            $host = getenv(strtoupper($service).'_SERVICE_HOST') ?: $service;
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