Completed
Pull Request — master (#100)
by Maxime
03:08
created

WebSocketClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 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
        $this->connectorFactory->enableDns();
94
        if ($this->url->isSecured()) {
95
            $this->connectorFactory->enableSsl();
96
        }
97
98
        return $this->connectorFactory;
99
    }
100
101
    /**
102
     * @return LoopInterface
103
     */
104
    private function getLoop() : LoopInterface
105
    {
106
        if (null !== $this->loop) {
107
            return $this->loop;
108
        }
109
110
        return $this->loop = LoopFactory::create();
111
    }
112
113
    private function getMessageProcessor()
114
    {
115
        if (!empty($this->messageProcessor)) {
116
            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...
117
        }
118
119
        return $this->messageProcessor = new MessageProcessor();
120
    }
121
}
122