Failed Conditions
Pull Request — master (#95)
by Keoghan
05:44
created

Valet::turnOff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 12
cp 0
rs 9.9
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Support\Valet;
4
5
use App\Models\Setting;
6
use App\Models\Site;
7
use App\Porter;
8
use App\Support\Console\Cli;
9
use App\Support\Console\ConsoleWriter;
10
use Illuminate\Support\Facades\Artisan;
11
use Illuminate\Support\Str;
12
13
class Valet
14
{
15
    /** @var Porter */
16
    protected $porter;
17
18
    /** @var Cli */
19
    protected $cli;
20
21
    /** @var ConsoleWriter */
22
    protected $writer;
23
24
    /** @var bool hasWarned */
25
    protected $hasWarned;
26
27
    protected const HTTP_PORT = 80;
28
    protected const HTTPS_PORT = 443;
29
    protected const COMPAT_HTTP_PORT = 8008; // 8080 is needed by the node container at the moment
30
    protected const COMPAT_HTTPS_PORT = 8443;
31
32 193
    public function __construct(Porter $porter, Cli $cli, ConsoleWriter $writer)
33
    {
34 193
        $this->porter = $porter;
35 193
        $this->cli = $cli;
36 193
        $this->writer = $writer;
37 193
    }
38
39
    public function turnOn()
40
    {
41
        $this->sudoWarning();
42
43
        Setting::updateOrCreate('valet', 'on');
44
        Setting::updateOrCreate('http_port', static::COMPAT_HTTP_PORT);
45
        Setting::updateOrCreate('https_port', static::COMPAT_HTTPS_PORT);
46
47
        Artisan::call('dns:off');
48
        $this->cli->execRealTime('valet start');
49
50
        $this->porter->compose();
51
        $this->porter->restart();
52
53
        // Run through all the porter sites and set up a proxy though valet...
54
        Site::all()->each(function (Site $site) {
55
            $this->addSite($site);
56
        });
57
58
        $this->writer->line('Completed setting up valet compatibility');
59
    }
60
61
    public function turnOff()
62
    {
63
        $this->sudoWarning();
64
65
        // Run through all the porter sites and set up a proxy though valet...
66
        Site::all()->each(function (Site $site) {
67
            $this->removeSite($site);
68
        });
69
70
        Setting::updateOrCreate('valet', 'off');
71
        Setting::updateOrCreate('http_port', static::HTTP_PORT);
72
        Setting::updateOrCreate('https_port', static::HTTPS_PORT);
73
74
        $this->cli->execRealTime('valet stop');
75
76
        Artisan::call('dns:on');
77
78
        $this->porter->compose();
79
        $this->porter->restart();
80
81
        $this->writer->line('Completed removing valet compatibility');
82
    }
83
84
    public function addSite(Site $site)
85
    {
86
        $this->sudoWarning();
87
88
        if ($this->isProxied($site)) {
89
            $this->removeSite($site);
90
        }
91
92
        $port = $site->secure ? static::COMPAT_HTTPS_PORT : static::COMPAT_HTTP_PORT;
93
        $protocol = $site->secure ? 'https://' : 'http://';
94
95
        $this->cli->exec("valet proxy {$site->name} {$protocol}127.0.0.1:{$port}");
96
97
        $this->writer->line("Added {$site->name} proxy for Valet");
98
    }
99
100
    public function removeSite(Site $site)
101
    {
102
        $this->sudoWarning();
103
104
        $this->cli->exec("valet unproxy {$site->name}");
105
106
        $this->writer->line("Removed Valet proxy for {$site->name}");
107
    }
108
109
    public function listSites()
110
    {
111
        $this->sudoWarning();
112
113
        return $this->cli->exec('valet proxies');
114
    }
115
116
    public function isProxied(Site $site)
117
    {
118
        return Str::contains($this->listSites(), $site->name);
119
    }
120
121
    public function sudoWarning()
122
    {
123
        if ($this->hasWarned) {
124
            return;
125
        }
126
127
        $this->writer->info('Requires Sudo permissions for Valet');
128
        $this->hasWarned = true;
129
    }
130
}
131