Completed
Pull Request — master (#100)
by Maxime
03:29
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\Message\MessageHandlerInterface;
15
use Nekland\Woketo\Rfc6455\MessageProcessor;
16
use React\EventLoop\Factory as LoopFactory;
17
use React\EventLoop\LoopInterface;
18
19
class WebSocketClient
20
{
21
    /**
22
     * @var int
23
     */
24
    private $port;
25
26
    /**
27
     * @var string
28
     */
29
    private $host;
30
31
    /**
32
     * @var array
33
     */
34
    private $config;
35
36
    /**
37
     * @var Connection
38
     */
39
    private $connection;
40
41
    /**
42
     * @var ConnectorFactoryInterface
43
     */
44
    private $connectorFactory;
45
46
    /**
47
     * @var LoopInterface
48
     */
49
    private $loop;
50
51
    public function __construct(int $port, string $host, array $config = [], ConnectorFactoryInterface $connectorFactory = null)
52
    {
53
        $this->port = $port;
54
        $this->host = $host;
55
        $this->connectorFactory = $connectorFactory;
56
        $this->setConfig($config);
57
    }
58
59
    public function start(MessageHandlerInterface $handler)
60
    {
61
        if ($this->config['prod'] && \extension_loaded('xdebug')) {
62
            throw new \Exception('xdebug is enabled, it\'s a performance issue. Disable that extension or specify "prod" option to false.');
63
        }
64
65
        $this->connection = new Connection(
66
            $this->port,
67
            $this->host,
68
            $this->getConnectorFactory()->createConnector($this->host, $this->port),
69
            $this->getMessageProcessor(),
70
            $handler
71
        );
72
73
        $this->loop->run();
74
    }
75
76
    /**
77
     * @param array $config
78
     * @return self
79
     */
80
    public function setConfig(array $config = [])
81
    {
82
        $this->config = array_merge([
83
            'prod' => true
84
        ], $config);
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return ConnectorFactory
91
     */
92
    private function getConnectorFactory() : ConnectorFactory
93
    {
94
        if ($this->connectorFactory === null) {
95
            $this->connectorFactory = new ConnectorFactory();
96
        }
97
        $this->connectorFactory->setLoop($this->getLoop());
98
99
        return $this->connectorFactory;
100
    }
101
102
    /**
103
     * @return LoopInterface
104
     */
105
    private function getLoop() : LoopInterface
106
    {
107
        if (null !== $this->loop) {
108
            return $this->loop;
109
        }
110
111
        return $this->loop = LoopFactory::create();
112
    }
113
114
    private function getMessageProcessor()
115
    {
116
        if (!empty($this->messageProcessor)) {
117
            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...
118
        }
119
120
        return $this->messageProcessor = new MessageProcessor();
121
    }
122
}
123