Parser   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 19 3
1
<?php
2
3
namespace SwooleTW\Http\Websocket;
4
5
use Illuminate\Support\Facades\App;
6
7
abstract class Parser
8
{
9
    /**
10
     * Strategy classes need to implement handle method.
11
     */
12
    protected $strategies = [];
13
14
    /**
15
     * Execute strategies before decoding payload.
16
     * If return value is true will skip decoding.
17
     *
18
     * @param \Swoole\WebSocket\Server $server
19
     * @param \Swoole\WebSocket\Frame $frame
20
     *
21
     * @return boolean
22
     */
23
    public function execute($server, $frame)
24
    {
25
        $skip = false;
26
27
        foreach ($this->strategies as $strategy) {
28
            $result = App::call(
29
                $strategy . '@handle',
30
                [
31
                    'server' => $server,
32
                    'frame' => $frame,
33
                ]
34
            );
35
            if ($result === true) {
36
                $skip = true;
37
                break;
38
            }
39
        }
40
41
        return $skip;
42
    }
43
44
    /**
45
     * Encode output payload for websocket push.
46
     *
47
     * @param string $event
48
     * @param mixed $data
49
     *
50
     * @return mixed
51
     */
52
    abstract public function encode(string $event, $data);
53
54
    /**
55
     * Input message on websocket connected.
56
     * Define and return event name and payload data here.
57
     *
58
     * @param \Swoole\Websocket\Frame $frame
59
     *
60
     * @return array
61
     */
62
    abstract public function decode($frame);
63
}
64