Completed
Pull Request — master (#58)
by Shawn
06:25 queued 04:10
created

User::getDestroyDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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
    public function supervisor()
41
    {
42
        return $this->belongsTo('SET\User', 'supervisor_id');
43
    }
44
45
    public function subordinates()
46
    {
47
        return $this->hasMany('SET\User', 'supervisor_id');
48
    }
49
50
    public function attachments()
51
    {
52
        return $this->morphMany('SET\Attachment', 'imageable');
53
    }
54
55
    public function notes()
56
    {
57
        return $this->hasMany('SET\Note');
58
    }
59
60
    public function travels()
61
    {
62
        return $this->hasMany('SET\Travel');
63
    }
64
65
    public function visits()
66
    {
67
        return $this->hasMany('SET\Visit');
68
    }
69
70
    public function assignedTrainings()
71
    {
72
        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
    public function logs()
81
    {
82
        return $this->hasMany('SET\Log');
83
    }
84
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
87
     */
88
    public function trainings()
89
    {
90
        return $this->belongsToMany('SET\Training');
91
    }
92
93
    /**
94
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
95
     */
96
    public function groups()
97
    {
98
        return $this->belongsToMany('SET\Group')->withPivot('access');
99
    }
100
101
    /**
102
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
103
     */
104
    public function duties()
105
    {
106
        return $this->belongsToMany('SET\Duty');
107
    }
108
109
    public function dutySwap()
110
    {
111
        return $this->morphMany('SET\DutySwap', 'imageable');
112
    }
113
114
    public function news()
115
    {
116
        return $this->hasMany('SET\News');
117
    }
118
119
    /**
120
     * If we have a nickname, return 'lastname, nickname' otherwise return 'lastname, firstname'.
121
     *
122
     * @return string
123
     */
124
    public function getUserFullNameAttribute()
125
    {
126
        if ($this->attributes['id'] == 1) {
127
            return 'system';
128
        }
129
130
        $firstName = $this->attributes['first_name'];
131
132
        if ($this->attributes['nickname']) {
133
            $firstName = $this->attributes['first_name'].' ('.$this->attributes['nickname'].')';
134
        }
135
136
        if (Setting::get('full_name_format') == 'first_last') {
137
            return $firstName.' '.$this->attributes['last_name'];
138
        }
139
140
        return $this->attributes['last_name'].', '.$firstName;
141
    }
142
143
    /**
144
     * @param $query
145
     * @param $input
146
     */
147
    public function scopeSearchUsers($query, $input)
148
    {
149
        return $query->where('first_name', 'LIKE', "%$input%")
150
            ->orWhere('last_name', 'LIKE', "%$input%")
151
            ->orWhere('emp_num', 'LIKE', "%$input%");
152
    }
153
154
    /**
155
     * @param $query
156
     *
157
     * @return mixed
158
     */
159
    public function scopeActive($query)
160
    {
161
        return $query->where('status', 'active');
162
    }
163
164
    public function scopeSkipSystem($query)
165
    {
166
        return $query->where('id', '>', 1);
167
    }
168
169
    /**
170
     * @param string $key
171
     * @param mixed  $value
172
     *
173
     * @return $this
174
     */
175 View Code Duplication
    public function setAttribute($key, $value)
176
    {
177
        if (is_scalar($value) && $value === '') {
178
            $value = null;
179
        }
180
181
        return parent::setAttribute($key, $value);
182
    }
183
184
    public function getDestroyDate($status)
185
    {
186
        if ($status == 'active') {
187
            return;
188
        }
189
190
        if ($status == 'separated') {
191
            return Carbon::today()->addYears(2)->startOfWeek();
192
        }
193
194
        return Carbon::today()->addWeek()->startOfWeek();
195
    }
196
}
197