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

CreateUser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 100
ccs 39
cts 39
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A prompt() 0 4 1
A prepare() 0 17 3
A preview() 0 5 1
A finish() 0 7 1
A getTable() 0 18 4
A getTableByModelClass() 0 7 1
1
<?php namespace Lanin\Laravel\SetupWizard\Commands\Steps;
2
3
class CreateUser extends AbstractStep
4
{
5
    /**
6
     * Return command prompt text.
7
     *
8
     * @return string
9
     */
10 3
    public function prompt()
11
    {
12 3
        return 'Create new user?';
13
    }
14
15
    /**
16
     * Prepare step data.
17
     *
18
     * @return mixed
19
     */
20 3
    public function prepare()
21
    {
22 3
        $result = [];
23 3
        foreach (config('setup.create_user.fields') as $column => $title)
24
        {
25 3
            $result[$column] = $this->command->ask($title);
26
27 3
            if ($column == config('setup.create_user.password_field'))
28 3
            {
29 3
                $result[$column] = bcrypt($result[$column]);
30 3
            }
31 3
        }
32
33 3
        $result['__table'] = $this->getTable(config('setup.create_user.table'));
34
35 3
        return $result;
36
    }
37
38
    /**
39
     * Preview results.
40
     *
41
     * @param  mixed $results
42
     * @return void
43
     */
44 3
    public function preview($results)
45
    {
46 3
        $this->command->info('I will insert this values into <comment>' . $results['__table'] . '</comment> table');
47 3
        $this->command->table(['column', 'value'], $this->arrayToTable($results));
48 3
    }
49
50
    /**
51
     * Finish step.
52
     *
53
     * @param  mixed $results
54
     * @return bool
55
     */
56 3
    public function finish($results)
57
    {
58 3
        $table = $results['__table'];
59 3
        unset($results['__table']);
60
61 3
        return \DB::table($table)->insert($results);
62
    }
63
64
    /**
65
     * Get users table name.
66
     *
67
     * @param  string $table
68
     * @return string
69
     */
70 9
    protected function getTable($table = '')
71
    {
72 9
        if (empty($table))
73 9
        {
74 6
            switch (config('auth.driver'))
75
            {
76 6
                case 'eloquent':
77 3
                    $table = $this->getTableByModelClass(config('auth.model'));
78 3
                    break;
79 3
                case 'database':
80 3
                default:
81 3
                    $table = config('auth.table');
82 3
                    break;
83 6
            }
84 6
        }
85
86 9
        return $table;
87
    }
88
89
    /**
90
     * Resolve user's model and get associated table.
91
     *
92
     * @param  string $model
93
     * @return string
94
     */
95 3
    protected function getTableByModelClass($model)
96
    {
97 3
        $model = new $model();
98 3
        $table = $model->getTable();
99
100 3
        return $table;
101
    }
102
}