Passed
Push — master ( a91d54...875978 )
by y
02:08
created

MessageHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 108
rs 10
c 1
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A onText() 0 3 1
A setBinaryStream() 0 3 1
A isBinaryStream() 0 2 1
A onText_CheckUtf8() 0 3 2
A getMaxLength() 0 2 1
A onBinary() 0 3 1
A setTextStream() 0 3 1
A setMaxLength() 0 3 1
A __construct() 0 2 1
A isTextStream() 0 2 1
1
<?php
2
3
namespace Helix\Socket\WebSocket;
4
5
class MessageHandler {
6
7
    /**
8
     * Whether to handle each binary frame as a message.
9
     *
10
     * @var bool
11
     */
12
    protected $binaryStream = true;
13
14
    /**
15
     * @var WebSocketClient
16
     */
17
    protected $client;
18
19
    /**
20
     * The maximum size of a complete message.
21
     *
22
     * @var int
23
     */
24
    protected $maxLength = 10 * 1024 * 1024; // 10 MiB
25
26
    /**
27
     * Whether to handle each text frame as a message.
28
     *
29
     * @var bool
30
     */
31
    protected $textStream = true;
32
33
    /**
34
     * @param WebSocketClient $client
35
     */
36
    public function __construct (WebSocketClient $client) {
37
        $this->client = $client;
38
    }
39
40
    /**
41
     * @return int
42
     */
43
    final public function getMaxLength (): int {
44
        return $this->maxLength;
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    final public function isBinaryStream (): bool {
51
        return $this->binaryStream;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    final public function isTextStream (): bool {
58
        return $this->textStream;
59
    }
60
61
    /**
62
     * @param string $binary
63
     */
64
    public function onBinary (string $binary): void {
65
        unset($binary);
66
        throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data.");
67
    }
68
69
    /**
70
     * @param string $text
71
     */
72
    public function onText (string $text): void {
73
        $this->onText_CheckUtf8($text);
74
        throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text.");
75
    }
76
77
    /**
78
     * Throws if the payload isn't UTF-8.
79
     *
80
     * @param string $text
81
     */
82
    protected function onText_CheckUtf8 (string $text): void {
83
        if (!mb_detect_encoding($text, 'UTF-8', true)) {
84
            throw new WebSocketError(Frame::CLOSE_BAD_DATA, "Received TEXT is not UTF-8.");
85
        }
86
    }
87
88
    /**
89
     * @param bool $flag
90
     * @return $this
91
     */
92
    public function setBinaryStream (bool $flag) {
93
        $this->binaryStream = $flag;
94
        return $this;
95
    }
96
97
    /**
98
     * @param int $maxLength
99
     * @return $this
100
     */
101
    public function setMaxLength (int $maxLength) {
102
        $this->maxLength = $maxLength;
103
        return $this;
104
    }
105
106
    /**
107
     * @param bool $flag
108
     * @return $this
109
     */
110
    public function setTextStream (bool $flag) {
111
        $this->textStream = $flag;
112
        return $this;
113
    }
114
115
}