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

CloseFrameHandler::process()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 4.8196
cc 10
eloc 19
nc 6
nop 3
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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