HeartbeatStrategy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 23 6
1
<?php
2
3
namespace SwooleTW\Http\Websocket\SocketIO\Strategies;
4
5
use SwooleTW\Http\Websocket\SocketIO\Packet;
6
7
class HeartbeatStrategy
8
{
9
    /**
10
     * If return value is true will skip decoding.
11
     *
12
     * @param \Swoole\WebSocket\Server $server
13
     * @param \Swoole\WebSocket\Frame $frame
14
     *
15
     * @return boolean
16
     */
17
    public function handle($server, $frame)
18
    {
19
        $packet = $frame->data;
20
        $packetLength = strlen($packet);
21
        $payload = '';
22
23
        if (Packet::getPayload($packet)) {
24
            return false;
25
        }
26
27
        if ($isPing = Packet::isSocketType($packet, 'ping')) {
28
            $payload .= Packet::PONG;
29
        }
30
31
        if ($isPing && $packetLength > 1) {
32
            $payload .= substr($packet, 1, $packetLength - 1);
33
        }
34
35
        if ($isPing) {
36
            $server->push($frame->fd, $payload);
37
        }
38
39
        return true;
40
    }
41
}
42