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

ConnectorFactory::createConnector()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
crap 12
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\Dns\Resolver\Factory as DnsResolverFactory;
15
use React\EventLoop\LoopInterface;
16
use React\SocketClient\ConnectorInterface;
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
    /**
45
     * @var array
46
     */
47
    private $sslOptions;
48
49
    public function __construct()
50
    {
51
        $this->dnsEnabled = false;
52
        $this->sslEnabled = false;
53
        $this->dnsServer = '8.8.8.8'; // Google DNS
54
        $this->sslOptions = [];
55
    }
56
57
    /**
58
     * @return ConnectorFactory
59
     */
60
    public function enableDns(): ConnectorFactory
61
    {
62
        $this->dnsEnabled = true;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @return ConnectorFactory
69
     */
70
    public function disableDns(): ConnectorFactory
71
    {
72
        $this->dnsEnabled = false;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return ConnectorFactory
79
     */
80
    public function enableSsl(): ConnectorFactory
81
    {
82
        $this->sslEnabled = true;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return ConnectorFactory
89
     */
90
    public function disableSsl(): ConnectorFactory
91
    {
92
        $this->sslEnabled = true;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $server    Ip address of the DNS server you want to contact.
99
     * @return ConnectorFactory
100
     */
101
    public function setDnsServer(string $server): ConnectorFactory
102
    {
103
        $this->dnsServer = $server;
104
105
        return $this;
106
    }
107
108
    /**
109
     * This allows the user to use its own loop to potentially use it for something else.
110
     *
111
     * @param LoopInterface $loop
112
     * @return ConnectorFactoryInterface
113
     */
114
    public function setLoop(LoopInterface $loop): ConnectorFactoryInterface
115
    {
116
        $this->loop = $loop;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param array $options
123
     * @return ConnectorFactory
124
     */
125
    public function setSslOptions(array $options): ConnectorFactory
126
    {
127
        $this->sslOptions = $options;
128
129
        return $this;
130
    }
131
132
    public function createConnector(): ConnectorInterface
133
    {
134
        $connector = new TcpConnector($this->loop);
135
136
        if ($this->dnsEnabled) {
137
            $resolver = (new DnsResolverFactory())->create($this->dnsServer, $this->loop);
138
            $connector = new DnsConnector($connector, $resolver);
139
        }
140
141
        if ($this->sslEnabled) {
142
            $connector = new SecureConnector($connector, $this->loop, $this->sslOptions);
143
        }
144
145
        $connector = new TimeoutConnector($connector, 3.0, $this->loop);
146
147
        return $connector;
148
    }
149
}
150