|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
|
4
|
|
|
|
|
5
|
|
|
trait HasPushSubscriptions |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get all of the subscriptions. |
|
9
|
|
|
* |
|
10
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
11
|
|
|
*/ |
|
12
|
10 |
|
public function pushSubscriptions() |
|
13
|
|
|
{ |
|
14
|
10 |
|
return $this->morphMany(config('webpush.model'), 'subscribable'); |
|
|
|
|
|
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Update (or create) subscription. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $endpoint |
|
21
|
|
|
* @param string|null $key |
|
22
|
|
|
* @param string|null $token |
|
23
|
|
|
* @param string|null $contentEncoding |
|
24
|
|
|
* @return \NotificationChannels\WebPush\PushSubscription |
|
25
|
|
|
*/ |
|
26
|
9 |
|
public function updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null) |
|
27
|
|
|
{ |
|
28
|
9 |
|
$subscription = app(config('webpush.model'))->findByEndpoint($endpoint); |
|
29
|
|
|
|
|
30
|
9 |
|
if ($subscription && $this->ownsPushSubscription($subscription)) { |
|
31
|
1 |
|
$subscription->public_key = $key; |
|
32
|
1 |
|
$subscription->auth_token = $token; |
|
33
|
1 |
|
$subscription->content_encoding = $contentEncoding; |
|
34
|
1 |
|
$subscription->save(); |
|
35
|
|
|
|
|
36
|
1 |
|
return $subscription; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
9 |
|
if ($subscription && ! $this->ownsPushSubscription($subscription)) { |
|
40
|
1 |
|
$subscription->delete(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
return $this->pushSubscriptions()->create([ |
|
44
|
9 |
|
'endpoint' => $endpoint, |
|
45
|
9 |
|
'public_key' => $key, |
|
46
|
9 |
|
'auth_token' => $token, |
|
47
|
9 |
|
'content_encoding' => $contentEncoding, |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Determine if the model owns the given subscription. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \NotificationChannels\WebPush\PushSubscription $subscription |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function ownsPushSubscription($subscription) |
|
58
|
|
|
{ |
|
59
|
3 |
|
return (string) $subscription->subscribable_id === (string) $this->getKey() && |
|
|
|
|
|
|
60
|
3 |
|
$subscription->subscribable_type === $this->getMorphClass(); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Delete subscription by endpoint. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $endpoint |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function deletePushSubscription($endpoint) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->pushSubscriptions() |
|
72
|
1 |
|
->where('endpoint', $endpoint) |
|
73
|
1 |
|
->delete(); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get all of the subscriptions. |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function routeNotificationForWebPush() |
|
82
|
|
|
{ |
|
83
|
3 |
|
return $this->pushSubscriptions; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.