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

WebSocketClient::getLoop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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
    /**
55
     * @param MessageHandlerInterface $handler
56
     * @throws \Exception
57
     */
58
    public function start(MessageHandlerInterface $handler)
59
    {
60
        if ($this->config['prod'] && \extension_loaded('xdebug')) {
61
            throw new \Exception('xdebug is enabled, it\'s a performance issue. Disable that extension or specify "prod" option to false.');
62
        }
63
64
        $this->connection = new Connection(
65
            $this->url,
66
            $this->getConnectorFactory()->createConnector()->connect($this->url->getHost() . ':' . $this->url->getPort()),
67
            $this->getMessageProcessor(),
68
            $handler
69
        );
70
71
        $this->loop->run();
72
    }
73
74
    /**
75
     * @param array $config
76
     * @return self
77
     */
78
    public function setConfig(array $config = [])
79
    {
80
        $this->config = array_merge([
81
            'prod' => true,
82
            'ssl' => [],
83
            'dns' => null,
84
        ], $config);
85
86
        return $this;
87
    }
88
89
    /**
90
     * Creates a connector factory with the given configuration if none given in the constructor.
91
     *
92
     * @return ConnectorFactoryInterface
93
     */
94
    private function getConnectorFactory() : ConnectorFactoryInterface
95
    {
96
        if ($this->connectorFactory !== null) {
97
            return $this->connectorFactory;
98
        }
99
        $this->connectorFactory = new ConnectorFactory();
100
        $this->connectorFactory->setLoop($this->getLoop());
101
        $this->connectorFactory->setSslOptions($this->config['ssl']);
102
103
        $this->connectorFactory->enableDns();
104
        if (null !== $this->config['dns']) {
105
            $this->connectorFactory->setDnsServer($this->config['dns']);
106
        }
107
        if ($this->url->isSecured()) {
108
            $this->connectorFactory->enableSsl();
109
        }
110
111
        return $this->connectorFactory;
112
    }
113
114
    /**
115
     * @return LoopInterface
116
     */
117
    private function getLoop() : LoopInterface
118
    {
119
        if (null !== $this->loop) {
120
            return $this->loop;
121
        }
122
123
        return $this->loop = LoopFactory::create();
124
    }
125
126
    /**
127
     * @return MessageProcessor
128
     */
129
    private function getMessageProcessor(): MessageProcessor
130
    {
131
        if (!empty($this->messageProcessor)) {
132
            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...
133
        }
134
135
        return $this->messageProcessor = new MessageProcessor();
136
    }
137
}
138