BuiltInPhpHttpServer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 48
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A listen() 0 14 1
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Http;
4
5
use Bigwhoop\Trumpet\Exceptions\InvalidArgumentException;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\NullLogger;
8
9
final class BuiltInPhpHttpServer implements HttpServer
10
{
11
    use LoggerAwareTrait;
12
    
13
    /** @var string */
14
    private $host = '';
15
16
    /** @var int */
17
    private $port = 0;
18
19
    /** @var string */
20
    private $routerPath = '';
21
22
    /** @var string */
23
    private $docRoot = '';
24
    
25
    public function __construct(string $host, int $port, string $routerPath, string $docRoot)
26
    {
27
        if (!is_readable($routerPath)) {
28
            throw new InvalidArgumentException("Router script '$routerPath' must be readable.");
29
        }
30
31
        if (!is_readable($docRoot) || !is_dir($docRoot)) {
32
            throw new InvalidArgumentException("Document root '$docRoot' must be a readable directory.");
33
        }
34
35
        $this->host = $host;
36
        $this->port = $port;
37
        $this->routerPath = $routerPath;
38
        $this->docRoot = $docRoot;
39
        $this->logger = new NullLogger();
40
    }
41
42
    public function listen()
43
    {
44
        $cmd = sprintf(
45
            'php -S %s -t %s %s',
46
            escapeshellarg($this->host.':'.$this->port),
47
            escapeshellarg($this->docRoot),
48
            escapeshellarg($this->routerPath)
49
        );
50
            
51
        $this->logger->info(sprintf("Starting webserver on %s:%d ...\n", $this->host, $this->port));
52
        
53
        putenv('foo=bar');
54
        exec($cmd);
55
    }
56
}
57