Setup::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace Lanin\Laravel\SetupWizard\Commands;
2
3
use Illuminate\Console\Command;
4
use Lanin\Laravel\SetupWizard\Commands\Steps\AbstractStep;
5
6
class Setup extends Command
7
{
8
    const VERSION = '0.1.4';
9
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'app:setup
16
                            {step? : Step to run}
17
                            {--P|pretend : Pretend to execute}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Setup the project.';
25
26
    /**
27
     * Installation steps.
28
     *
29
     * @var array
30
     */
31
    public $steps = [];
32
33
    /**
34
     * @inheritDoc
35
     */
36 22
    public function __construct()
37
    {
38 22
        parent::__construct();
39
40 22
        $this->steps = config('setup.steps');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('setup.steps') of type * is incompatible with the declared type array of property $steps.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 22
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 2
    public function getHelp()
47
    {
48 2
        $help = $this->description . ' Available steps:' . PHP_EOL;
49
50 2
        foreach ($this->steps as $alias => $class)
51
        {
52 2
            $help .= '  - <comment>' . $alias . '</comment>' . PHP_EOL;
53 2
        }
54
55 2
        return $help;
56
    }
57
58
    /**
59
     * Execute the console command.
60
     *
61
     * @return mixed
62
     */
63 4
    public function handle()
64
    {
65 4
        $this->info(sprintf('%s (v%s)', config('setup.title'), self::VERSION));
66
67 4
        $pretend = (bool) $this->option('pretend');
68 4
        $steps   = (array) $this->argument('step');
69 4
        $return  = $this->runSteps($steps, $pretend);
70
71 4
        $this->info('Setup finished.');
72
73 4
        return $return;
74
    }
75
76
    /**
77
     * Run all steps one by one.
78
     *
79
     * @param  array $steps
80
     * @param  bool $pretend
81
     * @return bool
82
     */
83 10
    protected function runSteps($steps = [], $pretend = false)
84
    {
85 10
        $return = true;
86 10
        $steps  = empty($steps) ? array_keys($this->steps) : $steps;
87
88 10
        foreach ($steps as $step)
89
        {
90 8
            if ($this->runStep($step, $pretend) === false)
91 8
            {
92 2
                $return = false;
93 2
            }
94 10
        }
95
96 10
        return $return;
97
    }
98
99
    /**
100
     * Run installation step
101
     *
102
     * @param  string $step
103
     * @param  bool $pretend
104
     * @return bool
105
     */
106 4
    protected function runStep($step, $pretend = false)
107
    {
108
        try
109
        {
110 4
            $step = $this->createStep($step);
111
112 2
            if ($this->confirm($step->prompt(), true))
113 2
            {
114 2
                return $step->run($pretend);
115
            }
116
        }
117 2
        catch (\Exception $e)
118
        {
119 2
            $this->error($e->getMessage());
120
        }
121
122 2
        return false;
123
    }
124
125
    /**
126
     * Instantiate step class.
127
     *
128
     * @param string $step
129
     * @return AbstractStep
130
     * @throws \Exception
131
     */
132 10
    protected function createStep($step)
133
    {
134 10
        if ( ! $this->stepExist($step))
135 10
        {
136 4
            throw new \Exception("Step <comment>{$step}</comment> doesn't exist.");
137
        }
138
139 6
        $class = $this->steps[$step];
140 6
        $step  = new $class($this);
141
142 6
        if ( ! ($step instanceof AbstractStep))
143 6
        {
144 2
            throw new \Exception("Step class <comment>{$class}</comment> should be an instance of AbstractStep.");
145
        }
146
147 4
        return $step;
148
    }
149
150
    /**
151
     * Check if step exists.
152
     *
153
     * @param  string $step
154
     * @return bool
155
     */
156 12
    protected function stepExist($step)
157
    {
158 12
        return isset($this->steps[$step]) && class_exists($this->steps[$step]);
159
    }
160
161
}
162