Passed
Push — master ( 3ee5ba...23bbf0 )
by Keoghan
04:26
created

Open::handle()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 39
ccs 25
cts 25
cp 1
rs 8.6026
c 1
b 0
f 0
cc 7
nc 18
nop 0
crap 7
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
     * Execute the console command.
27
     *
28
     * @return void
29
     */
30 4
    public function handle(): void
31
    {
32 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

32
        $site = Site::resolveFromPathOrCurrentWorkingDirectory(/** @scrutinizer ignore-type */ $this->argument('site'));
Loading history...
33 4
        $wasSecure = false;
34
35 4
        if (!$site) {
36 1
            $this->error('No site at this location, and no site path provided.');
37
38 1
            return;
39
        }
40
41 3
        if (!$this->checkItWillResolveProperly()) {
42 1
            return;
43
        }
44
45 2
        if ($site->secure) {
46 1
            $this->info('Removing SSL for site (required for free ngrok version)');
47 1
            $site->unsecure();
48 1
            $wasSecure = true;
49
        }
50
51 2
        $this->porter->stop('ngrok');
52
53 2
        $tls = ' -bind-tls='.($wasSecure ? 'true' : 'false');
54 2
        $region = ' -region='.$this->option('region');
55 2
        $inspect = ' -inspect='.($this->option('no-inspection') ? 'false' : 'true');
56
57 2
        $this->dockerCompose
58 2
            ->runContainer('ngrok')
59 2
            ->append("ngrok http -host-header=rewrite{$region}{$tls}{$inspect} {$site->url}:80")
60 2
            ->interactive()
61 2
            ->perform();
62
63 2
        if ($wasSecure) {
64 1
            $this->info('Restoring SSL for site');
65 1
            $site->secure();
66
        }
67
68 2
        $this->porter->stop('ngrok');
69 2
    }
70
71
    /**
72
     * Checking that Porter is using the dns:set-host IP. If we don't ngrok
73
     * requests will only resolve to 127.0.0.1 which is internal to the
74
     * ngrok container, and results in a useless 502 error.
75
     *
76
     * @return bool
77
     */
78 3
    public function checkItWillResolveProperly()
79
    {
80
        try {
81 3
            if ($this->porterLibrary->getMechanic()->isUsingStandardLoopback()) {
82 1
                $this->info('You need to use an alternative loopback address.');
83 1
                $this->info('Please run porter dns:set-host and review the documentation here: https://github.com/konsulting/porter#dns');
84
85 3
                return false;
86
            }
87
        } catch (UnableToRetrieveIP $e) {
88
            $this->info('Please run porter dns:flush and try again. You may need to give it a little while.');
89
90
            return false;
91
        }
92
93 2
        return true;
94
    }
95
}
96