BuiltinServer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A getSocketClient() 0 8 2
1
<?php
2
3
namespace mpyw\HyperBuiltinServer\Internal;
4
use React\ChildProcess\Process;
5
6
class BuiltinServer extends Process
7
{
8
    protected $host;
9
    protected $port;
10
11
    public function __construct($host = '127.0.0.1', $docroot = null, $router = null, $php = PHP_BINARY)
12
    {
13
        $port = mt_rand(49152, 65535);
14
        $command = implode(' ', array_filter([
15
            escapeshellarg($php),
16
            '-S',
17
            escapeshellarg("$host:$port"),
18
            $docroot !== null ? ('-t ' . escapeshellarg($docroot)) : null,
19
            $router !== null ? escapeshellarg($router) : null,
20
        ], 'is_string'));
21
        parent::__construct($command, null, null, ['bypass_shell' => true]);
22
        $this->host = $host;
23
        $this->port = $port;
24
    }
25
26
    public function getSocketClient()
27
    {
28
        $socket = @stream_socket_client("tcp://{$this->host}:{$this->port}");
29
        if ($socket === false) {
30
            throw new \RuntimeException(error_get_last()['message']);
31
        }
32
        return $socket;
33
    }
34
}
35