SimpleParser::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace SwooleTW\Http\Websocket;
4
5
class SimpleParser extends Parser
6
{
7
    /**
8
     * Strategy classes need to implement handle method.
9
     */
10
    protected $strategies = [];
11
12
    /**
13
     * Encode output payload for websocket push.
14
     *
15
     * @param string $event
16
     * @param mixed $data
17
     *
18
     * @return mixed
19
     */
20
    public function encode(string $event, $data)
21
    {
22
        return json_encode(
23
            [
24
                'event' => $event,
25
                'data' => $data,
26
            ]
27
        );
28
    }
29
30
    /**
31
     * Input message on websocket connected.
32
     * Define and return event name and payload data here.
33
     *
34
     * @param \Swoole\Websocket\Frame $frame
35
     *
36
     * @return array
37
     */
38
    public function decode($frame)
39
    {
40
        $data = json_decode($frame->data, true);
41
42
        return [
43
            'event' => $data['event'] ?? null,
44
            'data' => $data['data'] ?? null,
45
        ];
46
    }
47
}
48