Completed
Push — master ( f63cf3...4d3cf1 )
by Joschi
19:48 queued 19:48
created

Vhost::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * admin
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Tollwerk\Admin\Domain\Vhost
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Tollwerk\Admin\Domain\Vhost;
38
39
use Tollwerk\Admin\Domain\Domain\DomainInterface;
40
41
/**
42
 * Virtual host
43
 *
44
 * @package Apparat\Server
45
 * @subpackage Tollwerk\Admin\Domain
46
 */
47
class Vhost implements VhostInterface
48
{
49
    /**
50
     * Primary domain
51
     *
52
     * @var DomainInterface
53
     */
54
    protected $primaryDomain;
55
    /**
56
     * Secondary domains
57
     *
58
     * @var DomainInterface[]
59
     */
60
    protected $secondaryDomains = [];
61
    /**
62
     * Document root
63
     *
64
     * @var string
65
     */
66
    protected $docroot;
67
    /**
68
     * Virtual host type
69
     *
70
     * @var string
71
     */
72
    protected $type = self::TYPE_APACHE;
73
    /**
74
     * Ports
75
     *
76
     * @var array
77
     */
78
    protected $ports = [];
79
    /**
80
     * Active PHP version
81
     *
82
     * @var null|string
83
     */
84
    protected $php = null;
85
    /**
86
     * Absolute URL to redirect to
87
     *
88
     * @var null|string
89
     */
90
    protected $redirectUrl = null;
91
    /**
92
     * Redirect status code
93
     *
94
     * @var int
95
     */
96
    protected $redirectStatus = 301;
97
    /**
98
     * Default port for HTTP virtual hosts
99
     *
100
     * @var int
101
     */
102
    const PORT_HTTP_DEFAULT = 80;
103
    /**
104
     * Default port for HTTPS virtual hosts
105
     *
106
     * @var int
107
     */
108
    const PORT_HTTPS_DEFAULT = 443;
109
    /**
110
     * HTTP protocol
111
     *
112
     * @var int
113
     */
114
    const PROTOCOL_HTTP = 1;
115
    /**
116
     * HTTPS protocol
117
     *
118
     * @var int
119
     */
120
    const PROTOCOL_HTTPS = 2;
121
    /**
122
     * Apache virtual host
123
     *
124
     * @var string
125
     */
126
    const TYPE_APACHE = 'apache';
127
    /**
128
     * Supported protocols
129
     *
130
     * @var array
131
     */
132
    protected static $supportedProtocols = [
133
        self::PROTOCOL_HTTP => 'http',
134
        self::PROTOCOL_HTTPS => 'https',
135
    ];
136
    /**
137
     * Default protocol ports
138
     *
139
     * @var array
140
     */
141
    protected static $defaultProtocolPorts = [
142
        self::PROTOCOL_HTTP => 80,
143
        self::PROTOCOL_HTTPS => 443,
144
    ];
145
146
    /**
147
     * Virtual host constructor
148
     *
149
     * @param DomainInterface $primaryDomain Primary domain
150
     * @param string $docroot Document root
151
     * @param string $type Virtual host type
152
     * @internal param int $port Port
153
     */
154 10
    public function __construct(DomainInterface $primaryDomain, $docroot, $type = self::TYPE_APACHE)
155
    {
156 10
        $this->primaryDomain = $primaryDomain;
157 10
        $this->docroot = $docroot;
158 10
        $this->type = $type;
159 10
    }
160
161
    /**
162
     * Return the primary domain
163
     *
164
     * @return DomainInterface Primary domain
165
     */
166 2
    public function getPrimaryDomain()
167
    {
168 2
        return $this->primaryDomain;
169
    }
170
171
    /**
172
     * Return the document root
173
     *
174
     * @return string Document root
175
     */
176 1
    public function getDocroot()
177
    {
178 1
        return $this->docroot;
179
    }
180
181
    /**
182
     * Return the virtual host type
183
     *
184
     * @return string Virtual host type
185
     */
186
    public function getType()
187
    {
188
        return $this->type;
189
    }
190
191
    /**
192
     * Return the port
193
     *
194
     * @param int $protocol Protocol
195
     * @return int|null Port
196
     * @throws \RuntimeException If the protocol is unsupported
197
     */
198 3
    public function getPort($protocol)
199
    {
200 3
        $protocol = intval($protocol);
201
202
        // If the protocol is unsupported
203 3
        if (empty(self::$supportedProtocols[$protocol])) {
204 1
            throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081);
205
        }
206
207 2
        return empty($this->ports[$protocol]) ? null : $this->ports[$protocol];
208
    }
209
210
    /**
211
     * Return all supported protocols and corresponding ports
212
     *
213
     * @return array Protocols and ports
214
     */
215 1
    public function getPorts()
216
    {
217 1
        return $this->ports;
218
    }
219
220
    /**
221
     * Return the secondary domains
222
     *
223
     * @return DomainInterface[]
224
     */
225 1
    public function getSecondaryDomains()
226
    {
227 1
        return array_values($this->secondaryDomains);
228
    }
229
230
    /**
231
     * Set the secondary domains
232
     *
233
     * @param DomainInterface[] $secondaryDomains
234
     * @return Vhost Self reference
235
     * @throws \RuntimeException If the domain is invalid
236
     */
237 1
    public function setSecondaryDomains(array $secondaryDomains)
238
    {
239 1
        $this->secondaryDomains = [];
240
        /** @var DomainInterface $secondaryDomain */
241 1
        foreach ($secondaryDomains as $secondaryDomain) {
242
            // If the domain is invalid
243 1
            if (!is_object($secondaryDomain)
244 1
                || !(new \ReflectionClass($secondaryDomain))->implementsInterface(DomainInterface::class)
245
            ) {
246 1
                throw new \RuntimeException(sprintf('Invalid secondary domain "%s"', $secondaryDomain), 1475484852);
247
            }
248 1
            $this->secondaryDomains[strval($secondaryDomain)] = $secondaryDomain;
249
        }
250
251 1
        return $this;
252
    }
