Failed Conditions
Pull Request — master (#68)
by Keoghan
04:32
created

Open::handle()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 48
ccs 0
cts 30
cp 0
rs 8.1954
c 1
b 0
f 0
cc 8
nc 21
nop 0
crap 72
1
<?php
2
3
namespace App\Commands\Ngrok;
4
5
use App\Porter;
6
use App\Models\Site;
7
use App\Commands\BaseCommand;
8
use App\Support\Mechanics\Mechanic;
9
use App\Support\Mechanics\Exceptions\UnableToRetrieveIP;
10
11
class Open extends BaseCommand
12
{
13
    /**
14
     * The signature of the command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'ngrok {site?} {--region=eu} {--no-inspection}';
19
20
    /**
21
     * The description of the command.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Open ngrok connection to forward your dev environment to an external url';
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return void
31
     */
32
    public function handle(): void
33
    {
34
        $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

34
        $site = Site::resolveFromPathOrCurrentWorkingDirectory(/** @scrutinizer ignore-type */ $this->argument('site'));
Loading history...
35
        $wasSecure = false;
36
37
        if (! $site) {
38
            $this->error('No site at this location, and no site path provided.');
39
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
40
        }
41
42
        // We're now checking that Porter is using the dns:set-host IP. If we don't,
43
        // ngrok requests will only resolve to 127.0.0.1 which is internal
44
        // to the ngrok container, and results in a useless 502 error.
45
        try {
46
            if (app(Mechanic::class)->isUsingDefaultHostAddress()) {
47
                $this->info('You need to use an alternative loopback address.');
48
                $this->info('Please run porter dns:set-host and review the documentation here: https://github.com/konsulting/porter#dns');
49
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
50
            }
51
        } catch (UnableToRetrieveIP $e) {
52
            $this->info('Please run porter dns:flush and try again. You may need to give it a little while.');
53
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
54
        }
55
56
        if ($site->secure) {
57
            $this->info('Removing SSL for site (required for free ngrok version)');
58
            $site->unsecure();
59
            $wasSecure = true;
60
        }
61
62
        app(Porter::class)->stop('ngrok');
63
64
        $tls = ' -bind-tls='.($wasSecure ? 'true' : 'false');
65
        $region = ' -region='. $this->option('region');
66
        $inspect = ' -inspect='.($this->option('no-inspection') ? 'false' : 'true');
67
68
        $this->dockerCompose
69
            ->runContainer('ngrok')
70
            ->append("ngrok http -host-header=rewrite{$region}{$tls}{$inspect} {$site->url}:80")
71
            ->interactive()
72
            ->perform();
73
74
        if ($wasSecure) {
75
            $this->info('Restoring SSL for site');
76
            $site->secure();
77
        }
78
79
        app(Porter::class)->stop('ngrok');
80
    }
81
}
82