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

WebSocketClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A start() 0 15 3
A setConfig() 0 8 1
A getConnectorFactory() 0 9 2
A getLoop() 0 8 2
A getMessageProcessor() 0 8 2
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\Client;
13
14
use Nekland\Woketo\Http\Url;
15
use Nekland\Woketo\Message\MessageHandlerInterface;
16
use Nekland\Woketo\Rfc6455\MessageProcessor;
17
use React\EventLoop\Factory as LoopFactory;
18
use React\EventLoop\LoopInterface;
19
20
class WebSocketClient
21
{
22
    /**
23
     * @var Url
24
     */
25
    private $url;
26
27
    /**
28
     * @var array
29
     */
30
    private $config;
31
32
    /**
33
     * @var Connection
34
     */
35
    private $connection;
36
37
    /**
38
     * @var ConnectorFactoryInterface
39
     */
40
    private $connectorFactory;
41
42
    /**
43
     * @var LoopInterface
44
     */
45
    private $loop;
46
47
    public function __construct(string $url, array $config = [], ConnectorFactoryInterface $connectorFactory = null)
48
    {
49
        $this->url = new Url($url);
50
        $this->connectorFactory = $connectorFactory;
51
        $this->setConfig($config);
52
    }
53
54
    public function start(MessageHandlerInterface $handler)
55
    {
56
        if ($this->config['prod'] && \extension_loaded('xdebug')) {
57
            throw new \Exception('xdebug is enabled, it\'s a performance issue. Disable that extension or specify "prod" option to false.');
58
        }
59
60
        $this->connection = new Connection(
61
            $this->url,
62
            $this->getConnectorFactory()->createConnector($this->url->getHost(), $this->url->getPort()),
63
            $this->getMessageProcessor(),
64
            $handler
65
        );
66
67
        $this->loop->run();
68
    }
69
70
    /**
71
     * @param array $config
72
     * @return self
73
     */
74
    public function setConfig(array $config = [])
75
    {
76
        $this->config = array_merge([
77
            'prod' => true
78
        ], $config);
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return ConnectorFactory
85
     */
86
    private function getConnectorFactory() : ConnectorFactory
87
    {
88
        if ($this->connectorFactory === null) {
89
            $this->connectorFactory = new ConnectorFactory();
90
        }
91
        $this->connectorFactory->setLoop($this->getLoop());
92
93
        return $this->connectorFactory;
94
    }
95
96
    /**
97
     * @return LoopInterface
98
     */
99
    private function getLoop() : LoopInterface
100
    {
101
        if (null !== $this->loop) {
102
            return $this->loop;
103
        }
104
105
        return $this->loop = LoopFactory::create();
106
    }
107
108
    private function getMessageProcessor()
109
    {
110
        if (!empty($this->messageProcessor)) {
111
            return $this->messageProcessor;
0 ignored issues
show
Bug introduced by
The property messageProcessor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
112
        }
113
114
        return $this->messageProcessor = new MessageProcessor();
115
    }
116
}
117