Completed
Pull Request — master (#96)
by
unknown
02:07
created

User::preferredDevice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Notifications\Notifiable;
6
use Illuminate\Foundation\Auth\User as Authenticatable;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Spatie\Activitylog\Traits\LogsActivity;
9
//use Spatie\Activitylog\Traits\CausesActivity;
10
11
class User extends Authenticatable
12
{
13
    use Notifiable;
14
    use SoftDeletes;
15
    use LogsActivity;
16
    //use CausesActivity;
17
18
    /**
19
     * The attributes that should be mutated to dates.
20
     *
21
     * @var array
22
     */
23
    protected $dates = [
24
        'deleted_at'
25
    ];
26
27
    /**
28
     * The attributes that are mass assignable.
29
     *
30
     * @var array
31
     */
32
    protected $fillable = [
33
        'name', 'email', 'password', 'role', 'phone', 'preferred_device_id'
34
    ];
35
    
36
    /**
37
     * The attributes to ignore in the Activity Log
38
     *
39
     * @var array
40
     */
41
    protected static $ignoreChangedAttributes = [
42
        'updated_at', 'remember_token'
43
    ];
44
    
45
    /**
46
     * The attributes to log in the Activity Log
47
     *
48
     * @var array
49
     */
50
    protected static $logAttributes = [
51
        'name', 'email', 'password', 'role', 'phone', 'preferred_device_id'
52
    ];
53
    
54
    /**
55
     * Only log those that have actually changed after the update.
56
     *
57
     * @var array
58
     */
59
    protected static $logOnlyDirty = true;
60
61
    /**
62
     * The attributes that should be hidden for arrays.
63
     *
64
     * @var array
65
     */
66
    protected $hidden = [
67
        'password', 'remember_token',
68
    ];
69
    
70
    /**
71
     * Update the updated_at and created_at timestamps?
72
     *
73
     * @var array
74
     */
75
    public $timestamps = true;
76
77
    /**
78
     * Route notifications for the Nexmo channel.
79
     *
80
     * @return string
81
     */
82
    public function routeNotificationForNexmo()
83
    {
84
        return $this->phone;
85
    }
86
    
87
    /**
88
     * Is user Admin or better?
89
     *
90
     * @return boolean
91
     */
92
    public function isAdmin()
93
    {
94
        return $this->role > 2;
95
    }
96
    
97
    /**
98
     * Is user Manager or better?
99
     *
100
     * @return boolean
101
     */
102
    public function isManager()
103
    {
104
        return $this->role > 1;
105
    }
106
107
    /**
108
     * Is user User or better?
109
     *
110
     * @return boolean
111
     */
112
    public function isUser()
113
    {
114
        return $this->role > 0;
115
    }
116
    
117
    /**
118
     * Is user a guest?
119
     *
120
     * @return boolean
121
     */
122
    public function isGuest()
123
    {
124
        return $this->role == 0;
125
    }
126
    
127
    
128
    /**
129
     * Returns a list of managers.
130
     *
131
     * @return Users
132
     */
133
    public function managers()
134
    {
135
        return $this->where('role', '>', 1)->get();
136
    }
137
    
138
    /**
139
     * Returns the users role as a string.
140
     *
141
     * @return Users
142
     */
143
    public function roleString()
144
    {
145
        $role_en = array(0 => "Registered", 1 => "User", 2 => "Manager", 3 => "Admin");
146
        return $role_en[ $this->role ] . ' (' . $this->role . ')';
147
    }
148
    
149
    /**
150
     * Get the preferred device of the user
151
     */
152
    public function preferredDevice()
153
    {
154
        return $this->hasOne('App\Device', 'id', 'preferred_device_id');
155
    }
156
}
157