Completed
Push — master ( bfbb50...8023d3 )
by Sherif
09:55
created

AclUser::getCreatedAtAttribute()   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 1
1
<?php
2
3
namespace App\Modules\Users;
4
5
use App\User;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Laravel\Passport\HasApiTokens;
8
use App\Modules\Notifications\Notification;
9
use App\Modules\Roles\Role;
10
use App\Modules\OauthClients\OauthClient;
11
use App\Modules\Users\ModelObservers\AclUserObserver;
12
13
class AclUser extends User
14
{
15
    use SoftDeletes, HasApiTokens;
16
    protected $table    = 'users';
17
    protected $dates    = ['created_at', 'updated_at', 'deleted_at'];
18
    protected $hidden   = ['password', 'remember_token', 'deleted_at'];
19
    protected $guarded  = ['id'];
20
    protected $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone'];
21
22
    /**
23
     * Get the profile picture url.
24
     * @return string
25
     */
26
    public function getProfilePictureAttribute($value)
27
    {
28
        return url(\Storage::url($value));
0 ignored issues
show
Bug introduced by
The method url() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
29
    }
30
31
    /**
32
     * Encrypt the password attribute before
33
     * saving it in the storage.
34
     *
35
     * @param string $value
36
     */
37
    public function setPasswordAttribute($value)
38
    {
39
        $this->attributes['password'] = \Hash::make($value);
40
    }
41
42
    /**
43
     * Get the entity's notifications.
44
     */
45
    public function notifications()
46
    {
47
        return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc');
48
    }
49
50
    /**
51
     * Get the entity's read notifications.
52
     */
53
    public function readNotifications()
54
    {
55
        return $this->notifications()->whereNotNull('read_at');
56
    }
57
58
    /**
59
     * Get the entity's unread notifications.
60
     */
61
    public function unreadNotifications()
62
    {
63
        return $this->notifications()->whereNull('read_at');
64
    }
65
66
    public function roles()
67
    {
68
        return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps();
69
    }
70
71
    public function oauthClients()
72
    {
73
        return $this->hasMany(OauthClient::class, 'user_id');
74
    }
75
76
    /**
77
     * Return fcm device tokens that will be used in sending fcm notifications.
78
     *
79
     * @return array
80
     */
81
    public function routeNotificationForFCM()
82
    {
83
        $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]);
84
        $tokens  = [];
85
86
        foreach ($devices as $device) {
87
            if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) {
88
                $device->forceDelete();
89
                continue;
90
            }
91
92
            $tokens[] = $device->device_token;
93
        }
94
95
        return $tokens;
96
    }
97
98
    /**
99
     * The channels the user receives notification broadcasts on.
100
     *
101
     * @return string
102
     */
103
    public function receivesBroadcastNotificationsOn()
104
    {
105
        return 'users.'.$this->id;
106
    }
107
108
    /**
109
     * Custom password validation.
110
     *
111
     * @param  string $password
112
     * @return boolean
113
     */
114
    public function validateForPassportPasswordGrant($password)
115
    {
116
        if ($password == config('skeleton.social_pass')) {
117
            return true;
118
        }
119
120
        return \Hash::check($password, $this->password);
121
    }
122
    
123
    public static function boot()
124
    {
125
        parent::boot();
126
        AclUser::observe(AclUserObserver::class);
127
    }
128
}
129