Passed
Push — master ( 823e5a...6ca56e )
by Quim González
04:15
created

EditUserCommand::handle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 0
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Http\Requests\Traits\AsksForUsers;
6
use App\User;
7
use Illuminate\Console\Command;
8
9
class EditUserCommand extends Command
10
{
11
    use AsksForUsers;
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'user:edit {id? : The User id} {name? : The User name} {email? : The user email}';
18
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Edit an existing user.';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @return void
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        //si ens passen l'id, l'agafem, però sino mostrem tots els id amb l'ajuda del Trait.
45
        $id = $this->argument('id') ? $this->argument('id') : $this->askForUsers();
46
47
        $user = User::findOrFail($id);
48
49
        try {
50
            $user->update([
51
                'name'        => $this->argument('name') ? $this->argument('name') : $this->ask('User name?'),
52
                'email'     => $this->argument('email') ? $this->argument('user_id') : $this->ask('User email?'),
53
            ]);
54
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type App\Console\Commands\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
55
            $this->error('error'.$e);
56
        }
57
        $this->info('User has been edited succesfully');
58
59
    }
60
}
61