Webhookd   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 52
c 1
b 0
f 0
dl 0
loc 224
ccs 64
cts 64
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A onStart() 0 8 1
A onWorkerStart() 0 9 3
A onRequest() 0 9 1
A onTask() 0 24 2
A onShutdown() 0 3 1
A onFinish() 0 3 1
A onWorkerStop() 0 3 1
A __construct() 0 4 1
A setServer() 0 20 4
A start() 0 3 1
1
<?php
2
3
namespace App\Service;
4
5
use Swoole\Server;
0 ignored issues
show
Bug introduced by
The type Swoole\Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use App\EventSubject;
7
use Swoole\Http\Request;
0 ignored issues
show
Bug introduced by
The type Swoole\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Swoole\Http\Response;
0 ignored issues
show
Bug introduced by
The type Swoole\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * Webhookd class
13
 *
14
 * @see https://wiki.swoole.com/wiki/page/326.html
15
 */
16
class Webhookd
17
{
18
    /**
19
     * Server instance
20
     *
21
     * @var \Swoole\Http\Server
0 ignored issues
show
Bug introduced by
The type Swoole\Http\Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
     */
23
    private $_server;
24
25
    /**
26
     * Event subject instance
27
     *
28
     * @var \App\EventSubject
29
     */
30
    private $_subject;
31
32
    /**
33
     * Logger instance
34
     *
35
     * @var \Psr\Log\LoggerInterface
36
     */
37
    private $_logger;
38
39
    /**
40
     * Construct a new webhookd service.
41
     *
42
     * @param \App\EventSubject        $subject Event subject instance
43
     * @param \Psr\Log\LoggerInterface $logger  Logger instance
44
     */
45 9
    public function __construct(EventSubject $subject, LoggerInterface $logger)
46
    {
47 9
        $this->_subject = $subject;
48 9
        $this->_logger = $logger;
49 9
    }
50
51
    /**
52
     * Sets a http server.
53
     *
54
     * @param \Swoole\Http\Server $server Server instance
55
     *
56
     * @return void
57
     */
58 2
    public function setServer(Server $server): void
59
    {
60 2
        $this->_server = $server;
61 2
        $this->_server->set(
62
            [
63 2
            'worker_num' => 1,
64
            'task_worker_num' => 1,
65
            // 'task_ipc_mode' => 3,
66
            // 'message_queue_key' => 0x70001001,
67
            // 'task_tmpdir' => '/data/task/',
68
            ]
69
        );
70 2
        $methods = get_class_methods($this);
71 2
        foreach ($methods as $method) {
72 2
            if ($method != 'on' && substr($method, 0, 2) == 'on') {
73 2
                $this->_server->on(substr($method, 2), [$this, $method]);
74 2
                $this->_logger->debug(
75 2
                    '{event} event attached.',
76
                    [
77 2
                    'event' => $method
78
                    ]
79
                );
80
            }
81
        }
82 2
    }
83
84
    /**
85
     * Start the http server.
86
     *
87
     * @return void
88
     */
89 1
    public function start(): void
90
    {
91 1
        $this->_server->start();
92 1
    }
93
94
    /**
95
     * On start event.
96
     *
97
     * @param \Swoole\Server $server Server instance
98
     *
99
     * @return void
100
     *
101
     * @see https://wiki.swoole.com/wiki/page/p-event/onStart.html
102
     */
103 1
    public function onStart(Server $server): void
104
    {
105 1
        $port = $server->ports[0];
106 1
        $this->_logger->warning(
107 1
            'Http server started, listening on http://{host}:{port}',
108
            [
109 1
            'host' => $port->host,
110 1
            'port' => $port->port
111
            ]
112
        );
113 1
    }
114
115
    /**
116
     * On shutdown event.
117
     *
118
     * @param \Swoole\Server $server Server instance
119
     *
120
     * @return void
121
     *
122
     * @see https://wiki.swoole.com/wiki/page/p-event/onShutdown.html
123
     */
124 1
    public function onShutdown(Server $server): void
0 ignored issues
show
Unused Code introduced by
The parameter $server is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
    public function onShutdown(/** @scrutinizer ignore-unused */ Server $server): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126 1
        $this->_logger->warning('Http server stopped.');
127 1
    }
128
129
    /**
130
     * On worker start event.
131
     *
132
     * @param \Swoole\Server $server   Server instance
133
     * @param int            $workerId Worker id
134
     *
135
     * @return void
136
     *
137
     * @see https://wiki.swoole.com/wiki/page/p-event/onWorkerStart.html
138
     */
