Completed
Push — manage-users ( 2c714e...6ca97d )
by Fèvre
02:42
created

User::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\Sluggable;
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Auth\Passwords\CanResetPassword;
7
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
8
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
10
use Illuminate\Foundation\Auth\Access\Authorizable;
11
use Illuminate\Notifications\DatabaseNotification;
12
use Illuminate\Notifications\Notifiable;
13
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
14
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
15
use Ultraware\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
16
use Ultraware\Roles\Traits\HasRoleAndPermission;
17
use Xetaravel\Models\Entities\UserEntity;
18
use Xetaravel\Models\Presenters\UserPresenter;
19
use Xetaravel\Notifications\ResetPasswordNotification;
20
21
class User extends Model implements
22
    AuthenticatableContract,
23
    AuthorizableContract,
24
    CanResetPasswordContract,
25
    HasRoleAndPermissionContract,
26
    HasMediaConversions
27
{
28
    use Authenticatable,
29
        Authorizable,
30
        CanResetPassword,
31
        Notifiable,
32
        Sluggable,
33
        HasRoleAndPermission,
34
        HasMediaTrait,
35
        UserEntity,
36
        UserPresenter;
37
38
    /**
39
     * The attributes that are mass assignable.
40
     *
41
     * @var array
42
     */
43
    protected $fillable = [
44
        'username',
45
        'email',
46
        'password',
47
        'slug',
48
        'register_ip',
49
        'last_login_ip',
50
        'last_login'
51
    ];
52
53
    /**
54
     * The attributes that should be hidden for arrays.
55
     *
56
     * @var array
57
     */
58
    protected $hidden = [
59
        'password',
60
        'remember_token'
61
    ];
62
63
    /**
64
     * The accessors to append to the model's array form.
65
     *
66
     * @var array
67
     */
68
    protected $appends = [
69
        'profile_background',
70
71
        // Media Model
72
        'avatar_small',
73
        'avatar_medium',
74
        'avatar_big',
75
76
        // Account Model
77
        'first_name',
78
        'last_name',
79
        'biography',
80
        'signature',
81
        'facebook',
82
        'twitter'
83
    ];
84
85
    /**
86
     * The "booting" method of the model.
87
     *
88
     * @return void
89
     */
90
    protected static function boot()
91
    {
92
        parent::boot();
93
94
        // Generated the slug before updating.
95
        static::updating(function ($model) {
96
            $model->generateSlug();
97
        });
98
    }
99
100
    /**
101
     * Return the field to slug.
102
     *
103
     * @return string
104
     */
105
    public function slugStrategy(): string
106
    {
107
        return 'username';
108
    }
109
110
    /**
111
     * Register the related to the Model.
112
     *
113
     * @return void
114
     */
115
    public function registerMediaConversions()
116
    {
117
        $this->addMediaConversion('thumbnail.small')
118
                ->width(100)
119
                ->height(100)
120
                ->keepOriginalImageFormat();
121
122
        $this->addMediaConversion('thumbnail.medium')
123
                ->width(200)
124
                ->height(200)
125
                ->keepOriginalImageFormat();
126
127
        $this->addMediaConversion('thumbnail.big')
128
                ->width(300)
129
                ->height(300)
130
                ->keepOriginalImageFormat();
131
132
        $this->addMediaConversion('original')
133
                ->keepOriginalImageFormat();
134
    }
135
136
    /**
137
     * Get the comments for the user.
138
     *
139
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
140
     */
141
    public function comments()
142
    {
143
        return $this->hasMany(Comment::class);
144
    }
145
146
    /**
147
     * Get the articles for the user.
148
     *
149
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
150
     */
151
    public function articles()
152
    {
153
        return $this->hasMany(Article::class);
154
    }
155
156
    /**
157
     * Get the account for the user.
158
     *
159
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
160
     */
161
    public function account()
162
    {
163
        return $this->hasOne(Account::class);
164
    }
165
166
    /**
167
     * Get the roles for the user.
168
     *
169
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
170
     */
171
    public function roles()
172
    {
173
        return $this->belongsToMany(\Ultraware\Roles\Models\Role::class);
174
    }
175
176
    /**
177
     * Get the badges for the user.
178
     *
179
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
180
     */
181
    public function badges()
182
    {
183
        return $this->belongsToMany(Badge::class)->withTimestamps();
184
    }
185
186
    /**
187
     * Get the notifications for the user.
188
     *
189
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
190
     */
191
    public function notifications()
192
    {
193
        return $this->morphMany(DatabaseNotification::class, 'notifiable')
194
                        ->orderBy('read_at', 'asc')
195
                        ->orderBy('created_at', 'desc');
196
    }
197
198
    /**
199
     * Send the password reset notification.
200
     *
201
     * @param string $token
202
     *
203
     * @return void
204
     */
205
    public function sendPasswordResetNotification($token)
206
    {
207
        $this->notify(new ResetPasswordNotification($token));
208
    }
209
}
210