Completed
Push — master ( 9ecb5e...97e3cb )
by Sherif
13:30
created

AclUser::routeNotificationForFCM()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 5
nop 0
1
<?php namespace App\Modules\V1\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 = ['name', 'email', 'password'];
16
    public $searchable  = ['name', 'email'];
17
    
18
    public function getCreatedAtAttribute($value)
19
    {
20
        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
21
    }
22
23
    public function getUpdatedAtAttribute($value)
24
    {
25
        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString();
26
    }
27
28
    public function getDeletedAtAttribute($value)
29
    {
30
        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->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
    public function groups()
45
    {
46
        return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps();
47
    }
48
49
    /**
50
     * Return fcm device tokens related to the user.
51
     * @return array
52
     */
53
    public function routeNotificationForFCM()
54
    {
55
        $devices = \Core::pushNotificationsDevices()->findBy(['user_id' => $this->id]);
56
        $tokens  = [];
57
58
        foreach ($devices as $device) 
59
        {
60
            $accessToken = decrypt($device->access_token);
61
62
            try
63
            {
64
                if (\Core::users()->accessTokenExpiredOrRevoked($accessToken)) 
65
                {
66
                    continue;
67
                }
68
69
                $tokens[] = $device->device_token;
70
            } 
71
            catch (\Exception $e) 
72
            {
73
                $device->forceDelete();
74
            }
75
        }
76
77
        return $tokens;
78
    }
79
80
    /**
81
     * The channels the user receives notification broadcasts on.
82
     *
83
     * @return string
84
     */
85
    public function receivesBroadcastNotificationsOn()
86
    {
87
        return 'users.' . $this->id;
88
    }
89
    
90
    public static function boot()
91
    {
92
        parent::boot();
93
        parent::observe(\App::make('App\Modules\V1\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...
94
    }
95
}
96