Passed
Push — master ( f44c53...21c685 )
by Caen
03:57 queued 12s
created

ServeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 40
rs 10
c 2
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPortSelection() 0 3 2
A getExecutablePath() 0 3 1
A handle() 0 11 3
A runServerProcess() 0 4 1
A getHostSelection() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Illuminate\Support\Facades\Process;
8
use function config;
9
use Hyde\Hyde;
10
use LaravelZero\Framework\Commands\Command;
11
use function sprintf;
12
13
/**
14
 * Start the realtime compiler server.
15
 *
16
 * @see https://github.com/hydephp/realtime-compiler
17
 */
18
class ServeCommand extends Command
19
{
20
    /** @var string */
21
    protected $signature = 'serve {--host= : <comment>[default: "localhost"]</comment>}} {--port= : <comment>[default: 8080]</comment>}';
22
23
    /** @var string */
24
    protected $description = 'Start the realtime compiler server.';
25
26
    public function handle(): int
27
    {
28
        $this->line('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop');
29
30
        $this->runServerProcess(sprintf('php -S %s:%d %s',
31
            $this->getHostSelection() ?: 'localhost',
32
            $this->getPortSelection() ?: 8080,
33
            $this->getExecutablePath()
34
        ));
35
36
        return Command::SUCCESS;
37
    }
38
39
    protected function getPortSelection(): int
40
    {
41
        return (int) ($this->option('port') ?: config('hyde.server.port', 8080));
42
    }
43
44
    protected function getHostSelection(): string
45
    {
46
        return $this->option('host') ?: config('hyde.server.host', 'localhost');
47
    }
48
49
    protected function getExecutablePath(): string
50
    {
51
        return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php');
52
    }
53
54
    protected function runServerProcess(string $command): void
55
    {
56
        Process::run($command, function ($type, $line) {
57
            $this->output->write($line);
58
        });
59
    }
60
}
61