BaseCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace App\Commands;
4
5
use App\Exceptions\PorterNotSetUp;
6
use App\Porter;
7
use App\PorterLibrary;
8
use App\Support\Console\Cli;
9
use App\Support\Console\DockerCompose\CliCommandFactory;
10
use LaravelZero\Framework\Commands\Command;
11
use NunoMaduro\LaravelConsoleMenu\Menu;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * @method Menu menu(string $title, array $options)
17
 */
18
abstract class BaseCommand extends Command
19
{
20
    /** @var \App\Support\Console\Cli */
21
    protected $cli;
22
23
    /** @var CliCommandFactory */
24
    protected $dockerCompose;
25
26
    /** @var Porter */
27
    protected $porter;
28
29
    /** @var PorterLibrary */
30
    protected $porterLibrary;
31
32
    /**
33
     * If true, the command will not run unless the setup process has been run.
34
     *
35
     * @var bool
36
     */
37
    protected $porterMustBeSetUp = true;
38
39 210
    public function __construct(
40
        Cli $cli,
41
        CliCommandFactory $dockerCompose,
42
        Porter $porter,
43
        PorterLibrary $porterLibrary
44
    ) {
45 210
        parent::__construct();
46
47 210
        $this->cli = $cli;
48 210
        $this->dockerCompose = $dockerCompose;
49 210
        $this->porter = $porter;
50 210
        $this->porterLibrary = $porterLibrary;
51
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @param \Symfony\Component\Console\Input\InputInterface   $input
57
     * @param \Symfony\Component\Console\Output\OutputInterface $output
58
     *
59
     * @throws PorterNotSetUp
60
     *
61
     * @return mixed
62
     */
63 45
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65 45
        $this->checkPorterIsSetUp();
66
67 44
        return parent::execute($input, $output);
68
    }
69
70
    /**
71
     * Ensure that Porter has been set up if necessary before continuing.
72
     *
73
     * @throws PorterNotSetUp
74
     */
75 45
    private function checkPorterIsSetUp()
76
    {
77 45
        if ($this->porterMustBeSetUp && !$this->porterLibrary->alreadySetUp()) {
78 1
            throw new PorterNotSetUp('Porter must be set up to run this command. Run \'porter begin\' first.');
79
        }
80
    }
81
}
82