HeartbeatStrategy::handle()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 9
nop 2
dl 0
loc 23
rs 9.2222
c 0
b 0
f 0
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