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

ConnectorFactory::setLoop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 React\EventLoop\Factory as LoopFactory;
15
use React\Dns\Resolver\Factory as DnsResolverFactory;
16
use React\EventLoop\LoopInterface;
17
use React\SocketClient\DnsConnector;
18
use React\SocketClient\SecureConnector;
19
use React\SocketClient\TcpConnector;
20
use React\SocketClient\TimeoutConnector;
21
22
class ConnectorFactory implements ConnectorFactoryInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $dnsServer;
28
29
    /**
30
     * @var bool
31
     */
32
    private $dnsEnabled;
33
34
    /**
35
     * @var bool
36
     */
37
    private $sslEnabled;
38
39
    /**
40
     * @var LoopInterface
41
     */
42
    private $loop;
43
44
    public function __construct()
45
    {
46
        $this->dnsEnabled = false;
47
        $this->sslEnabled = false;
48
        $this->dnsServer = '8.8.8.8'; // Google DNS
49
    }
50
51
    /**
52
     * @return ConnectorFactory
53
     */
54
    public function enableDns() : ConnectorFactory
55
    {
56
        $this->dnsEnabled = true;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return ConnectorFactory
63
     */
64
    public function disableDns() : ConnectorFactory
65
    {
66
        $this->dnsEnabled = false;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return ConnectorFactory
73
     */
74
    public function enableSsl() : ConnectorFactory
75
    {
76
        $this->sslEnabled = true;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return ConnectorFactory
83
     */
84
    public function disableSsl() : ConnectorFactory
85
    {
86
        $this->sslEnabled = true;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $server    Ip address of the DNS server you want to contact.
93
     * @return ConnectorFactory
94
     */
95
    public function setDnsServer(string $server) : ConnectorFactory
96
    {
97
        $this->dnsServer = $server;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param LoopInterface $loop
104
     * @return ConnectorFactory
105
     */
106
    public function setLoop(LoopInterface $loop) : ConnectorFactory
107
    {
108
        $this->loop = $loop;
109
110
        return $this;
111
    }
112
113
    public function createConnector(string $host, int $port, bool $secured = false, array $sslConfig = [])
114
    {
115
        $connector = new TcpConnector($this->loop);
116
117
        if ($this->dnsEnabled) {
118
            $resolver = (new DnsResolverFactory())->create($this->dnsServer, $this->loop);
119
            $connector = new DnsConnector($connector, $resolver);
120
        }
121
122
        if ($this->sslEnabled) {
123
            $connector = new SecureConnector($connector, $this->loop, $sslConfig);
124
        }
125
126
        $connector = new TimeoutConnector($connector, 3.0, $this->loop);
127
128
        return $connector->connect($host . ':' . $port);
129
    }
130
}
131