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

CreateUser::getTableByModelClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 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
}