Completed
Push — issue-37 ( e24320...44d7c4 )
by Fèvre
03:48
created

User::discussLogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Database\Eloquent\Builder;
11
use Illuminate\Foundation\Auth\Access\Authorizable;
12
use Illuminate\Notifications\DatabaseNotification;
13
use Illuminate\Notifications\Notifiable;
14
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
15
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
16
use Spatie\MediaLibrary\Media;
17
use Ultraware\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
18
use Ultraware\Roles\Traits\HasRoleAndPermission;
19
use Xetaravel\Models\Presenters\UserPresenter;
20
use Xetaravel\Notifications\ResetPasswordNotification;
21
22
class User extends Model implements
23
    AuthenticatableContract,
24
    AuthorizableContract,
25
    CanResetPasswordContract,
26
    HasRoleAndPermissionContract,
27
    HasMediaConversions
28
{
29
    use Authenticatable,
30
        Authorizable,
31
        CanResetPassword,
32
        Notifiable,
33
        Sluggable,
34
        HasRoleAndPermission,
35
        HasMediaTrait,
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
        'github_id',
49
        'register_ip',
50
        'last_login_ip',
51
        'last_login'
52
    ];
53
54
    /**
55
     * The attributes that should be hidden for arrays.
56
     *
57
     * @var array
58
     */
59
    protected $hidden = [
60
        'password',
61
        'remember_token'
62
    ];
63
64
    /**
65
     * The accessors to append to the model's array form.
66
     *
67
     * @var array
68
     */
69
    protected $appends = [
70
        'profile_background',
71
        'profile_url',
72
73
        // Media Model
74
        'avatar_small',
75
        'avatar_medium',
76
        'avatar_big',
77
        'avatar_primary_color',
78
79
        // Account Model
80
        'first_name',
81
        'last_name',
82
        'full_name',
83
        'biography',
84
        'signature',
85
        'facebook',
86
        'twitter'
87
    ];
88
89
    /**
90
     * The attributes that should be mutated to dates.
91
     *
92
     * @var array
93
     */
94
    protected $dates = [
95
        'last_login'
96
    ];
97
98
    /**
99
     * The "booting" method of the model.
100
     *
101
     * @return void
102
     */
103
    protected static function boot()
104
    {
105
        parent::boot();
106
107
        // Generated the slug before updating.
108
        static::updating(function ($model) {
109
            $model->generateSlug();
110
        });
111
112
        static::deleting(function ($model) {
113
            foreach ($model->discussPosts as $post) {
114
                $post->delete();
115
            }
116
117
            foreach ($model->discussUsers as $user) {
118
                $user->delete();
119
            }
120
121
            foreach ($model->discussConversations as $conversation) {
122
                $conversation->delete();
123
            }
124
        });
125
    }
126
127
    /**
128
     * Return the field to slug.
129
     *
130
     * @return string
131
     */
132
    public function slugStrategy(): string
133
    {
134
        return 'username';
135
    }
136
137
    /**
138
     * Register the related to the Model.
139
     *
140
     * @return void
141
     */
142
    public function registerMediaConversions(Media $media = null)
143
    {
144
        $this->addMediaConversion('thumbnail.small')
145
                ->width(100)
146
                ->height(100)
147
                ->keepOriginalImageFormat();
148
149
        $this->addMediaConversion('thumbnail.medium')
150
                ->width(200)
151
                ->height(200)
152
                ->keepOriginalImageFormat();
153
154
        $this->addMediaConversion('thumbnail.big')
155
                ->width(300)
156
                ->height(300)
157
                ->keepOriginalImageFormat();
158
159
        $this->addMediaConversion('original')
160
                ->keepOriginalImageFormat();
161
    }
162
163
    /**
164
     * Get the comments for the user.
165
     *
166
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
167
     */
168
    public function comments()
169
    {
170
        return $this->hasMany(Comment::class);
171
    }
172
173
    /**
174
     * Get the articles for the user.
175
     *
176
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
177
     */
178
    public function articles()
179
    {
180
        return $this->hasMany(Article::class);
181
    }
182
183
    /**
184
     * Get the account for the user.
185
     *
186
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
187
     */
188
    public function account()
189
    {
190
        return $this->hasOne(Account::class);
191
    }
192
193
    /**
194
     * Get the roles for the user.
195
     *
196
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
197
     */
198
    public function roles()
199
    {
200
        return $this->belongsToMany(Role::class)->withTimestamps();
201
    }
202
203
    /**
204
     * Get the badges for the user.
205
     *
206
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
207
     */
208
    public function badges()
209
    {
210
        return $this->belongsToMany(Badge::class)->withTimestamps();
211
    }
212
213
    /**
214
     * Get the notifications for the user.
215
     *
216
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
217
     */
218
    public function notifications()
219
    {
220
        return $this->morphMany(DatabaseNotification::class, 'notifiable')
221
                        ->orderBy('read_at', 'asc')
222
                        ->orderBy('created_at', 'desc');
223
    }
224
225
    /**
226
     * Get the discuss posts for the user.
227
     *
228
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
229
     */
230
    public function discussPosts()
231
    {
232
        return $this->hasMany(DiscussPost::class);
233
    }
234
235
    /**
236
     * Get the discuss conversations for the user.
237
     *
238
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
239
     */
240
    public function discussConversations()
241
    {
242
        return $this->hasMany(DiscussConversation::class);
243
    }
244
245
    /**
246
     * Get the discuss users for the user.
247
     *
248
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
249
     */
250
    public function discussUsers()
251
    {
252
        return $this->hasMany(DiscussUser::class);
253
    }
254
255
    /**
256
     * Get the discuss logs for the user.
257
     *
258
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
259
     */
260
    public function discussLogs()
261
    {
262
        return $this->hasMany(DiscussLog::class);
263
    }
264
265
    /**
266
     * Get the rubies for the user.
267
     *
268
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
269
     */
270
    public function rubies()
271
    {
272
        return $this->hasMany(Ruby::class);
273
    }
274
275
    /**
276
     * Get the experiences for the user.
277
     *
278
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
279
     */
280
    public function experiences()
281
    {
282
        return $this->hasMany(Experience::class);
283
    }
284
285
    /**
286
     * Send the password reset notification.
287
     *
288
     * @param string $token
289
     *
290
     * @return void
291
     */
292
    public function sendPasswordResetNotification($token)
293
    {
294
        $this->notify(new ResetPasswordNotification($token));
295
    }
296
297
    /**
298
     * Get all permissions from roles.
299
     *
300
     * @return \Illuminate\Database\Eloquent\Builder
301
     */
302
    public function rolePermissions(): Builder
303
    {
304
        $permissionModel = app(config('roles.models.permission'));
305
306
        return $permissionModel
307
            ::select([
308
                'permissions.*',
309
                'permission_role.created_at as pivot_created_at',
310
                'permission_role.updated_at as pivot_updated_at'
311
            ])
312
            ->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
313
            ->join('roles', 'roles.id', '=', 'permission_role.role_id')
314
            ->whereIn('roles.id', $this->getRoles()->pluck('id')->toArray())
315
            ->orWhere('roles.level', '<', $this->level())
316
            ->groupBy([
317
                'permissions.id',
318
                'permissions.name',
319
                'permissions.slug',
320
                'permissions.description',
321
                'permissions.model',
322
                'permissions.created_at',
323
                'permissions.updated_at',
324
                'permissions.is_deletable',
325
                'pivot_created_at',
326
                'pivot_updated_at'
327
            ]);
328
    }
329
}
330