Completed
Push — master ( e48f21...33a953 )
by Shawn
05:25 queued 02:57
created

User::setAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3.1406
1
<?php
2
3
namespace SET;
4
5
use Carbon\Carbon;
6
use Illuminate\Foundation\Auth\User as Authenticatable;
7
use Illuminate\Notifications\Notifiable;
8
9
/**
10
 * Class User.
11
 */
12
class User extends Authenticatable
13
{
14
    use Notifiable;
15
16
    /**
17
     * @var string
18
     */
19
    protected $table = 'users';
20
    /**
21
     * @var bool
22
     */
23
    public $timestamps = true;
24
    /**
25
     * @var array
26
     */
27
    protected $fillable = ['username', 'emp_num', 'first_name', 'nickname', 'last_name',
28
        'email', 'phone', 'status', 'clearance', 'elig_date', 'inv', 'inv_close', 'destroyed_date',
29
        'supervisor_id', 'access_level', 'password', ];
30
    /**
31
     * @var array
32
     */
33
    protected $hidden = ['username', 'password', 'remember_token'];
34
35
    /**
36
     * make destroyed_date a Carbon instance.
37
     */
38
    protected $dates = ['destroyed_date'];
39
40 5
    public function supervisor()
41
    {
42 5
        return $this->belongsTo('SET\User', 'supervisor_id');
43
    }
44
45 2
    public function subordinates()
46
    {
47 2
        return $this->hasMany('SET\User', 'supervisor_id');
48
    }
49
50 2
    public function attachments()
51
    {
52 2
        return $this->morphMany('SET\Attachment', 'imageable');
53
    }
54
55 3
    public function notes()
56
    {
57 3
        return $this->hasMany('SET\Note');
58
    }
59
60 3
    public function travels()
61
    {
62 3
        return $this->hasMany('SET\Travel');
63
    }
64
65 44
    public function visits()
66
    {
67 44
        return $this->hasMany('SET\Visit');
68
    }
69
70 3
    public function assignedTrainings()
71
    {
72 3
        return $this->hasMany('SET\TrainingUser', 'user_id');
73
    }
74
75
    public function trainingUsers()
76
    {
77
        return $this->hasMany('SET\TrainingUser', 'user_id');
78
    }
79
80 1
    public function logs()
81
    {
82 1
        return $this->hasMany('SET\Log');
83
    }
84
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
87
     */
88 4
    public function trainings()
89
    {
90 4
        return $this->belongsToMany('SET\Training');
91
    }
92
93
    /**
94
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
95
     */
96 5
    public function groups()
97
    {
98 5
        return $this->belongsToMany('SET\Group')->withPivot('access');
99
    }
100
101
    /**
102
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
103
     */
104 2
    public function duties()
105
    {
106 2
        return $this->belongsToMany('SET\Duty');
107
    }
108
109
    /**
110
     * If we have a nickname, return 'lastname, nickname' otherwise return 'lastname, firstname'.
111
     *
112
     * @return string
113
     */
114 54
    public function getUserFullNameAttribute()
115
    {
116 54
        if ($this->attributes['id'] == 1) {
117 10
            return 'system';
118
        }
119
120 54
        $firstName = $this->attributes['first_name'];
121
122 54
        if ($this->attributes['nickname']) {
123 52
            $firstName = $this->attributes['first_name'].' ('.$this->attributes['nickname'].')';
124
        }
125
126 54
        if (Setting::get('full_name_format') == 'first_last') {
127 1
            return $firstName.' '.$this->attributes['last_name'];
128
        }
129
130 53
        return $this->attributes['last_name'].', '.$firstName;
131
    }
132
133
    /**
134
     * @param $query
135
     * @param $input
136
     */
137 3
    public function scopeSearchUsers($query, $input)
138
    {
139 3
        return $query->where('first_name', 'LIKE', "%$input%")
140 3
            ->orWhere('last_name', 'LIKE', "%$input%")
141 3
            ->orWhere('emp_num', 'LIKE', "%$input%");
142
    }
143
144
    /**
145
     * @param $query
146
     *
147
     * @return mixed
148
     */
149 60
    public function scopeActive($query)
150
    {
151 60
        return $query->where('status', 'active');
152
    }
153
154 16
    public function scopeSkipSystem($query)
155
    {
156 16
        return $query->where('id', '>', 1);
157
    }
158
159
    /**
160
     * Store empty values as null in the DB.
161
     *
162
     * @param string $key
163
     * @param mixed  $value
164
     *
165
     * @return $this
166
     */
167 126 View Code Duplication
    public function setAttribute($key, $value)
168
    {
169 126
        if (is_scalar($value) && $value === '') {
170
            $value = null;
171
        }
172
173 126
        return parent::setAttribute($key, $value);
174
    }
175
176 4
    public function getDestroyDate($status)
177
    {
178 4
        if ($status == 'active') {
179 2
            return;
180
        }
181
182 2
        if ($status == 'separated') {
183 1
            return Carbon::today()->addYears(2)->startOfWeek();
184
        }
185
186 1
        return Carbon::today()->addWeek()->startOfWeek();
187
    }
188
}
189