Completed
Pull Request — master (#100)
by Maxime
02:48
created

AbstractConnection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 22.22%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 72
ccs 4
cts 18
cp 0.2222
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A onMessage() 0 16 3
processHandshake() 0 1 ?
processMessage() 0 1 ?
A getIp() 0 4 1
A getLogger() 0 4 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\WebsocketException;
15
use Nekland\Woketo\Message\MessageHandlerInterface;
16
use Nekland\Woketo\Rfc6455\Handshake\HandshakeInterface;
17
use Nekland\Woketo\Rfc6455\MessageProcessor;
18
use Psr\Log\LoggerAwareTrait;
19
use React\Socket\Connection;
20
21
abstract class AbstractConnection
22
{
23
    use LoggerAwareTrait;
24
    /**
25
     * @var Connection
26
     */
27
    protected $stream;
28
29
    /**
30
     * @var MessageProcessor
31
     */
32
    protected $messageProcessor;
33
34
    /**
35
     * @var HandshakeInterface
36
     */
37
    protected $handshake;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $handshakeDone;
43
44
    /**
45
     * @var MessageHandlerInterface
46
     */
47
    protected $handler;
48
49 2
    public function __construct(
50
        MessageProcessor $messageProcessor,
51
        HandshakeInterface $handshake = null
52
    ) {
53 2
        $this->handshake = $handshake;
54 2
        $this->messageProcessor = $messageProcessor;
55 2
    }
56
57
    protected function onMessage(string $data)
58
    {
59
        try {
60
            if (!$this->handshakeDone) {
61
                $this->processHandshake($data);
62
            } else {
63
                $this->processMessage($data);
64
            }
65
66
            return;
67
        } catch (WebsocketException $e) {
68
            $this->messageProcessor->close($this->stream);
69
            $this->logger->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage());
70
            $this->handler->onError($e, $this);
71
        }
72
    }
73
74
    protected abstract function processHandshake(string $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
75
    protected abstract function processMessage(string $data);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
76
77
    /**
78
     * @return string
79
     */
80
    public function getIp()
81
    {
82
        return $this->stream->getRemoteAddress();
83
    }
84
85
    /**
86
     * @return \Psr\Log\LoggerInterface
87
     */
88
    public function getLogger()
89
    {
90
        return $this->logger;
91
    }
92
}