Passed
Push — master ( f2e485...b7427b )
by Karel
06:44
created

User::deleteById()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 9
nop 1
dl 0
loc 21
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace Chuckbe\Chuckcms\Models;
4
5
use Illuminate\Notifications\Notifiable;
6
use Illuminate\Foundation\Auth\User as Authenticatable;
7
use Spatie\Permission\Traits\HasRoles;
8
9
/**
10
 * @property string $name
11
 * @property string $email
12
 * @property string $token
13
 */
14
class User extends Authenticatable
15
{
16
    use HasRoles;
0 ignored issues
show
introduced by
The trait Spatie\Permission\Traits\HasRoles requires some properties which are not provided by Chuckbe\Chuckcms\Models\User: $map, $permissions, $roles, $guard_name
Loading history...
17
    use Notifiable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $phone_number which is not provided by Chuckbe\Chuckcms\Models\User.
Loading history...
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'name', 'email', 'password', 'active', 'token'
26
    ];
27
28
    /**
29
     * The attributes that should be hidden for arrays.
30
     *
31
     * @var array
32
     */
33
    protected $hidden = [
34
        'password', 'remember_token',
35
    ];
36
37
    public function deleteById($id)
38
    {
39
        $user = $this->where('id', $id)->first();
40
        if ($user) {
41
            $roles = $user->getRoleNames();
42
            $permissions = $user->getDirectPermissions();
43
44
            foreach($roles as $role) {
45
                $user->removeRole($role);
46
            }
47
            foreach($permissions as $permission) {
48
                $user->removePermissionTo($permission);
49
            }
50
51
            if ($user->delete()) {
52
                return 'success';
53
            } else {
54
                return 'error';
55
            }
56
        } else {
57
            return 'false';
58
        }
59
    }
60
}