AdministratorCreateCommand::dataWithId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Console;
4
5
use Exception;
6
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Contracts\Hashing\Hasher;
8
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Terranet\Administrator\Traits\SessionGuardHelper;
10
11
class AdministratorCreateCommand extends Command
12
{
13
    use SessionGuardHelper;
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'administrator:create';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create new administrator user.';
28
29
    /**
30
     * @var Hasher
31
     */
32
    private $hasher;
33
34
    /**
35
     * Create a new command instance.
36
     *
37
     * @param Hasher $hasher
38
     */
39
    public function __construct(Hasher $hasher)
40
    {
41
        parent::__construct();
42
43
        $this->hasher = $hasher;
44
    }
45
46
    /**
47
     * Execute the console command.
48
     */
49
    public function handle()
50
    {
51
        $name = $this->ask('Name', 'Administrator');
52
        $email = $this->ask('Email', '[email protected]');
53
        $password = $this->ask('Password', 'secret');
54
55
        $data = $this->prepareData($name, $email, $password);
56
57
        $this->tryUpdatingRole($this->createUserInstance($data));
58
    }
59
60
    /**
61
     * Prepare administrator's data.
62
     *
63
     * @param $name
64
     * @param $email
65
     * @param $password
66
     *
67
     * @return array
68
     */
69
    protected function prepareData($name, $email, $password)
70
    {
71
        $data = compact('name', 'email', 'password');
72
73
        if (app('scaffold.config')->get('manage_passwords')) {
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        if (/** @scrutinizer ignore-call */ app('scaffold.config')->get('manage_passwords')) {
Loading history...
74
            $data['password'] = $this->hasher->make($data['password']);
75
        }
76
77
        return $data;
78
    }
79
80
    /**
81
     * Try to update user's role to admin if column 'role' exists.
82
     *
83
     * @param $instance
84
     *
85
     * @return bool
86
     */
87
    protected function tryUpdatingRole($instance)
88
    {
89
        try {
90
            return $instance->forceFill(['role' => 'admin'])->save();
91
        } catch (Exception $e) {
92
            return false;
93
        }
94
    }
95
96
    /**
97
     * Insert new user into database.
98
     *
99
     * @param $data
100
     *
101
     * @throws Exception
102
     *
103
     * @return Model
104
     */
105
    protected function createUserInstance($data)
106
    {
107
        $config = app('scaffold.config');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $config = /** @scrutinizer ignore-call */ app('scaffold.config');
Loading history...
108
109
        if (!$model = $this->fetchModel($config)) {
110
            throw new Exception('Could not find a model to create user.');
111
        }
112
113
        try {
114
            $model::unguard();
115
116
            return (new $model())->create($this->dataWithId($data));
117
        } catch (Exception $e) {
118
            return (new $model())->create($data);
119
        }
120
    }
121
122
    /**
123
     * Merge Administrator's data with an ID.
124
     *
125
     * @param $data
126
     *
127
     * @return array
128
     */
129
    protected function dataWithId($data)
130
    {
131
        return array_merge(['id' => 1], $data);
132
    }
133
}
134