Completed
Push — master ( 8927df...f70445 )
by Abdelrahman
02:05
created

UserUpdateCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 0 57 10
A filter() 0 4 2
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Console\Commands;
17
18
use Illuminate\Console\Command;
19
use Illuminate\Support\Facades\Lang;
20
use Illuminate\Contracts\Validation\Factory;
21
22
class UserUpdateCommand extends Command
23
{
24
    /**
25
     * The name and signature of the console command.
26
     *
27
     * @var string
28
     */
29
    protected $signature = 'fort:user:update
30
                            {field? : The identifier of the user (id, email, username)}
31
                            {--E|email= : The Email of the user}
32
                            {--U|username= : The username of the user}
33
                            {--P|password= : The password of the user}
34
                            {--F|firstName= : The first name of the user}
35
                            {--M|middleName= : The middle name of the user}
36
                            {--L|lastName= : The last name of the user}
37
                            {--A|active : The active statuse of the user}
38
                            {--I|inactive : Set the user as inactive}';
39
40
    /**
41
     * The console command description.
42
     *
43
     * @var string
44
     */
45
    protected $description = 'Update an existing user.';
46
47
    /**
48
     * Execute the console command.
49
     *
50
     * @return void
51
     */
52
    public function handle()
53
    {
54
        $data = array_filter([
55
56
            // Required user attributes
57
            'email'       => $this->option('email'),
58
            'username'    => $this->option('username'),
59
            'password'    => $this->option('password') ? bcrypt($this->option('password')) : null,
0 ignored issues
show
Bug introduced by
It seems like $this->option('password') targeting Illuminate\Console\Command::option() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
60
            'first_name'  => $this->option('firstName'),
61
            'middle_name' => $this->option('middleName'),
62
            'last_name'   => $this->option('lastName'),
63
            'active'      => $this->option('active') ?: ! $this->option('inactive'),
64
65
        ], [
66
            $this,
67
            'filter'
68
        ]);
69
70
        // Get required argument
71
        $field = $this->argument('field') ?: $this->ask(Lang::get('rinvex.fort::artisan.user.invalid'));
72
73
        // Find single user
74
        if (intval($field)) {
75
            $user = $this->laravel['rinvex.fort.user']->find($field);
76
        } else if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
77
            $user = $this->laravel['rinvex.fort.user']->findWhere(['email' => $field])->first();
78
        } else {
79
            $user = $this->laravel['rinvex.fort.user']->findWhere(['username' => $field])->first();
80
        }
81
82
        if (! $user) {
83
            return $this->error(Lang::get('rinvex.fort::artisan.user.invalid', ['field' => $field]));
84
        }
85
86
        $rules = [
87
            'email'    => 'sometimes|required|email|max:255|unique:'.config('rinvex.fort.tables.users').',email',
88
            'username' => 'sometimes|required|max:255|unique:'.config('rinvex.fort.tables.users').',username',
89
        ];
90
91
        if (! empty($data)) {
92
            $validator = app(Factory::class)->make($data, $rules);
93
    
94
            if ($validator->fails()) {
95
                $this->error('Errors:');
96
    
97
                foreach ($validator->errors()->getMessages() as $key => $messages) {
98
                    $this->error('- '.$key.': '.$messages[0]);
99
                }
100
            } else {
101
                $user->update($data);
102
    
103
                $this->info(Lang::get('rinvex.fort::artisan.user.updated').' ['.Lang::get('rinvex.fort::artisan.user.id').': '.$user->id.', '.Lang::get('rinvex.fort::artisan.user.email').': '.$user->email.', '.Lang::get('rinvex.fort::artisan.user.username').': '.$user->username.']');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 284 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
104
            }
105
        } else {
106
            $this->info(Lang::get('rinvex.fort::artisan.user.nothing'));
107
        }
108
    }
109
110
    /**
111
     * Filter null and empty values.
112
     *
113
     * @param $value
114
     *
115
     * @return bool
116
     */
117
    protected function filter($value)
118
    {
119
        return ($value !== null && $value !== '');
120
    }
121
}
122