CreateUser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

6 Methods

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