Begin::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0393

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 22
ccs 11
cts 14
cp 0.7856
rs 9.7998
cc 2
nc 2
nop 0
crap 2.0393
1
<?php
2
3
namespace App\Commands;
4
5
use App\Exceptions\PorterSetupFailed;
6
use App\Support\Images\Organiser\Organiser as ImageOrganiser;
7
8
class Begin extends BaseCommand
9
{
10
    /**
11
     * The begin command may be run at any time, since it's needed to perform setup.
12
     *
13
     * @var bool
14
     */
15
    protected $porterMustBeSetUp = false;
16
17
    /**
18
     * The signature of the command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'begin 
23
        {home? : The path of Porter\'s home directory (optional). Defaults to current directory.} 
24
        {--force : Force setup to run, even if it has already been run}';
25
26
    /**
27
     * The description of the command.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Run initial seeders and set up';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @throws \Exception
37
     *
38
     * @return void
39
     */
40 2
    public function handle(): void
41
    {
42 2
        $this->line('================');
43 2
        $this->line('PREPARING PORTER');
44 2
        $this->line('================');
45 2
        $this->line('');
46
47
        try {
48 2
            $this->porterLibrary->setup(/* @scrutinizer ignore-type */$this->app, (bool) $this->option('force'));
49
        } catch (PorterSetupFailed $e) {
50
            $this->alert($e->getMessage());
51
52
            return;
53
        }
54
55 2
        $this->info('Your Porter settings are stored in '.$this->porterLibrary->path());
56 2
        $this->info('');
57
58 2
        $this->setHomeDirectory();
59
60 2
        $this->info('Retrieving docker images');
61 2
        app(ImageOrganiser::class)->pullImages();
62
    }
63
64
    /**
65
     * Set the Porter home directory, prompting for input if necessary.
66
     *
67
     * @return void
68
     */
69 2
    protected function setHomeDirectory()
70
    {
71 2
        if ($this->argument('home') || $this->option('no-interaction')) {
72 2
            $home = realpath((string) $this->argument('home') ?: $this->cli->currentWorkingDirectory());
73
        } else {
74
            $this->comment('Please enter the root directory for your sites, or leave blank to use the current directory.');
75
            $home = $this->ask('', $this->cli->currentWorkingDirectory());
76
        }
77
78 2
        $this->info("Setting home to {$home}.");
79 2
        $this->comment('This is the root directory for your sites.');
80 2
        $this->comment("If this is incorrect, you can change it using the 'porter home' command.");
81 2
        $this->comment('');
82
83 2
        $this->callSilent('home', ['path' => $home]);
84
    }
85
}
86