Passed
Pull Request — master (#73)
by Keoghan
03:23
created

Open::removeSSLIfNeeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Commands\Ngrok;
4
5
use App\Commands\BaseCommand;
6
use App\Models\Site;
7
use App\Support\Mechanics\Exceptions\UnableToRetrieveIP;
8
9
class Open extends BaseCommand
10
{
11
    /**
12
     * The signature of the command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'ngrok {site?} {--region=eu} {--no-inspection}';
17
18
    /**
19
     * The description of the command.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Open ngrok connection to forward your dev environment to an external url';
24
25
    /**
26
     * Was the site secure at the start of the command?
27
     *
28
     * @var bool
29
     */
30
    protected $wasSecure = false;
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return void
36
     */
37 4
    public function handle(): void
38
    {
39 4
        $site = Site::resolveFromPathOrCurrentWorkingDirectory($this->argument('site'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('site') can also be of type array; however, parameter $path of App\Models\Site::resolve...rrentWorkingDirectory() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $site = Site::resolveFromPathOrCurrentWorkingDirectory(/** @scrutinizer ignore-type */ $this->argument('site'));
Loading history...
40
41 4
        if (!$site) {
42 1
            $this->error('No site at this location, and no site path provided.');
43 1
            return;
44
        }
45
46 3
        if (!$this->checkItWillResolveProperly()) {
47 1
            return;
48
        }
49
50 2
        $this->removeSSLIfNeeded($site);
51
52 2
        $this->porter->stop('ngrok');
53
54 2
        $this->dockerCompose
55 2
            ->runContainer('ngrok')
56 2
            ->append($this->constructNgrokCommand($site))
57 2
            ->interactive()
58 2
            ->perform();
59
60 2
        $this->restoreSSLIfNeeded($site);
61
62 2
        $this->porter->stop('ngrok');
63 2
    }
64
65
    /**
66
     * Checking that Porter is using the dns:set-host IP. If we don't ngrok
67
     * requests will only resolve to 127.0.0.1 which is internal to the
68
     * ngrok container, and results in a useless 502 error.
69
     *
70
     * @return bool
71
     */
72 3
    public function checkItWillResolveProperly()
73
    {
74
        try {
75 3
            if ($this->porterLibrary->getMechanic()->isUsingStandardLoopback()) {
76 1
                $this->info('You need to use an alternative loopback address.');
77 1
                $this->info('Please run porter dns:set-host and review the documentation here: https://github.com/konsulting/porter#dns');
78
79 3
                return false;
80
            }
81
        } catch (UnableToRetrieveIP $e) {
82
            $this->info('Please run porter dns:flush and try again. You may need to give it a little while.');
83
84
            return false;
85
        }
86
87 2
        return true;
88
    }
89
90
    /**
91
     * Remove SSL from the site if it was secured.
92
     *
93
     * @param  Site $site
94
     */
95 2
    protected function removeSSLIfNeeded(Site $site): void
96
    {
97 2
        if (! $site->secure) {
98 1
            return;
99
        }
100
101 1
        $this->info('Removing SSL for site (required for free ngrok version)');
102 1
        $this->wasSecure = true;
103 1
        $site->unsecure();
104 1
    }
105
106
    /**
107
     * Add SSL back to the site if it was previously secured
108
     *
109
     * @param  Site  $site
110
     */
111 2
    protected function restoreSSLIfNeeded(Site $site): void
112
    {
113 2
        if (! $this->wasSecure) {
114 1
            return;
115
        }
116
117 1
        $this->info('Restoring SSL for site');
118 1
        $site->secure();
119 1
    }
120
121
    /**
122
     * Construct the ngrok command
123
     *
124
     * @param  Site  $site
125
     *
126
     * @return string
127
     */
128 2
    protected function constructNgrokCommand(Site $site): string
129
    {
130 2
        $tls = ' -bind-tls='.($this->wasSecure ? 'true' : 'false');
131 2
        $region = ' -region='.$this->option('region');
132 2
        $inspect = ' -inspect='.($this->option('no-inspection') ? 'false' : 'true');
133
134 2
        return "ngrok http -host-header=rewrite{$region}{$tls}{$inspect} {$site->url}:80";
135
    }
136
}
137