Completed
Push — master ( 25988b...fb8af5 )
by huang
37:27 queued 28:37
created

WebSocketServer::doMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 7
Ratio 38.89 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 7
loc 18
ccs 0
cts 14
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Servitization\Server;
11
12
use FastD\Http\ServerRequest;
13
use FastD\Packet\Json;
14
use FastD\Servitization\OnWorkerStart;
15
use FastD\Swoole\Server\WebSocket;
16
use swoole_server;
17
use swoole_websocket_frame;
18
19
/**
20
 * Class WebSocketServer.
21
 */
22
class WebSocketServer extends WebSocket
23
{
24
    use OnWorkerStart;
25
26
    /**
27
     * @param swoole_server          $server
28
     * @param swoole_websocket_frame $frame
29
     *
30
     * @return int
31
     */
32
    public function doMessage(swoole_server $server, swoole_websocket_frame $frame)
33
    {
34
        $data = $frame->data;
35
        $data = Json::decode($data);
36
        $request = new ServerRequest($data['method'], $data['path']);
37 View Code Duplication
        if (isset($data['args'])) {
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...
38
            if ('GET' === $request->getMethod()) {
39
                $request->withQueryParams($data['args']);
40
            } else {
41
                $request->withParsedBody($data['args']);
42
            }
43
        }
44
        $response = app()->handleRequest($request);
45
        $server->push($frame->fd, (string) $response->getBody());
46
        app()->shutdown($request, $response);
47
48
        return 0;
49
    }
50
}
51