Passed
Push — master ( b4e552...a91d54 )
by y
03:05
created

WebSocketClient::writeText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Helix\Socket;
4
5
/**
6
 * Client data framing.
7
 *
8
 * https://tools.ietf.org/html/rfc6455
9
 */
10
class WebSocketClient extends StreamClient implements ReactiveInterface {
11
12
    /**
13
     * @var WebSocketServer
14
     */
15
    protected $server;
16
17
    /**
18
     * @param $resource
19
     * @param WebSocketServer $server
20
     */
21
    public function __construct ($resource, WebSocketServer $server) {
22
        parent::__construct($resource);
23
        $this->server = $server;
24
    }
25
26
    /**
27
     * Removes the client from the server and reactor.
28
     */
29
    protected function onClose () {
30
        $this->server->remove($this);
31
    }
32
33
    /**
34
     * @param string $payload
35
     */
36
    protected function onData (string $payload): void {
0 ignored issues
show
Unused Code introduced by
The parameter $payload is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    protected function onData (/** @scrutinizer ignore-unused */ string $payload): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    }
38
39
    /**
40
     * WebSockets do not use the out-of-band channel.
41
     */
42
    final public function onOutOfBand () {
43
    }
44
45
    /**
46
     * When a browser responds to a ping.
47
     */
48
    protected function onPong (): void {
49
        // stub
50
    }
51
52
    /**
53
     * Reads framed messages.
54
     */
55
    public function onReadable (): void {
56
        $data = $this->recv(4096);
57
        $frame = new WebSocketFrame($data);
58
        if ($frame->isText()) {
59
            $this->onText($frame->getPayload());
60
        }
61
        elseif ($frame->isBinary()) {
62
            $this->onData($frame->getPayload());
63
        }
64
        elseif ($frame->isPong()) {
65
            $this->onPong();
66
        }
67
        elseif ($frame->isClose()) {
68
            $this->close();
69
        }
70
    }
71
72
    /**
73
     * Stub.
74
     *
75
     * @param string $text
76
     */
77
    protected function onText (string $text): void {
0 ignored issues
show
Unused Code introduced by
The parameter $text is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

77
    protected function onText (/** @scrutinizer ignore-unused */ string $text): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    }
79
80
    /**
81
     * Pings the browser.
82
     */
83
    public function ping () {
84
        $this->write(WebSocketFrame::pack('', WebSocketFrame::OP_PING));
85
    }
86
87
    public function writeText (string $message) {
88
        $packed = WebSocketFrame::pack($message);
89
        $this->write($packed);
90
    }
91
}