1
1
<?php
2
2
3
- namespace NotificationChannels \WebPushNotifications ;
3
+ namespace NotificationChannels \WebPush ;
4
4
5
5
trait HasPushSubscriptions
6
6
{
@@ -20,36 +20,42 @@ public function pushSubscriptions()
20
20
* @param string $endpoint
21
21
* @param string|null $key
22
22
* @param string|null $token
23
- * @return \Eusebiu\WebPush\PushSubscription
23
+ *
24
+ * @return PushSubscription
24
25
*/
25
26
public function updatePushSubscription ($ endpoint , $ key = null , $ token = null )
26
27
{
27
28
$ subscription = PushSubscription::findByEndpoint ($ endpoint );
28
29
29
- // Check if a subscription exists for the given endpoint.
30
- if ($ subscription ) {
31
- // If it belongs to a different user then delete it.
32
- if ((int ) $ subscription ->user_id !== (int ) $ this ->getAuthIdentifier ()) {
33
- $ subscription ->delete ();
34
- }
35
- // Otherwise update the public key and auth token.
36
- else {
37
- $ subscription ->public_key = $ key ;
38
- $ subscription ->auth_token = $ token ;
39
- $ subscription ->save ();
30
+ if ($ subscription && !$ this ->subscriptionBelongsToDifferentUser ()) {
31
+ $ subscription ->public_key = $ key ;
32
+ $ subscription ->auth_token = $ token ;
33
+ $ subscription ->save ();
34
+
35
+ return $ subscription ;
36
+ }
40
37
41
- return $ subscription ;
42
- }
38
+ if ( $ subscription && $ this -> subscriptionBelongsToDifferentUser ()) {
39
+ $ subscription -> delete ();
43
40
}
44
41
45
- // Just create a new subscription.
42
+
46
43
return $ this ->pushSubscriptions ()->save (new PushSubscription ([
47
44
'endpoint ' => $ endpoint ,
48
45
'public_key ' => $ key ,
49
46
'auth_token ' => $ token ,
50
47
]));
51
48
}
52
49
50
+ /**
51
+ * @param PushSubscription $subscription
52
+ * @return bool
53
+ */
54
+ public function subscriptionBelongsToDifferentUser ($ subscription )
55
+ {
56
+ return (int )$ subscription ->user_id !== (int )$ this ->getAuthIdentifier ();
57
+ }
58
+
53
59
/**
54
60
* Delete subscription by endpoint.
55
61
*
@@ -59,16 +65,16 @@ public function updatePushSubscription($endpoint, $key = null, $token = null)
59
65
public function deleteSubscription ($ endpoint )
60
66
{
61
67
$ this ->pushSubscriptions ()
62
- ->where ('endpoint ' , $ endpoint )
63
- ->delete ();
68
+ ->where ('endpoint ' , $ endpoint )
69
+ ->delete ();
64
70
}
65
71
66
72
/**
67
73
* Get all subscriptions.
68
74
*
69
75
* @return \Illuminate\Database\Eloquent\Collection
70
76
*/
71
- public function routeNotificationForWebPushNotifications ()
77
+ public function routeNotificationForWebPush ()
72
78
{
73
79
return $ this ->pushSubscriptions ;
74
80
}
0 commit comments