253
254
    /**
255
     * Add a secondary domain
256
     *
257
     * @param DomainInterface $secondaryDomain Secondary domain
258
     * @return Vhost Self reference
259
     */
260 1
    public function addSecondaryDomain(DomainInterface $secondaryDomain)
261
    {
262 1
        if (!array_key_exists(strval($secondaryDomain), $this->secondaryDomains)) {
263 1
            $this->secondaryDomains[strval($secondaryDomain)] = $secondaryDomain;
264
        }
265 1
        return $this;
266
    }
267
268
    /**
269
     * Remove a secondary domain
270
     *
271
     * @param DomainInterface $secondaryDomain Secondary domain
272
     * @return Vhost Self reference
273
     */
274 1
    public function removeSecondaryDomain(DomainInterface $secondaryDomain)
275
    {
276 1
        unset($this->secondaryDomains[strval($secondaryDomain)]);
277 1
        return $this;
278
    }
279
280
    /**
281
     * Return the active PHP version
282
     *
283
     * @return null|string Active PHP version
284
     */
285 1
    public function getPhp()
286
    {
287 1
        return $this->php;
288
    }
289
290
    /**
291
     * Set the active PHP version
292
     *
293
     * @param null|string $php Active PHP version
294
     * @return Vhost Self reference
295
     * @throws \RuntimeException If the PHP version is invalid
296
     */
297 1
    public function setPhp($php)
298
    {
299
        // If the PHP version is invalid
300 1
        if (($php !== null) && !preg_match('%^\d\.\d$%', $php)) {
301 1
            throw new \RuntimeException(sprintf('Invalid PHP version "%s"', $php), 1475485163);
302
        }
303
304 1
        $this->php = $php;
305 1
        return $this;
306
    }
307
308
    /**
309
     * Enable a supported protocol
310
     *
311
     * @param int $protocol Protocol
312
     * @param int $port Port
313
     * @return Vhost Self reference
314
     * @throws \RuntimeException If the protocol is unsupported
315
     */
316 3
    public function enableProtocol($protocol, $port = null)
317
    {
318 3
        $protocol = intval($protocol);
319
320
        // If the protocol is unsupported
321 3
        if (empty(self::$supportedProtocols[$protocol])) {
322 1
            throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081);
323
        }
324
325 3
        $port = ($port === null) ? self::$defaultProtocolPorts[$protocol] : intval($port);
326 3
        if ($port <= 0) {
327 1
            throw new \RuntimeException(sprintf('Invalid protocol port "%s"', $port), 1475502412);
328
        }
329
330 2
        $this->ports[$protocol] = $port;
331 2
        return $this;
332
    }
333
334
    /**
335
     * Disable a supported protocol
336
     *
337
     * @param int $protocol Protocol
338
     * @return Vhost Self reference
339
     * @throws \RuntimeException If the protocol is unsupported
340
     */
341 2
    public function disableProtocol($protocol)
342
    {
343 2
        $protocol = intval($protocol);
344
345
        // If the protocol is unsupported
346 2
        if (empty(self::$supportedProtocols[$protocol])) {
347 1
            throw new \RuntimeException(sprintf('Invalid protocol "%s"', $protocol), 1475484081);
348
        }
349
350 1
        unset($this->ports[$protocol]);
351 1
        return $this;
352
    }
353
354
    /**
355
     * Return the redirect URL
356
     *
357
     * @return null|string Redirect URL
358
     */
359 1
    public function getRedirectUrl()
360
    {
361 1
        return $this->redirectUrl;
362
    }
363
364
    /**
365
     * Set the redirect URL
366
     *
367
     * @param null|string $redirectUrl Redirect URL
368
     * @return Vhost Self reference
369
     * @throws \RuntimeException If the redirect URL is invalid
370
     */
371 1
    public function setRedirectUrl($redirectUrl)
372
    {
373 1
        $redirectUrl = trim($redirectUrl) ?: null;
374
375
        // If the redirect URL is invalid
376 1
        if (($redirectUrl !== null) &&
377 1
            (!filter_var($redirectUrl, FILTER_VALIDATE_URL)
378 1
                || !in_array(strtolower(parse_url($redirectUrl, PHP_URL_SCHEME)), self::$supportedProtocols))
379
        ) {
380 1
            throw new \RuntimeException(sprintf('Invalid redirect URL "%s"', $redirectUrl), 1475486589);
381
        }
382
383 1
        $this->redirectUrl = $redirectUrl;
384 1
        return $this;
385
    }
386
387
    /**
388
     * Return the redirect HTTP status code
389
     *
390
     * @return int Redirect HTTP status code
391
     */
392 1
    public function getRedirectStatus()
393
    {
394 1
        return $this->redirectStatus;
395
    }
396
397
    /**
398
     * Set the redirect HTTP status code
399
     *
400
     * @param int $redirectStatus Redirect HTTP status code
401
     * @return Vhost Self reference
402
     */
403 1
    public function setRedirectStatus($redirectStatus)
404
    {
405 1
        if (!is_int($redirectStatus) || (($redirectStatus < 300) || ($redirectStatus > 308))) {
406 1
            throw new \RuntimeException(sprintf('Invalid redirect HTTP status code "%s"', $redirectStatus), 1475486679);
407
        }
408 1
        $this->redirectStatus = $redirectStatus;
409 1
        return $this;
410
    }
411
}
412