RestoreUserCommand::getUserModel()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 9.9666
1
<?php
2
3
namespace Luilliarcec\UserCommands\Commands;
4
5
6
class RestoreUserCommand extends UserCommand
7
{
8
    /**
9
     * The name and signature of the console command.
10
     *
11
     * @var string
12
     */
13
    protected $signature = 'user:restore
14
                            {value : Get user for a value (by email, id)}
15
                            {field? : Field to search by}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Restore a user';
23
24
    /**
25
     * Execute the console command.
26
     */
27
    public function handle()
28
    {
29
        $user = $this->getUserModel();
30
31
        if (is_null($user)) {
32
            $this->error('Oops, the user was not found!');
33
            return 1;
34
        }
35
36
        if (!method_exists($user, 'restore')) {
37
            $this->error('Oops, user does not use softdelete!');
38
            return 1;
39
        }
40
41
        $user->restore();
42
43
        $this->info('User restored successfully!');
44
45
        return 0;
46
    }
47
48
    /**
49
     * Get user model
50
     *
51
     * @return \Illuminate\Database\Eloquent\Model|null
52
     */
53
    protected function getUserModel()
54
    {
55
        $value = $this->argument('value');
56
        $field = $this->argument('field');
57
58
        if ($field) {
59
            return $this->user::onlyTrashed()
0 ignored issues
show
Bug introduced by
The method onlyTrashed() does not exist on null. ( Ignorable by Annotation )

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

59
            return $this->user::/** @scrutinizer ignore-call */ onlyTrashed()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
                ->where($field, $value)
61
                ->first();
62
        }
63
64
        return $this->user::onlyTrashed()
65
            ->where('email', $value)
66
            ->first() ?: $this->user::onlyTrashed()->find($value);
67
    }
68
}
69