Completed
Push — master ( ccb503...769203 )
by Dawid
02:33
created

Configuration   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.71%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 240
ccs 55
cts 62
cp 0.8871
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getPort() 0 4 1
A getAddress() 0 4 1
A isSslEnabled() 0 4 1
A enableSsl() 0 15 3
A setResponseCompression() 0 4 1
A isDaemonEnabled() 0 4 2
A setMaxConnections() 0 4 1
A setWorkers() 0 4 1
A setMaxRequests() 0 4 1
A setMaximumBacklog() 0 4 1
A setDispatchMode() 0 4 1
A enableDaemon() 0 11 3
A setUploadDir() 0 4 1
A setOwnerGroup() 0 4 1
A setChroot() 0 8 2
A setBufferOutputSize() 0 4 1
A toArray() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Network\Server;
4
5
use Igni\Network\Exception\ConfigurationException;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
9
/**
10
 * Class ServerConfiguration
11
 * @package Igni\Http\Server
12
 */
13
class Configuration
14
{
15
    /**
16
     * Dispatch the connection to the workers in sequence.
17
     * Recommended for stateless asynchronous server.
18
     */
19
    public const DISPATCH_POLLING_MODE = 1;
20
21
    /**
22
     * Dispatch the connection to the worker according to the id number of connection.
23
     * In this mode, the data from the same connection will be handled by the same worker process.
24
     * Recommended for stateful server.
25
     */
26
    public const DISPATCH_FIXED_MODE = 2;
27
28
    /**
29
     * Dispatch the connection to the unoccupied worker process.
30
     * Recommended for stateless, synchronous and blocking server.
31
     */
32
    public const DISPATCH_PREEMPTIVE_MODE = 3;
33
34
    /**
35
     * Dispatch the connection to the worker according to the ip of client.
36
     * The dispatch algorithm is ip2long(ClientIP) % worker_num
37
     */
38
    public const DISPATCH_IP_MODE = 4;
39
40
    /**
41
     * Used if none provided in the constructor
42
     */
43
    public const DEFAULT_ADDRESS = '0.0.0.0';
44
45
    /**
46
     * Used if none provided in the constructor
47
     */
48
    public const DEFAULT_PORT = 80;
49
50
    /**
51
     * @var array
52
     */
53
    private $settings = [];
54
55
    /**
56
     * HttpConfiguration constructor.
57
     *
58
     * @param string $address
59
     * @param int $port
60
     */
61 30
    public function __construct(int $port = self::DEFAULT_PORT, string $address = self::DEFAULT_ADDRESS)
62
    {
63 30
        $this->settings['address'] = $address;
64 30
        $this->settings['port'] = $port;
65 30
    }
66
67 3
    public function getPort(): int
68
    {
69 3
        return $this->settings['port'];
70
    }
71
72 3
    public function getAddress(): string
73
    {
74 3
        return $this->settings['address'];
75
    }
76
77
    /**
78
     * Checks if ssl is enabled.
79
     *
80
     * @return bool
81
     */
82 1
    public function isSslEnabled(): bool
83
    {
84 1
        return isset($this->settings['ssl_cert_file']);
85
    }
86
87
    /**
88
     * Enables ssl on the server
89
     *
90
     * @param string $certFile
91
     * @param string $keyFile
92
     */
93 4
    public function enableSsl(string $certFile, string $keyFile): void
94
    {
95 4
        if (!is_readable($certFile)) {
96 1
            throw ConfigurationException::forInvalidSslCertFile($certFile);
97
        }
98
99 3
        if (!is_readable($keyFile)) {
100 1
            throw ConfigurationException::forInvalidSslKeyFile($keyFile);
101
        }
102
103
        $this->settings += [
104 2
            'ssl_cert_file' => $certFile,
105 2
            'ssl_key_file' => $keyFile,
106
        ];
107 2
    }
108
109
    /**
110
     * Sets response compression level 0 - no compression 9 - high compression
111
     *
112
     * @param int $level
113
     */
114
    public function setResponseCompression(int $level = 0): void
115
    {
116
        $this->settings['compression_level'] = $level;
117
    }
118
119
    /**
120
     * Checks if server is daemonized.
121
     *
122
     * @return bool
123
     */
124 1
    public function isDaemonEnabled(): bool
125
    {
126 1
        return isset($this->settings['daemonize']) && $this->settings['daemonize'];
127
    }
128
129
    /**
130
     * Sets the max tcp connection number of the server.
131
     *
132
     * @param int $max
133
     */
134 1
    public function setMaxConnections(int $max = 10000): void
135
    {
136 1
        $this->settings['max_conn'] = $max;
137 1
    }
138
139
    /**
140
     * Sets the number of worker processes.
141
     *
142
     * @param int $count
143
     */
144 1
    public function setWorkers(int $count = 1): void
145
    {
146 1
        $this->settings['worker_num'] = $count;
147 1
    }
148
149
    /**
150
     * Sets the number of requests processed by the worker process before process manager recycles it.
151
     * Once process is recycled (memory used by process is freed and process is killed) process manger
152
     * will spawn new worker.
153
     *
154
     * @param int $max
155
     */
156 1
    public function setMaxRequests(int $max = 0): void
157
    {
158 1
        $this->settings['max_request'] = $max;
159 1
    }
160
161
    /**
162
     * Sets the maximum number of pending connections. This refers to the number of clients
163
     * that can be waiting to be served. Exceeding this number results in the client getting
164
     * an error when attempting to connect.
165
     *
166
     * @param int $max
167
     */
168 1
    public function setMaximumBacklog(int $max = 0): void
169
    {
170 1
        $this->settings['backlog'] = $max;
171 1
    }
172
173
    /**
174
     * Sets dispatch mode for child processes works only if the server is run in process mode.
175
     *
176
     * @param int $mode
177
     */
178 1
    public function setDispatchMode(int $mode = self::DISPATCH_FIXED_MODE): void
179
    {
180 1
        $this->settings['dispatch_mode'] = $mode;
181 1
    }
182
183
    /**
184
     * Allows server to be run as a background process.
185
     *
186
     * @param string $pidFile
187
     */
188 1
    public function enableDaemon(string $pidFile): void
189
    {
190 1
        if (!is_writable($pidFile) && !is_writable(dirname($pidFile))) {
191
            throw ConfigurationException::forUnavailablePidFile($pidFile);
192
        }
193
194
        $this->settings += [
195 1
            'daemonize' => true,
196 1
            'pid_file' => $pidFile,
197
        ];
198 1
    }
199
200
    /**
201
     * Sets temporary dir for uploaded files
202
     *
203
     * @param string $dir
204
     */
205 1
    public function setUploadDir(string $dir): void
206
    {
207 1
        $this->settings['upload_tmp_dir'] = $dir;
208 1
    }
209
210
    /**
211
     * Sets the group of worker and task worker process.
212
     * @param string $group
213
     */
214 1
    public function setOwnerGroup(string $group): void
215
    {
216 1
        $this->settings['group'] = $group;
217 1
    }
218
219
    /**
220
     * Redirect the root path of worker process.
221
     * @param string $dir
222
     */
223 1
    public function setChroot(string $dir): void
224
    {
225 1
        if (!is_dir($dir)) {
226
            throw ConfigurationException::forInvalidDirectory($dir);
227
        }
228
229 1
        $this->settings['chroot'] = $dir;
230 1
    }
231
232
    /**
233
     * Set the output buffer size in the memory. The default value is 2M.
234
     * The data to send can't be larger than the $size every request.
235
     *
236
     * @param int $size bytes
237
     */
238 1
    public function setBufferOutputSize(int $size = 2 * 1024 * 1024): void
239
    {
240 1
        $this->settings['buffer_output_size'] = $size;
241 1
    }
242
243
    /**
244
     * Returns swoole compatible settings array.
245
     *
246
     * @return array
247
     */
248 11
    public function toArray(): array
249
    {
250 11
        return $this->settings;
251
    }
252
}
253