1 | <?php |
||
5 | trait HasPushSubscriptions |
||
6 | { |
||
7 | /** |
||
8 | * Get the user's subscriptions. |
||
9 | * |
||
10 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
11 | */ |
||
12 | 9 | public function pushSubscriptions() |
|
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 | 8 | public function updatePushSubscription($endpoint, $key = null, $token = null) |
|
26 | { |
||
27 | 8 | $subscription = PushSubscription::findByEndpoint($endpoint); |
|
28 | |||
29 | 8 | if ($subscription && $this->pushSubscriptionBelongsToUser($subscription)) { |
|
30 | 1 | $subscription->public_key = $key; |
|
31 | 1 | $subscription->auth_token = $token; |
|
32 | 1 | $subscription->save(); |
|
33 | |||
34 | 1 | return $subscription; |
|
35 | } |
||
36 | |||
37 | 8 | if ($subscription && ! $this->pushSubscriptionBelongsToUser($subscription)) { |
|
38 | 1 | $subscription->delete(); |
|
39 | 1 | } |
|
40 | |||
41 | 8 | return $this->pushSubscriptions()->save(new PushSubscription([ |
|
42 | 8 | 'endpoint' => $endpoint, |
|
43 | 8 | 'public_key' => $key, |
|
44 | 8 | 'auth_token' => $token, |
|
45 | 8 | ])); |
|
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) |
|
58 | |||
59 | /** |
||
60 | * Delete subscription by endpoint. |
||
61 | * |
||
62 | * @param string $endpoint |
||
63 | * @return void |
||
64 | */ |
||
65 | public function deletePushSubscription($endpoint) |
||
71 | |||
72 | /** |
||
73 | * Get all subscriptions. |
||
74 | * |
||
75 | * @return \Illuminate\Database\Eloquent\Collection |
||
76 | */ |
||
77 | 3 | public function routeNotificationForWebPush() |
|
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
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.