User::userTokens()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Auth\Authenticatable;
7
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
8
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Collection;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Illuminate\Database\Eloquent\SoftDeletes;
14
use Illuminate\Database\Query\Builder as QueryBuilder;
15
use IonGhitun\MysqlEncryption\Models\BaseModel;
16
use Laravel\Lumen\Auth\Authorizable;
17
18
/**
19
 * Class User
20
 *
21
 * @property int $id
22
 * @property string|null $name
23
 * @property string|null $email
24
 * @property string|null $password
25
 * @property array|null $picture
26
 * @property int $status
27
 * @property int $language_id
28
 * @property int $role_id
29
 * @property string|null $activation_code
30
 * @property string|null $forgot_code
31
 * @property Carbon|null $forgot_time
32
 * @property string|null $facebook_id
33
 * @property string|null $google_id
34
 * @property Carbon|null $created_at
35
 * @property Carbon|null $updated_at
36
 * @property Carbon|null $deleted_at
37
 *
38
 * @property Language $language
39
 * @property Role $role
40
 *
41
 * @property-read Collection|UserToken[] $userTokens
42
 * @property-read Collection|UserNotification[] $userNotifications
43
 * @property-read Collection|UserTask[] $userTasks
44
 * @property-read Collection|UserTask[] $userAssignedTasks
45
 * @property-read int|null $user_assigned_tasks_count
46
 * @property-read int|null $user_notifications_count
47
 * @property-read int|null $user_tasks_count
48
 * @property-read int|null $user_tokens_count
49
 *
50
 * @method static Builder|User newModelQuery()
51
 * @method static Builder|User newQuery()
52
 * @method static Builder|User query()
53
 * @method static Builder|User whereActivationCode($value)
54
 * @method static Builder|User whereCreatedAt($value)
55
 * @method static Builder|User whereDeletedAt($value)
56
 * @method static Builder|User whereEmail($value)
57
 * @method static Builder|User whereFacebookId($value)
58
 * @method static Builder|User whereForgotCode($value)
59
 * @method static Builder|User whereForgotTime($value)
60
 * @method static Builder|User whereGoogleId($value)
61
 * @method static Builder|User whereId($value)
62
 * @method static Builder|User whereLanguageId($value)
63
 * @method static Builder|User whereName($value)
64
 * @method static Builder|User wherePassword($value)
65
 * @method static Builder|User wherePicture($value)
66
 * @method static Builder|User whereRoleId($value)
67
 * @method static Builder|User whereStatus($value)
68
 * @method static Builder|User whereUpdatedAt($value)
69
 * @method static QueryBuilder|User onlyTrashed()
70
 * @method static QueryBuilder|User withTrashed()
71
 * @method static QueryBuilder|User withoutTrashed()
72
 * @method static Builder|BaseModel orWhereEncrypted($column, $value)
73
 * @method static Builder|BaseModel orWhereNotEncrypted($column, $value)
74
 * @method static Builder|BaseModel orderByEncrypted($column, $direction)
75
 * @method static Builder|BaseModel whereEncrypted($column, $value)
76
 * @method static Builder|BaseModel whereNotEncrypted($column, $value)
77
 *
78
 * @package App\Models
79
 */
80
class User extends Model implements AuthenticatableContract, AuthorizableContract
81
{
82
    use Authenticatable, Authorizable, SoftDeletes;
83
84
    /** @var int */
85
    const STATUS_UNCONFIRMED = 0;
86
87
    /** @var int */
88
    const STATUS_CONFIRMED = 1;
89
90
    /** @var int */
91
    const STATUS_EMAIL_UNCONFIRMED = 2;
92
93
    /** @var bool */
94
    public $timestamps = true;
95
96
    /** @var string */
97
    protected $table = 'users';
98
99
    /** @var array */
100
    protected $fillable = [
101
        'name',
102
        'email',
103
        'password',
104
        'picture',
105
        'status',
106
        'language_id',
107
        'role_id',
108
        'activation_code',
109
        'forgot_code',
110
        'forgot_time',
111
        'facebook_id',
112
        'google_id'
113
    ];
114
115
    /** @var array */
116
    protected $hidden = [
117
        'password',
118
        'activation_code',
119
        'forgot_code',
120
        'forgot_time',
121
        'facebook_id',
122
        'google_id'
123
    ];
124
125
    /** @var array */
126
    protected $visible = [
127
        'id',
128
        'name',
129
        'email',
130
        'picture',
131
        'status',
132
        'language_id',
133
        'role_id',
134
        'created_at',
135
        'updated_at',
136
        'language',
137
        'role',
138
        'userNotifications',
139
        'userTasks',
140
        'userAssignedTasks'
141
    ];
142
143
    /** @var array */
144
    protected $casts = [
145
        'status' => 'int'
146
    ];
147
148
    /** @var array */
149
    protected $sortable = [
150
        'id',
151
        'name',
152
        'email',
153
        'status',
154
        'created_at',
155
        'updated_at',
156
    ];
157
158
    /** @var array */
159
    protected $searchable = [
160
        'name',
161
        'email'
162
    ];
163
164
    /** @var array */
165
    protected $encrypted = [
166
        'name',
167
        'email'
168
    ];
169
170
    /** @var array */
171
    protected $filterable = [
172
        'status',
173
        'language_id',
174
        'role_id'
175
    ];
176
177
    /**
178
     * User tokens.
179
     *
180
     * @return HasMany
181
     */
182
    public function userTokens()
183
    {
184
        return $this->hasMany(UserToken::class, 'user_id', 'id');
185
    }
186
187
    /**
188
     * User notifications.
189
     *
190
     * @return HasMany
191
     */
192
    public function userNotifications()
193
    {
194
        return $this->hasMany(UserNotification::class, 'user_id', 'id');
195
    }
196
197
    /**
198
     * Language.
199
     *
200
     * @return BelongsTo
201
     */
202
    public function language()
203
    {
204
        return $this->belongsTo(Language::class, 'language_id', 'id');
205
    }
206
207
    /**
208
     * Role.
209
     *
210
     * @return BelongsTo
211
     */
212
    public function role()
213
    {
214
        return $this->belongsTo(Role::class, 'role_id', 'id');
215
    }
216
217
    /**
218
     * User tasks.
219
     *
220
     * @return HasMany
221
     */
222
    public function userTasks()
223
    {
224
        return $this->hasMany(UserTask::class, 'user_id', 'id');
225
    }
226
227
    /**
228
     * User assigned tasks.
229
     *
230
     * @return HasMany
231
     */
232
    public function userAssignedTasks()
233
    {
234
        return $this->hasMany(UserTask::class, 'assigned_user_id', 'id');
235
    }
236
237
    /**
238
     * @param $value
239
     *
240
     * @return Carbon|null
241
     */
242
    public function getForgotTimeAttribute($value)
243
    {
244
        if ($value !== null) {
245
            return Carbon::parse($value);
246
        }
247
248
        return null;
249
    }
250
251
    /**
252
     * @param $value
253
     *
254
     * @return array|null
255
     */
256
    public function getPictureAttribute($value)
257
    {
258
        if ($value !== null && $value !== '') {
259
            return json_decode($value, true);
260
        }
261
262
        return null;
263
    }
264
}
265