Test Failed
Pull Request — master (#95)
by Keoghan
06:04
created

Valet::turnOn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 9.7998
cc 2
nc 2
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 205
    public function __construct(Porter $porter, Cli $cli, ConsoleWriter $writer)
33
    {
34 205
        $this->porter = $porter;
35 205
        $this->cli = $cli;
36 205
        $this->writer = $writer;
37 205
    }
38
39 2
    public function turnOn()
40
    {
41 2
        if (setting('use_valet', 'off') === 'on') {
42 1
            $this->writer->info('Valet compatibility already complete');
43 1
            return;
44
        }
45
46 1
        $this->sudoWarning();
47
48 1
        Setting::updateOrCreate('valet', 'on');
49 1
        Setting::updateOrCreate('http_port', static::COMPAT_HTTP_PORT);
50 1
        Setting::updateOrCreate('https_port', static::COMPAT_HTTPS_PORT);
51
52 1
        Artisan::call('dns:off');
53 1
        $this->cli->execRealTime('valet start');
54
55 1
        $this->porter->compose();
56 1
        $this->porter->restart();
57
58
        // Run through all the porter sites and set up a proxy though valet...
59
        Site::all()->each(function (Site $site) {
60 1
            $this->addSite($site);
61 1
        });
62
63 1
        $this->writer->line('Completed setting up valet compatibility');
64 1
    }
65
66 2
    public function turnOff()
67
    {
68 2
        if (setting('use_valet', 'off') === 'off') {
69 2
            $this->writer->info('Valet compatibility already off');
70 1
            return;
71
        }
72
73
        $this->sudoWarning();
74
75
        // Run through all the porter sites and set up a proxy though valet...
76
        Site::all()->each(function (Site $site) {
77
            $this->removeSite($site);
78
        });
79
80
        Setting::updateOrCreate('valet', 'off');
81
        Setting::updateOrCreate('http_port', static::HTTP_PORT);
82
        Setting::updateOrCreate('https_port', static::HTTPS_PORT);
83
84
        $this->cli->execRealTime('valet stop');
85
        $this->cli->execRealTime('sudo brew services stop dnsmasq');
86
87
        Artisan::call('dns:on');
88
89
        $this->porter->compose();
90
        $this->porter->restart();
91
92
        $this->writer->line('Completed removing valet compatibility');
93
    }
94
95 2
    public function addSite(Site $site)
96
    {
97 2
        $this->sudoWarning();
98
99 2
        if ($this->isProxied($site)) {
100 1
            $this->removeSite($site);
101
        }
102
103 2
        $port = $site->secure ? static::COMPAT_HTTPS_PORT : static::COMPAT_HTTP_PORT;
104 2
        $protocol = $site->secure ? 'https://' : 'http://';
105
106 2
        $this->cli->exec("valet proxy {$site->name} {$protocol}127.0.0.1:{$port}");
107
108 2
        $this->writer->line("Added {$site->name} proxy for Valet");
109 2
    }
110
111 2
    public function removeSite(Site $site)
112
    {
113 2
        $this->sudoWarning();
114
115 2
        $this->cli->exec("valet unproxy {$site->name}");
116
117 2
        $this->writer->line("Removed Valet proxy for {$site->name}");
118 2
    }
119
120 4
    public function listSites()
121
    {
122 4
        $this->sudoWarning();
123
124 4
        return $this->cli->exec('valet proxies');
125
    }
126
127 3
    public function isProxied(Site $site)
128
    {
129 3
        return Str::contains($this->listSites(), $site->name);
130
    }
131
132 5
    protected function sudoWarning()
133
    {
134 5
        if ($this->hasWarned) {
135 3
            return;
136
        }
137
138 5
        $this->writer->info('Requires Sudo permissions for Valet');
139 5
        $this->hasWarned = true;
140 5
    }
141
}
142