Completed
Push — master ( e26ab3...1dd3f4 )
by Maxime
9s
created

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