Passed
Branch master (cd4548)
by Fèvre
19:36
created

User::roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

323
            ::/** @scrutinizer ignore-call */ select([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
324
                'permissions.*',
325
                'permission_role.created_at as pivot_created_at',
326
                'permission_role.updated_at as pivot_updated_at'
327
            ])
328
            ->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
329
            ->join('roles', 'roles.id', '=', 'permission_role.role_id')
330
            ->whereIn('roles.id', $this->getRoles()->pluck('id')->toArray())
331
            ->orWhere('roles.level', '<', $this->level())
332
            ->groupBy([
333
                'permissions.id',
334
                'permissions.name',
335
                'permissions.slug',
336
                'permissions.description',
337
                'permissions.model',
338
                'permissions.created_at',
339
                'permissions.updated_at',
340
                'permissions.is_deletable',
341
                'pivot_created_at',
342
                'pivot_updated_at'
343
            ]);
344
    }
345
}
346