Passed
Push — master ( 82f4a5...a09b17 )
by Martin
17:51
created

User::permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace App;
4
5
use Illuminate\Notifications\Notifiable;
6
use Illuminate\Contracts\Auth\MustVerifyEmail;
7
use Illuminate\Foundation\Auth\User as Authenticatable;
8
9
class User extends Authenticatable implements MustVerifyEmail
10
{
11
    use Notifiable;
12
13
    /**
14
     * The attributes that are mass assignable.
15
     *
16
     * @var array
17
     */
18
    protected $fillable = [
19
        'name', 'email', 'password',
20
    ];
21
22
    /**
23
     * The attributes that should be hidden for arrays.
24
     *
25
     * @var array
26
     */
27
    protected $hidden = [
28
        'password', 'remember_token',
29
    ];
30
31
    public function permissions()
32
    {
33
        $permissions = collect([]);
34
        $this->roles->each(function ($role) use (&$permissions) {
35
            $role->permissions->each(function ($permission) use (&$permissions) {
36
                $permissions->push($permission);
37
            });
38
        });
39
        return $permissions;
40
        // This approach would require 1:n-relations between models, but those are n:m-relations
41
        // return $this->hasManyThrough('App\Permission', 'App\Role');
42
    }
43
44
    public function deleteWithRoles()
45
    {
46
        $this->roles()->detach();
47
        $this->delete();
48
    }
49
50
    public function roles()
51
    {
52
        return $this->belongsToMany('App\Role');
53
    }
54
55
    public function purchases()
56
    {
57
        return $this->hasMany('App\Purchase', 'customer_id');
58
    }
59
60
    public function purchasesAsVendor()
61
    {
62
        return $this->hasMany('App\Purchase', 'vendor_id');
63
    }
64
65
    public function hasPermission($permission)
66
    {
67
        return $this->permissions()->containsStrict('name', $permission);
68
    }
69
}
70