Dispatcher::dispatchAsync()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 54

Duplication

Lines 3
Ratio 5.56 %

Code Coverage

Tests 23
CRAP Score 11.2698

Importance

Changes 0
Metric Value
dl 3
loc 54
ccs 23
cts 30
cp 0.7667
rs 7.1369
c 0
b 0
f 0
cc 10
nc 8
nop 4
crap 11.2698

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Basis;
4
5
use Basis\Context;
6
use Exception;
7
use GuzzleHttp\Client;
8
use OpenTelemetry\Tracing\Tracer;
9
10
class Dispatcher
11
{
12
    use Toolkit;
13
14 1
    public function dispatch(string $job, array $params = [], string $service = null, $context = null)
15
    {
16 1
        return $this->dispatchAsync($job, $params, $service, $context)->wait();
17
    }
18
19 1
    public function dispatchAsync(string $job, array $params = [], string $service = null, $context = null)
20
    {
21 1
        if ($service === null) {
22 1
            $service = $this->getServiceName($job);
23
        }
24
25 1
        $url = $this->getUrl($service, $job);
26 1 View Code Duplication
        if (is_array($params) && array_key_exists('eventId', $params)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $url .= '/'.$params['eventId'];
28
        }
29
30 1
        if (!$context) {
31 1
            $context = $this->getContext();
32
        }
33
34 1
        $span = $this->get(Tracer::class)
35 1
            ->getActiveSpan()
36 1
            ->getSpanContext();
37
38 1
        $response = $this->get(Client::class)->postAsync($url, [
39
            'multipart' => [
40
                [
41 1
                    'name' => 'rpc',
42
                    'contents' => json_encode([
43 1
                        'job'     => $job,
44 1
                        'params'  => $params,
45 1
                        'context' => $context,
46
                        'span' => [
47 1
                            'traceId' => $span->getTraceId(),
48 1
                            'spanId' => $span->getSpanId(),
49
                        ],
50
                    ])
51
                ]
52
            ]
53
        ]);
54
55
        return $response->then(function ($response) {
56 1
            $contents = $response->getBody();
57 1
            if (!$contents) {
58
                throw new Exception("Host $host ($service) is unreachable");
59
            }
60
    
61 1
            $result = json_decode($contents);
62 1
            if (!$result || !$result->success) {
63
                $exception = new Exception($result->message ?: $contents);
64
                if ($result->trace) {
65
                    $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...
66
                }
67
                throw $exception;
68
            }
69
    
70 1
            return $result->data;
71 1
        });
72
    }
73
74 1
    protected function getContext() : array
75
    {
76 1
        $context = get_object_vars($this->get(Context::class));
77
    
78 1
        if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) {
79
            $context['host'] = $_SERVER['HTTP_X_REAL_IP'];
80
        }
81
82 1
        if (array_key_exists('HTTP_X_SESSION', $_SERVER)) {
83
            $context['session'] = $_SERVER['HTTP_X_SESSION'];
84
        }
85
86 1
        return $context;
87
    }
88
89 1
    protected function getServiceName(string $job) : string
90
    {
91 1
        return explode('.', $job)[0];
92
    }
93
94 1
    protected function getUrl(string $service, string $job) : string
95
    {
96 1
        $host = $this->get(Service::class)->getHost($service)->address;
97 1
        return "http://$host/api/" . str_replace('.', '/', $job);
98
    }
99
100
    public function send(string $job, array $params = [], string $service = null, $context = null)
101
    {
102
        if ($service === null) {
103
            $service = $this->getServiceName($job);
104
        }
105
106
        $url = $this->getUrl($service, $job);
107 View Code Duplication
        if (is_array($params) && array_key_exists('eventId', $params)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            $url .= '/'.$params['eventId'];
109
        }
110
111
        if (!$context) {
112
            $context = $this->getContext();
113
        }
114
115
        $rpc = [
116
            'context' => $context,
117
            'job'     => $job,
118
            'params'  => $params,
119
        ];
120
121
        $content = 'rpc='.urlencode(json_encode($rpc));
122
123
        $parts = parse_url($url);
124
        $port = isset($parts['port']) ? $parts['port'] : 80;
125
        $fp = fsockopen($parts['host'], $port, $errno, $errstr, 30);
126
127
        if (!$fp) {
128
            return false;
129
        }
130
131
        $parts = [
132
            "POST " . $parts['path'] . " HTTP/1.1",
133
            "Host: " . $parts['host'],
134
        ];
135
136
        if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) {
137
            $parts[] = "X-Real-Ip: ".$_SERVER['HTTP_X_REAL_IP'];
138
        }
139
        if (array_key_exists('HTTP_X_SESSION', $_SERVER)) {
140
            $parts[] = "X-Session: ".$_SERVER['HTTP_X_SESSION'];
141
        }
142
143
        $parts = array_merge($parts, [
144
            "Content-Type: application/x-www-form-urlencoded",
145
            "Content-Length: " . strlen($content),
146
            "Connection: Close",
147
            "",
148
            isset($content) ? $content : '',
149
        ]);
150
151
        fwrite($fp, implode("\r\n", $parts));
152
        fclose($fp);
153
154
        return true;
155
    }
156
}
157