Completed
Push — master ( 7eaff4...20cf6e )
by Dawid
11s
created

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