Completed
Pull Request — master (#25)
by Dawid
03:13
created

HttpConfiguration::getSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Http\Server;
4
5
/**
6
 * Class HttpConfiguration
7
 * @package Igni\Http\Server
8
 */
9
class HttpConfiguration
10
{
11
    public const DISPATCH_ROUND_ROBIN = 1;
12
    public const DISPATCH_MODULO = 2;
13
    public const DISPATCH_PREEMPTIVE_ASSIGNMENT = 3;
14
    public const DEFAULT_ADDRESS = '0.0.0.0';
15
    public const DEFAULT_PORT = 80;
16
17
    public const LOG_DEBUG = 0;
18
    public const LOG_TRACE = 1;
19
    public const LOG_INFO = 2;
20
    public const LOG_NOTICE = 3;
21
    public const LOG_WARNING = 4;
22
    public const LOG_ERROR = 5;
23
24
25
    /**
26
     * @var array
27
     */
28
    private $settings = [];
29
30
    /**
31
     * HttpConfiguration constructor.
32
     *
33
     * @param string $address
34
     * @param int $port
35
     */
36 9
    public function __construct(string $address = self::DEFAULT_ADDRESS, int $port = self::DEFAULT_PORT)
37
    {
38 9
        $this->settings['address'] = $address;
39 9
        $this->settings['port'] = $port;
40 9
    }
41
42
    /**
43
     * Checks if ssl is enabled.
44
     *
45
     * @return bool
46
     */
47 3
    public function isSslEnabled(): bool
48
    {
49 3
        return isset($this->settings['ssl_cert_file']);
50
    }
51
52
    /**
53
     * Enables ssl on the server
54
     *
55
     * @param string $certFile
56
     * @param string $keyFile
57
     */
58 2
    public function enableSsl(string $certFile, string $keyFile): void
59
    {
60
        $this->settings += [
61 2
            'ssl_cert_file' => $certFile,
62 2
            'ssl_key_file' => $keyFile,
63
        ];
64 2
    }
65
66
    /**
67
     * Checks if server is daemonized.
68
     *
69
     * @return bool
70
     */
71 1
    public function isDaemonEnabled(): bool
72
    {
73 1
        return isset($this->settings['daemonize']) && $this->settings['daemonize'];
74
    }
75
76
    /**
77
     * Sets the max tcp connection number of the server.
78
     *
79
     * @param int $max
80
     */
81 1
    public function setMaxConnections(int $max = 10000): void
82
    {
83 1
        $this->settings['max_conn'] = $max;
84 1
    }
85
86
    /**
87
     * Sets the number of worker processes.
88
     *
89
     * @param int $count
90
     */
91 1
    public function setWorkers(int $count = 1): void
92
    {
93 1
        $this->settings['worker_num'] = $count;
94 1
    }
95
96
    /**
97
     * Sets the number of requests processed by the worker process before been recycled.
98
     *
99
     * @param int $max
100
     */
101
    public function setMaxRequests(int $max = 0): void
102
    {
103
        $this->settings['max_request'] = $max;
104
    }
105
106
    /**
107
     * Sets path to the file that will be used to persist server log.
108
     * Log level can also be set as an additional parameter.
109
     *
110
     * @param string $filename
111
     * @param int $logLevel
112
     */
113 1
    public function setLogFile(string $filename, int $logLevel = self::LOG_TRACE): void
114
    {
115 1
        $this->settings['log_file'] = $filename;
116 1
        $this->settings['log_level'] = $logLevel;
117 1
    }
118
119
    /**
120
     * Sets the maximum number of pending connections. This refers to the number of clients
121
     * that can be waiting to be served. Exceeding this number results in the client getting
122
     * an error when attempting to connect.
123
     *
124
     * @param int $max
125
     */
126 1
    public function setMaximumBacklog(int $max = 0): void
127
    {
128 1
        $this->settings['backlog'] = $max;
129 1
    }
130
131
    /**
132
     * Sets dispatch mode for child processes.
133
     *
134
     * @param int $mode
135
     */
136 1
    public function setDispatchMode(int $mode = self::DISPATCH_ROUND_ROBIN): void
137
    {
138 1
        $this->settings['dispatch_mode'] = $mode;
139 1
    }
140
141
    /**
142
     * Allows server to be run as a background process.
143
     *
144
     * @param string $pid
145
     */
146 1
    public function enableDaemon(string $pid): void
147
    {
148
        $this->settings += [
149 1
            'daemonize' => true,
150 1
            'pid_file' => $pid,
151
        ];
152 1
    }
153
154
    /**
155
     * Sets temporary dir for uploaded files
156
     *
157
     * @param string $dir
158
     */
159 1
    public function setUploadDir(string $dir): void
160
    {
161 1
        $this->settings['upload_tmp_dir'] = $dir;
162 1
    }
163
164
    /**
165
     * Returns swoole compatible settings array.
166
     *
167
     * @return array
168
     */
169 4
    public function getSettings(): array
170
    {
171 4
        return $this->settings;
172
    }
173
}
174