Completed
Push — master ( 9271e2...253fe0 )
by Maxim
04:13
created

AbstractStep::arrayToTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 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 78
    public function __construct(Command $command)
25
    {
26 78
        $this->command = $command;
27 78
    }
28
29
    /**
30
     * Run step.
31
     *
32
     * @param  bool $pretend
33
     * @return mixed
34
     */
35 15
    public function run($pretend = false)
36
    {
37 15
        $results = $this->prepare();
38
39 15
        $this->preview($results);
40
41 15
        if ($this->command->confirm('Everything is right?'))
42 15
        {
43 12
            $return = $pretend ? true : $this->finish($results);
44
45 12
            if ($return === false)
46 12
            {
47 3
                $return = $this->repeat();
48 3
            }
49 12
        }
50
        else
51
        {
52 3
            $return = $this->repeat();
53
        }
54
55 15
        return $return;
56
    }
57
58
    /**
59
     * Repeat step.
60
     *
61
     * @return bool
62
     */
63 9
    public function repeat()
64
    {
65 9
        if ($this->repeats > self::REPEATS)
66 9
        {
67 3
            return false;
68
        }
69
70 9
        if ($this->command->confirm('Do you want to repeat step?'))
71 9
        {
72 3
            $this->repeats++;
73
74 3
            return $this->run();
75
        }
76
77 6
        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