Completed
Push — master ( a23113...3f274c )
by Sherif
02:17
created

AclUser::getDeletedAtAttribute()   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 namespace App\Modules\Acl;
2
3
use App\User;
4
use Illuminate\Database\Eloquent\SoftDeletes;
5
use Illuminate\Notifications\Notifiable;
6
use Laravel\Passport\HasApiTokens;
7
8
class AclUser extends User {
9
10
    use SoftDeletes, HasApiTokens;
11
    protected $table    = 'users';
12
    protected $dates    = ['created_at', 'updated_at', 'deleted_at'];
13
    protected $hidden   = ['password', 'remember_token','deleted_at'];
14
    protected $guarded  = ['id'];
15
    protected $fillable = ['profile_picture', 'name', 'email', 'password'];
16
    public $searchable  = ['name', 'email'];
17
    
18
    public function getCreatedAtAttribute($value)
19
    {
20
        return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString();
21
    }
22
23
    public function getUpdatedAtAttribute($value)
24
    {
25
        return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString();
26
    }
27
28
    public function getDeletedAtAttribute($value)
29
    {
30
        return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString();
31
    }
32
33
    /**
34
     * Encrypt the password attribute before
35
     * saving it in the storage.
36
     * 
37
     * @param string $value 
38
     */
39
    public function setPasswordAttribute($value)
40
    {
41
        $this->attributes['password'] = bcrypt($value);
42
    }
43
44
    /**
45
     * Get the entity's notifications.
46
     */
47
    public function notifications()
48
    {
49
        return $this->morphMany('\App\Modules\Notifications\Notification', 'notifiable')->orderBy('created_at', 'desc');
50
    }
51
52
    /**
53
     * Get the entity's read notifications.
54
     */
55
    public function readNotifications()
56
    {
57
        return $this->notifications()->whereNotNull('read_at');
58
    }
59
60
    /**
61
     * Get the entity's unread notifications.
62
     */
63
    public function unreadNotifications()
64
    {
65
        return $this->notifications()->whereNull('read_at');
66
    }
67
68
    public function groups()
69
    {
70
        return $this->belongsToMany('\App\Modules\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps();
71
    }
72
73
    public function oauthClients()
74
    {
75
        return $this->hasMany('App\Modules\Acl\OauthClient', 'user_id');
76
    }
77
78
    /**
79
     * Return fcm device tokens that will be used in sending fcm notifications.
80
     * 
81
     * @return array
82
     */
83
    public function routeNotificationForFCM()
84
    {
85
        $devices = \Core::pushNotificationsDevices()->findBy(['user_id' => $this->id]);
86
        $tokens  = [];
87
88
        foreach ($devices as $device) 
89
        {
90
            $accessToken = decrypt($device->access_token);
91
92
            try
93
            {
94
                if (\Core::users()->accessTokenExpiredOrRevoked($accessToken)) 
95
                {
96
                    continue;
97
                }
98
99
                $tokens[] = $device->device_token;
100
            } 
101
            catch (\Exception $e) 
102
            {
103
                $device->forceDelete();
104
            }
105
        }
106
107
        return $tokens;
108
    }
109
110
    /**
111
     * The channels the user receives notification broadcasts on.
112
     *
113
     * @return string
114
     */
115
    public function receivesBroadcastNotificationsOn()
116
    {
117
        return 'users.' . $this->id;
118
    }
119
120
    /**
121
     * Custom password validation.
122
     * 
123
     * @param  string $password
124
     * @return boolean
125
     */
126
    public function validateForPassportPasswordGrant($password) 
127
    {
128
        if ($password == config('skeleton.social_pass'))
129
        {
130
            return true;
131
        }
132
133
        return \Hash::check($password, $this->password);
134
    }
135
    
136
    public static function boot()
137
    {
138
        parent::boot();
139
        parent::observe(\App::make('App\Modules\Acl\ModelObservers\AclUserObserver'));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (observe() instead of boot()). Are you sure this is correct? If so, you might want to change this to $this->observe().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
140
    }
141
}
142