AbstractStep   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 9
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 118
ccs 28
cts 28
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B run() 0 22 4
A repeat() 0 16 3
A arrayToTable() 0 6 1
prompt() 0 1 ?
prepare() 0 1 ?
preview() 0 1 ?
finish() 0 1 ?
1
<?php namespace Lanin\Laravel\SetupWizard\Commands\Steps;
2
3
use Illuminate\Console\Command;
4
5
abstract class AbstractStep
6
{
7
    const REPEATS = 3;
8
9
    /**
10
     * @var int
11
     */
12
    protected $repeats = 1;
13
14
    /**
15
     * @var Command
16
     */
17
    protected $command;
18
19
    /**
20
     * Create a new Step.
21
     *
22
     * @param Command $command
23
     */
24 102
    public function __construct(Command $command)
25
    {
26 102
        $this->command = $command;
27 102
    }
28
29
    /**
30
     * Run step.
31
     *
32
     * @param  bool $pretend
33
     * @return mixed
34
     */
35 10
    public function run($pretend = false)
36
    {
37 10
        $results = $this->prepare();
38
39 10
        $this->preview($results);
40
41 10
        if ($this->command->confirm('Everything is right?'))
42 10
        {
43 8
            $return = $pretend ? true : $this->finish($results);
44
45 8
            if ($return === false)
46 8
            {
47 2
                $return = $this->repeat();
48 2
            }
49 8
        }
50
        else
51
        {
52 2
            $return = $this->repeat();
53
        }
54
55 10
        return $return;
56
    }
57
58
    /**
59
     * Repeat step.
60
     *
61
     * @return bool
62
     */
63 6
    public function repeat()
64
    {
65 6
        if ($this->repeats > self::REPEATS)
66 6
        {
67 2
            return false;
68
        }
69
70 6
        if ($this->command->confirm('Do you want to repeat step?'))
71 6
        {
72 2
            $this->repeats++;
73
74 2
            return $this->run();
75
        }
76
77 4
        return false;
78
    }
79
80
    /**
81
     * Transform associative array to use in command->table() method.
82
     *
83
     * @param  array $array
84
     * @return array
85
     */
86 6
    protected function arrayToTable(array $array)
87
    {
88 6
        list($keys, $values) = array_divide($array);
89
90 6
        return collect($keys)->zip(collect($values))->toArray();
91
    }
92
93
    /**
94
     * Return command prompt text.
95
     *
96
     * @return string
97
     */
98
    abstract public function prompt();
99
100
    /**
101
     * Prepare step data.
102
     *
103
     * @return mixed
104
     */
105
    abstract public function prepare();
106
107
    /**
108
     * Preview results.
109
     *
110
     * @param  mixed $results
111
     * @return void
112
     */
113
    abstract public function preview($results);
114
115
    /**
116
     * Finish step.
117
     *
118
     * @param  mixed $results
119
     * @return bool
120
     */
121
    abstract public function finish($results);
122
}
123