Passed
Push — master ( 2f7798...928ca5 )
by Arthur
01:38
created

ServerConfig::getLoopingDelay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace WSSC\Components;
4
5
use Exception;
6
use WSSC\Contracts\WebSocketServerContract;
7
8
class ServerConfig
9
{
10
    /**
11
     * @var int
12
     */
13
    private int $clientsPerFork = WebSocketServerContract::CLIENTS_PER_FORK;
14
15
    /**
16
     * @var int|null
17
     */
18
    private ?int $streamSelectTimeout = WebSocketServerContract::STREAM_SELECT_TIMEOUT;
19
20
    /**
21
     * @var string
22
     */
23
    private string $host = WebSocketServerContract::DEFAULT_HOST;
24
25
    /**
26
     * @var int
27
     */
28
    private int $port = WebSocketServerContract::DEFAULT_PORT;
29
30
    /**
31
     * @var bool
32
     */
33
    private bool $isForking = true;
34
35
    /**
36
     * @var string
37
     */
38
    private string $processName = WebSocketServerContract::PROC_TITLE;
39
40
    /**
41
     * @var bool
42
     */
43
    private bool $checkOrigin = false;
44
45
    /**
46
     * @var array
47
     */
48
    private array $origins = [];
49
50
    /**
51
     * @var bool
52
     */
53
    private bool $originHeader = false;
54
55
    /**
56
     * @var bool
57
     */
58
    private bool $isSsl = false;
59
60
    /**
61
     * @var string
62
     */
63
    private string $localCert;
64
65
    /**
66
     * @var string
67
     */
68
    private string $localPk;
69
70
    /**
71
     * @var bool
72
     */
73
    private bool $allowSelfSigned;
74
75
    /**
76
     * @var int
77
     */
78
    private int $cryptoType;
79
80
    /**
81
     * @var int
82
     */
83
    private int $loopingDelay = 0;
84
85
    /**
86
     * @var array
87
     */
88
    private array $loopingDelayRange = [0, 1000];
89
90
    /**
91
     * @return int
92
     */
93
    public function getClientsPerFork(): int
94
    {
95
        return $this->clientsPerFork;
96
    }
97
98
    /**
99
     * @param int $clientsPerFork
100
     * @return ServerConfig
101
     */
102
    public function setClientsPerFork(int $clientsPerFork): ServerConfig
103
    {
104
        $this->clientsPerFork = $clientsPerFork;
105
        return $this;
106
    }
107
108
    /**
109
     * @return null|int
110
     */
111
    public function getStreamSelectTimeout(): ?int
112
    {
113
        return $this->streamSelectTimeout;
114
    }
115
116
    /**
117
     * @param null|int $streamSelectTimeout
118
     * @return ServerConfig
119
     */
120
    public function setStreamSelectTimeout(?int $streamSelectTimeout): ServerConfig
121
    {
122
        $this->streamSelectTimeout = $streamSelectTimeout;
123
        return $this;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getHost(): string
130
    {
131
        return $this->host;
132
    }
133
134
    /**
135
     * @param string $host
136
     * @return ServerConfig
137
     */
138
    public function setHost(string $host): ServerConfig
139
    {
140
        $this->host = $host;
141
        return $this;
142
    }
143
144
    /**
145
     * @return int
146
     */
147
    public function getPort(): int
148
    {
149
        return $this->port;
150
    }
151
152
    /**
153
     * @param int $port
154
     * @return ServerConfig
155
     */
156
    public function setPort(int $port): ServerConfig
157
    {
158
        $this->port = $port;
159
        return $this;
160
    }
161
162
    /**
163
     * @return bool
164
     */
165
    public function isForking(): bool
166
    {
167
        return $this->isForking;
168
    }
169
170
    /**
171
     * @param bool $isForking
172
     * @return ServerConfig
173
     */
174
    public function setForking(bool $isForking): ServerConfig
175
    {
176
        $this->isForking = $isForking;
177
        return $this;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function getProcessName(): string
184
    {
185
        return $this->processName;
186
    }
187
188
    /**
189
     * @param string $processName
190
     */
191
    public function setProcessName(string $processName): ServerConfig
192
    {
193
        $this->processName = $processName;
194
        return $this;
195
    }
196
197
    /**
198
     * @return bool
199
     */
200
    public function isCheckOrigin(): bool
201
    {
202
        return $this->checkOrigin;
203
    }
204
205
    /**
206
     * @param bool $checkOrigin
207
     * @return ServerConfig
208
     */
209
    public function setCheckOrigin(bool $checkOrigin): ServerConfig
210
    {
211
        $this->checkOrigin = $checkOrigin;
212
        return $this;
213
    }
214
215
    /**
216
     * @return array
217
     */
218
    public function getOrigins(): array
219
    {
220
        return $this->origins;
221
    }
222
223
    /**
224
     * @param array $origins
225
     * @return ServerConfig
226
     */
227
    public function setOrigins(array $origins): ServerConfig
228
    {
229
        if (empty($origins) === false) {
230
            $this->setCheckOrigin(true);
231
        }
232
        $this->origins = $origins;
233
        return $this;
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function isOriginHeader(): bool
240
    {
241
        return $this->originHeader;
242
    }
243
244
    /**
245
     * @param bool $originHeader
246
     * @return ServerConfig
247
     */
248
    public function setOriginHeader(bool $originHeader): ServerConfig
249
    {
250
        $this->originHeader = $originHeader;
251
        return $this;
252
    }
253
254
    /**
255
     * @param bool $isSsl
256
     * @return ServerConfig
257
     */
258
    public function setIsSsl(bool $isSsl): ServerConfig
259
    {
260
        $this->isSsl = $isSsl;
261
        return $this;
262
    }
263
264
    /**
265
     * @return bool
266
     */
267
    public function isSsl(): bool
268
    {
269
        return $this->isSsl;
270
    }
271
272
    /**
273
     * @param mixed $localCert
274
     * @return ServerConfig
275
     */
276
    public function setLocalCert(string $localCert): ServerConfig
277
    {
278
        $this->localCert = $localCert;
279
        return $this;
280
    }
281
282
    /**
283
     * @return mixed
284
     */
285
    public function getLocalCert(): string
286
    {
287
        return $this->localCert;
288
    }
289
290
    /**
291
     * @param mixed $localPk
292
     * @return ServerConfig
293
     */
294
    public function setLocalPk(string $localPk): ServerConfig
295
    {
296
        $this->localPk = $localPk;
297
        return $this;
298
    }
299
300
    /**
301
     * @return mixed
302
     */
303
    public function getLocalPk(): string
304
    {
305
        return $this->localPk;
306
    }
307
308
    /**
309
     * @param bool $allowSelfSigned
310
     * @return ServerConfig
311
     */
312
    public function setAllowSelfSigned(bool $allowSelfSigned): ServerConfig
313
    {
314
        $this->allowSelfSigned = $allowSelfSigned;
315
        return $this;
316
    }
317
318
    /**
319
     * @return bool
320
     */
321
    public function getAllowSelfSigned(): bool
322
    {
323
        return $this->allowSelfSigned;
324
    }
325
326
    /**
327
     * @param int $cryptoType
328
     * @return ServerConfig
329
     */
330
    public function setCryptoType(int $cryptoType): ServerConfig
331
    {
332
        $this->cryptoType = $cryptoType;
333
        return $this;
334
    }
335
336
    /**
337
     * @return mixed
338
     */
339
    public function getCryptoType(): int
340
    {
341
        return $this->cryptoType;
342
    }
343
344
    /**
345
     * Set the looping sleep in milliseconds
346
     *
347
     * @return self
348
     */
349
    public function setLoopingDelay(int $loopingDelay): self
350
    {
351
        if (($loopingDelay < $this->loopingDelayRange[0]) || ($loopingDelay > $this->loopingDelayRange[1])) {
352
            throw new Exception('loopingDelay value must be between 1 and 1000 milliseconds.');
353
        }
354
355
        $this->loopingDelay = $loopingDelay;
356
357
        return $this;
358
    }
359
360
    /**
361
     * @return int
362
     */
363
    public function getLoopingDelay(): int
364
    {
365
        return $this->loopingDelay;
366
    }
367
}
368