Completed
Push — master ( 6c70b9...924d4f )
by Charlotte
9s
created

CloseFrameHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 42
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
D process() 0 34 10
1
<?php
2
/**
3
 * This file is a part of Woketo package.
4
 *
5
 * (c) Nekland <[email protected]>
6
 *
7
 * For the full license, take a look to the LICENSE file
8
 * on the root directory of this project
9
 */
10
11
namespace Nekland\Woketo\Rfc6455\MessageHandler;
12
13
use Nekland\Woketo\Rfc6455\Frame;
14
use Nekland\Woketo\Rfc6455\Message;
15
use Nekland\Woketo\Rfc6455\MessageProcessor;
16
use Nekland\Woketo\Utils\BitManipulation;
17
use React\Socket\ConnectionInterface;
18
19
class CloseFrameHandler implements Rfc6455MessageHandlerInterface
20
{
21 8
    public function supports(Message $message)
22
    {
23 8
        return $message->getFirstFrame()->getOpcode() === Frame::OP_CLOSE;
24
    }
25
26 8
    public function process(Message $message, MessageProcessor $messageProcessor, ConnectionInterface $socket)
27
    {
28 8
        $code = Frame::CLOSE_NORMAL;
29
30 8
        $frame = $message->getFirstFrame();
31 8
        $payload = $frame->getPayload();
32 8
        if ($frame->getRsv1() || $frame->getRsv2() || $frame->getRsv3()) {
33 1
            $code = Frame::CLOSE_PROTOCOL_ERROR;
34
        }
35
36 8
        if (BitManipulation::frameSize($payload) > 1) {
37 8
            $errorCode = BitManipulation::bytesFromTo($payload, 0, 1);
38
39
            // https://tools.ietf.org/html/rfc6455#section-7.4
40
            if (
41
                (
42 8
                    !in_array($errorCode, [
43 8
                        Frame::CLOSE_NORMAL, Frame::CLOSE_GOING_AWAY, Frame::CLOSE_PROTOCOL_ERROR, Frame::CLOSE_GOING_AWAY,
44
                        Frame::CLOSE_WRONG_DATA, Frame::CLOSE_INCOHERENT_DATA, Frame::CLOSE_TOO_BIG_TO_PROCESS,
45
                        Frame::CLOSE_POLICY_VIOLATION, Frame::CLOSE_MISSING_EXTENSION, Frame::CLOSE_UNEXPECTING_CONDITION
46
                    ])
47 8
                    && $errorCode < 3000 && $errorCode > 900
48
                )
49 6
                || $errorCode > 4999
50 8
                || $errorCode < 1000
51
            ) {
52 4
                $code = Frame::CLOSE_PROTOCOL_ERROR;
53
            }
54
        }
55
56
57 8
        $messageProcessor->write($messageProcessor->getFrameFactory()->createCloseFrame($code), $socket);
58 8
        $socket->end();
59 8
    }
60
}
61