Completed
Push — master ( 581697...be91d0 )
by Maxime
11s
created

AbstractConnection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 67.74%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 156
ccs 21
cts 31
cp 0.6774
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
processHandshake() 0 1 ?
processMessage() 0 1 ?
getIp() 0 1 ?
A onMessage() 0 16 3
A getLogger() 0 4 2
A getHandler() 0 15 3
A close() 0 4 1
A getLoop() 0 4 1
write() 0 1 ?
A __construct() 0 7 1
1
<?php
2
3
/**
4
 * This file is a part of Woketo package.
5
 *
6
 * (c) Nekland <[email protected]>
7
 *
8
 * For the full license, take a look to the LICENSE file
9
 * on the root directory of this project
10
 */
11
12
namespace Nekland\Woketo\Core;
13
14
use Nekland\Woketo\Exception\NoHandlerException;
15
use Nekland\Woketo\Exception\WebsocketException;
16
use Nekland\Woketo\Message\MessageHandlerInterface;
17
use Nekland\Woketo\Rfc6455\Frame;
18
use Nekland\Woketo\Rfc6455\Handshake\HandshakeInterface;
19
use Nekland\Woketo\Rfc6455\Message;
20
use Nekland\Woketo\Rfc6455\MessageProcessor;
21
use Nekland\Woketo\Utils\SimpleLogger;
22
use Psr\Log\LoggerAwareTrait;
23
use Psr\Log\NullLogger;
24
use React\EventLoop\LoopInterface;
25
use React\EventLoop\TimerInterface;
26
use React\Socket\ConnectionInterface;
27
28
abstract class AbstractConnection
29
{
30
    /**
31
     * 5 seconds
32
     */
33
    const DEFAULT_TIMEOUT = 5;
34
35
    use LoggerAwareTrait;
36
37
    /**
38
     * @var ConnectionInterface
39
     */
40
    protected $stream;
41
42
    /**
43
     * @var MessageProcessor
44
     */
45
    protected $messageProcessor;
46
47
    /**
48
     * @var \Nekland\Woketo\Rfc6455\Handshake\ServerHandshake|\Nekland\Woketo\Rfc6455\Handshake\ClientHandshake
49
     */
50
    protected $handshake;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $handshakeDone;
56
57
    /**
58
     * @var string
59
     */
60
    protected $uri;
61
62
    /**
63
     * @var MessageHandlerInterface|callable
64
     */
65
    protected $handler;
66
67
    /**
68
     * @var TimerInterface
69
     */
70
    protected $timeout;
71
72
    /**
73
     * @var Message
74
     */
75
    protected $currentMessage;
76
77
    /**
78
     * @var LoopInterface
79
     */
80
    protected $loop;
81
82 12
    public function __construct(MessageProcessor $messageProcessor, LoopInterface $loop, HandshakeInterface $handshake = null)
83
    {
84 12
        $this->handshake = $handshake;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handshake can also be of type object<Nekland\Woketo\Rf...ake\HandshakeInterface>. However, the property $handshake is declared as type object<Nekland\Woketo\Rf...dshake\ClientHandshake>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
85 12
        $this->messageProcessor = $messageProcessor;
86 12
        $this->loop = $loop;
87 12
        $this->logger = new NullLogger();
88 12
    }
89
90
    /**
91
     * Behavior on new raw data received.
92
     *
93
     * @param string $data
94
     */
95
    protected function onMessage(string $data)
96
    {
97
        try {
98
            if (!$this->handshakeDone) {
99
                $this->processHandshake($data);
100
            } else {
101
                $this->processMessage($data);
102
            }
103
104
            return;
105
        } catch (WebsocketException $e) {
106
            $this->messageProcessor->close($this->stream);
107
            $this->getLogger()->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage());
108
            $this->handler->onError($e, $this);
0 ignored issues
show
Bug introduced by
The method onError cannot be called on $this->handler (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
        }
110
    }
111
112
    /**
113
     * @param string $data
114
     * @return void
115
     */
116
    protected abstract function processHandshake(string $data);
117
118
    /**
119
     * @param string $data
120
     * @return void
121
     */
122
    protected abstract function processMessage(string $data);
123
124
    /**
125
     * May return ip or hostname
126
     *
127
     * @return string
128
     */
129
    public abstract function getIp();
130
131
    /**
132
     * @return \Psr\Log\LoggerInterface
133
     */
134 5
    public function getLogger()
135
    {
136 5
        return $this->logger ?: $this->logger = new SimpleLogger();
137
    }
138
139
    /**
140
     * @return MessageHandlerInterface
141
     * @throws NoHandlerException
142
     */
143 8
    protected function getHandler() : MessageHandlerInterface
144
    {
145 8
        if (!$this->handler instanceof MessageHandlerInterface) {
146 8
            $handler = $this->handler;
147 8
            $handler = $handler($this->uri, $this);
148
149 8
            if (null === $handler) {
150 1
                throw new NoHandlerException(sprintf('No handler for request URI %s.', $this->uri));
151
            }
152
153 7
            $this->handler = $handler;
154
        }
155
156 7
        return $this->handler;
157
    }
158
159
    /**
160
     * Close the connection with normal close.
161
     * @param int $status
162
     * @param string|null $reason
163
     */
164 1
    public function close(int $status = Frame::CLOSE_NORMAL, string $reason = null)
165
    {
166 1
        $this->messageProcessor->close($this->stream, $status, $reason);
167 1
    }
168
169
    /**
170
     * @return LoopInterface
171
     */
172 1
    public function getLoop(): LoopInterface
173
    {
174 1
        return $this->loop;
175
    }
176
177
    /**
178
     * @param string|Frame  $frame
179
     * @param int           $opCode
180
     * @throws \Nekland\Woketo\Exception\RuntimeException
181
     */
182
    public abstract function write($frame, int $opCode = Frame::OP_TEXT);
183
}
184