|
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
|
|
|
public function pushSubscriptions() |
|
13
|
|
|
{ |
|
14
|
|
|
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
|
|
|
public function updatePushSubscription($endpoint, $key = null, $token = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$subscription = PushSubscription::findByEndpoint($endpoint); |
|
28
|
|
|
|
|
29
|
|
|
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
|
|
|
if ($subscription && ! $this->pushSubscriptionBelongsToUser($subscription)) { |
|
38
|
|
|
$subscription->delete(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->pushSubscriptions()->save(new PushSubscription([ |
|
42
|
|
|
'endpoint' => $endpoint, |
|
43
|
|
|
'public_key' => $key, |
|
44
|
|
|
'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
|
|
|
public function pushSubscriptionBelongsToUser($subscription) |
|
55
|
|
|
{ |
|
56
|
|
|
return (int) $subscription->user_id === (int) $this->getAuthIdentifier(); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Delete subscription by endpoint. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $endpoint |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function deletePushSubscription($endpoint) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->pushSubscriptions() |
|
68
|
|
|
->where('endpoint', $endpoint) |
|
69
|
|
|
->delete(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get all subscriptions. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
76
|
|
|
*/ |
|
77
|
|
|
public function routeNotificationForWebPush() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->pushSubscriptions; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.