1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
4
|
|
|
|
5
|
|
|
trait HasPushSubscriptions |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get the user's subscriptions. |
9
|
|
|
* |
10
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
11
|
|
|
*/ |
12
|
8 |
|
public function pushSubscriptions() |
13
|
|
|
{ |
14
|
8 |
|
return $this->hasMany(PushSubscription::class); |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Update (or create) user subscription. |
19
|
|
|
* |
20
|
|
|
* @param string $endpoint |
21
|
|
|
* @param string|null $key |
22
|
|
|
* @param string|null $token |
23
|
|
|
* @return \NotificationChannels\WebPush\PushSubscription |
24
|
|
|
*/ |
25
|
7 |
|
public function updatePushSubscription($endpoint, $key = null, $token = null) |
26
|
|
|
{ |
27
|
7 |
|
$subscription = PushSubscription::findByEndpoint($endpoint); |
28
|
|
|
|
29
|
7 |
|
if ($subscription && $this->pushSubscriptionBelongsToUser($subscription)) { |
30
|
|
|
$subscription->public_key = $key; |
|
|
|
|
31
|
|
|
$subscription->auth_token = $token; |
|
|
|
|
32
|
|
|
$subscription->save(); |
33
|
|
|
|
34
|
|
|
return $subscription; |
35
|
|
|
} |
36
|
|
|
|
37
|
7 |
|
if ($subscription && ! $this->pushSubscriptionBelongsToUser($subscription)) { |
38
|
2 |
|
$subscription->delete(); |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
return $this->pushSubscriptions()->save(new PushSubscription([ |
42
|
7 |
|
'endpoint' => $endpoint, |
43
|
7 |
|
'public_key' => $key, |
44
|
7 |
|
'auth_token' => $token, |
45
|
|
|
])); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Determine if the given subscription belongs to this user. |
50
|
|
|
* |
51
|
|
|
* @param \NotificationChannels\WebPush\PushSubscription $subscription |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
3 |
|
public function pushSubscriptionBelongsToUser($subscription) |
55
|
|
|
{ |
56
|
3 |
|
$subscriber_foreing_key = config('webpush.subscriber_foreing_key', 'user_id'); |
57
|
|
|
|
58
|
3 |
|
return $subscription->{$subscriber_foreing_key} === $this->getKey(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Delete subscription by endpoint. |
63
|
|
|
* |
64
|
|
|
* @param string $endpoint |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function deletePushSubscription($endpoint) |
68
|
|
|
{ |
69
|
|
|
$this->pushSubscriptions() |
70
|
|
|
->where('endpoint', $endpoint) |
71
|
|
|
->delete(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get all subscriptions. |
76
|
|
|
* |
77
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
78
|
|
|
*/ |
79
|
3 |
|
public function routeNotificationForWebPush() |
80
|
|
|
{ |
81
|
3 |
|
return $this->pushSubscriptions; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.