Completed
Pull Request — master (#116)
by
unknown
02:19
created

User::getCreatedAtHumanAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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
10
class User extends Authenticatable
11
{
12
    use Notifiable;
13
    use SoftDeletes;
14
    use LogsActivity;
15
16
    /**
17
     * The attributes that should be mutated to dates.
18
     *
19
     * @var array
20
     */
21
    protected $dates = [
22
        'deleted_at'
23
    ];
24
25
    /**
26
     * The attributes that are mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = [
31
        'name', 'email', 'password', 'role', 'phone', 'preferred_device_id'
32
    ];
33
    
34
    /**
35
     * The attributes to ignore in the Activity Log
36
     *
37
     * @var array
38
     */
39
    protected static $ignoreChangedAttributes = [
40
        'updated_at', 'remember_token'
41
    ];
42
    
43
    /**
44
     * The attributes to log in the Activity Log
45
     *
46
     * @var array
47
     */
48
    protected static $logAttributes = [
49
        'name', 'email', 'password', 'role', 'phone', 'preferred_device_id'
50
    ];
51
    
52
    /**
53
     * Only log those that have actually changed after the update.
54
     *
55
     * @var array
56
     */
57
    protected static $logOnlyDirty = true;
58
59
    /**
60
     * The attributes that should be hidden for arrays.
61
     *
62
     * @var array
63
     */
64
    protected $hidden = [
65
        'password', 'remember_token',
66
    ];
67
    
68
    /**
69
     * Update the updated_at and created_at timestamps?
70
     *
71
     * @var array
72
     */
73
    public $timestamps = true;
74
75
    /**
76
     * Route notifications for the Nexmo channel.
77
     *
78
     * @return string
79
     */
80
    public function routeNotificationForNexmo()
81
    {
82
        return $this->phone;
83
    }
84
    
85
    /**
86
     * Is user Admin or better?
87
     *
88
     * @return boolean
89
     */
90
    public function isAdmin()
91
    {
92
        return $this->role > 2;
93
    }
94
    
95
    /**
96
     * Is user Manager or better?
97
     *
98
     * @return boolean
99
     */
100
    public function isManager()
101
    {
102
        return $this->role > 1;
103
    }
104
105
    /**
106
     * Is user User or better?
107
     *
108
     * @return boolean
109
     */
110
    public function isUser()
111
    {
112
        return $this->role > 0;
113
    }
114
    
115
    /**
116
     * Is user a guest?
117
     *
118
     * @return boolean
119
     */
120
    public function isGuest()
121
    {
122
        return $this->role == 0;
123
    }
124
    
125
    
126
    /**
127
     * Returns a list of managers.
128
     *
129
     * @return Users
130
     */
131
    public function managers()
132
    {
133
        return $this->where('role', '>', 1)->get();
134
    }
135
    
136
    /**
137
     * Returns the users role as a string.
138
     *
139
     * @return Users
140
     */
141
    public function roleString()
142
    {
143
        $role_en = array(0 => "Registered", 1 => "User", 2 => "Manager", 3 => "Admin");
144
        return $role_en[ $this->role ] . ' (' . $this->role . ')';
145
    }
146
    
147
    /**
148
     * Get the preferred device of the user
149
     */
150
    public function preferredDevice()
151
    {
152
        return $this->hasOne('App\Device', 'id', 'preferred_device_id');
153
    }
154
    
155
    /**
156
     * Accessor: Get the user's last update time in seconds/minutes/hours since update or converted to user
157
     * friendly readable format.
158
     * If the time is less then a day old then display time since it last updated
159
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
160
     * and using the user's preferred timezone
161
     *
162
     *
163
     * @return string
164
     */
165 View Code Duplication
    public function getUpdatedAtHumanAttribute()
166
    {
167
        if ($this->updated_at->diffInDays() > 0)
168
            return $this->updated_at->setTimezone($this->timezone)->format('M d, Y h:i a');
169
        else
170
            return $this->updated_at->diffForHumans();
171
    }
172
    
173
    /**
174
     * Accessor: Get the user's creation time in seconds/minutes/hours since update or converted to user
175
     * friendly readable format.
176
     * If the time is less then a day old then display time since creation
177
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
178
     * and using the user's preferred timezone
179
     *
180
     *
181
     * @return string
182
     */
183 View Code Duplication
    public function getCreatedAtHumanAttribute()
184
    {
185
        if ($this->created_at->diffInDays() > 0)
186
            return $this->created_at->setTimezone($this->timezone)->format('M d, Y h:i a');
187
        else
188
            return $this->created_at->diffForHumans();
189
    }
190
}
191