CloseFrameHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
B process() 0 33 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\FrameHandler;
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 Rfc6455FrameHandlerInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 8
    public function supports(Message $message)
25
    {
26 8
        return $message->getFirstFrame()->getOpcode() === Frame::OP_CLOSE;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 8
    public function process(Message $message, MessageProcessor $messageProcessor, ConnectionInterface $socket)
33
    {
34 8
        $code = Frame::CLOSE_NORMAL;
35
36 8
        $frame = $message->getFirstFrame();
37 8
        $payload = $frame->getPayload();
38 8
        if ($frame->getRsv1() || $frame->getRsv2() || $frame->getRsv3()) {
39 1
            $code = Frame::CLOSE_PROTOCOL_ERROR;
40
        }
41
42 8
        if (BitManipulation::frameSize($payload) > 1) {
43 8
            $errorCode = BitManipulation::bytesFromTo($payload, 0, 1);
44
45
            // https://tools.ietf.org/html/rfc6455#section-7.4
46
            if (
47
                (
48 8
                    !\in_array($errorCode, [
49 8
                        Frame::CLOSE_NORMAL, Frame::CLOSE_GOING_AWAY, Frame::CLOSE_PROTOCOL_ERROR, Frame::CLOSE_GOING_AWAY,
50
                        Frame::CLOSE_WRONG_DATA, Frame::CLOSE_INCOHERENT_DATA, Frame::CLOSE_TOO_BIG_TO_PROCESS,
51
                        Frame::CLOSE_POLICY_VIOLATION, Frame::CLOSE_MISSING_EXTENSION, Frame::CLOSE_UNEXPECTING_CONDITION
52
                    ])
53 8
                    && $errorCode < 3000 && $errorCode > 900
54
                )
55 6
                || $errorCode > 4999
56 8
                || $errorCode < 1000
57
            ) {
58 4
                $code = Frame::CLOSE_PROTOCOL_ERROR;
59
            }
60
        }
61
62 8
        $messageProcessor->write($messageProcessor->getFrameFactory()->createCloseFrame($code), $socket);
63 8
        $socket->end();
64 8
    }
65
}
66