139 1
    public function onWorkerStart(Server $server, int $workerId): void
140
    {
141 1
        global $argv;
142 1
        swoole_set_process_name('php ' . $argv[0] . ': ' . ($server->taskworker ? 'task_worker' : 'worker'));
0 ignored issues
show
Bug introduced by
The function swoole_set_process_name was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        /** @scrutinizer ignore-call */ 
143
        swoole_set_process_name('php ' . $argv[0] . ': ' . ($server->taskworker ? 'task_worker' : 'worker'));
Loading history...
143 1
        $this->_logger->info(
144 1
            '{worker}(id:{id}) started.',
145
            [
146 1
            'worker' => ($server->taskworker ? 'Task worker' : 'Worker'),
147 1
            'id' => $workerId
148
            ]
149
        );
150 1
    }
151
152
    /**
153
     * On worker stop event.
154
     *
155
     * @param \Swoole\Server $server   Server instance
156
     * @param int            $workerId Worker id
157
     *
158
     * @return void
159
     *
160
     * @see https://wiki.swoole.com/wiki/page/p-event/onWorkerStop.html
161
     */
162 1
    public function onWorkerStop(Server $server, int $workerId): void
0 ignored issues
show
Unused Code introduced by
The parameter $server is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

162
    public function onWorkerStop(/** @scrutinizer ignore-unused */ Server $server, int $workerId): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
    {
164 1
        $this->_logger->info('Worker(id:{id}) stopped.', ['id' => $workerId]);
165 1
    }
166
167
    /**
168
     * On request event.
169
     *
170
     * @param \Swoole\Http\Request  $request  Request instance
171
     * @param \Swoole\Http\Response $response Response instance
172
     *
173
     * @return void
174
     *
175
     * @see https://wiki.swoole.com/wiki/page/330.html
176
     */
177 1
    public function onRequest(Request $request, Response $response): void
178
    {
179
        // $request->header['x-gitlab-event']
180
        // $request->header['x-gitlab-token']
181 1
        $data = $request->rawContent();
182 1
        $this->_logger->debug('New request received: {data}', ['data' => $request->getData()]);
183 1
        $this->_server->task($data);
184 1
        $response->header('Content-Type', 'text/plain');
185 1
        $response->end('success');
186 1
    }
187
188
    /**
189
     * On task event
190
     *
191
     * @param \Swoole\Server $server      Server instance
192
     * @param int            $taskId      Task id
193
     * @param int            $srcWorkerId Worker id
194
     * @param string         $data        Task input value
195
     *
196
     * @return int
197
     *
198
     * @see https://wiki.swoole.com/wiki/page/54.html
199
     */
200 2
    public function onTask(Server $server, int $taskId, int $srcWorkerId, string $data): int
0 ignored issues
show
Unused Code introduced by
The parameter $server is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

200
    public function onTask(/** @scrutinizer ignore-unused */ Server $server, int $taskId, int $srcWorkerId, string $data): int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
    {
202 2
        $this->_logger->info(
203 2
            'Task(id:{id},worker_id:{worker_id}) started.',
204
            [
205 2
                'id' => $taskId,
206 2
                'worker_id' => $srcWorkerId
207
            ]
208
        );
209 2
        $input = json_decode($data, true);
210 2
        $return = json_last_error();
211 2
        if ($return == JSON_ERROR_NONE) {
212 1
            $this->_subject->exchangeArray($input);
213 1
            $this->_subject->notify();
214
        } else {
215 1
            $this->_logger->error(
216 1
                'Decoding: {data} - {message}',
217
                [
218 1
                'data' => var_export($data, true),
219 1
                'message' => json_last_error_msg()
220
                ]
221
            );
222
        }
223 2
        return $return;
224
    }
225
226
    /**
227
     * On task finish event
228
     *
229
     * @param \Swoole\Server $server Server instance
230
     * @param int            $taskId Task id
231
     * @param mixed          $data   Task return value
232
     *
233
     * @return void
234
     *
235
     * @see https://wiki.swoole.com/wiki/page/136.html
236
     */
237 1
    public function onFinish(Server $server, int $taskId, $data): void
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

237
    public function onFinish(Server $server, int $taskId, /** @scrutinizer ignore-unused */ $data): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $server is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

237
    public function onFinish(/** @scrutinizer ignore-unused */ Server $server, int $taskId, $data): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
238
    {
239 1
        $this->_logger->info('Task(id:{id}) finished.', ['id' => $taskId]);
240 1
    }
241
}
242