Completed
Push — master ( f30e01...d2c2c6 )
by Maxim
02:26
created

Setup::runSteps()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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