Skip to content

Commit f4221dd

Browse files
committed
scrutinise
1 parent 125e798 commit f4221dd

9 files changed

+90
-63
lines changed

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ First you must install the service provider:
1414
// config/app.php
1515
'providers' => [
1616
...
17-
NotificationChannels\WebPushNotifications\Provider::class,
17+
NotificationChannels\WebPush\WebPushServiceProvider::class,
1818
];
1919
```
2020

@@ -27,10 +27,10 @@ Then configure [Google Cloud Messaging](https://console.cloud.google.com) by set
2727
],
2828
```
2929

30-
You need to add the `NotificationChannels\WebPushNotifications\HasPushSubscriptions` in your `User` model:
30+
You need to add the `NotificationChannels\WebPush\HasPushSubscriptions` in your `User` model:
3131

3232
``` php
33-
use NotificationChannels\WebPushNotifications\HasPushSubscriptions;
33+
use NotificationChannels\WebPush\HasPushSubscriptions;
3434

3535
class User extends Model
3636
{
@@ -41,7 +41,7 @@ class User extends Model
4141
Next publish the migration with:
4242

4343
``` bash
44-
php artisan vendor:publish --provider="NotificationChannels\WebPushNotifications\Provider" --tag="migrations"
44+
php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="migrations"
4545
```
4646

4747
After the migration has been published you can create the `push_subscriptions` table by running the migrations:
@@ -56,8 +56,8 @@ Now you can use the channel in your `via()` method inside the notification as we
5656

5757
``` php
5858
use Illuminate\Notifications\Notification;
59-
use NotificationChannels\WebPushNotifications\Message;
60-
use NotificationChannels\WebPushNotifications\Channel as WebPushChannel;
59+
use NotificationChannels\WebPush\WebPushMessage;
60+
use NotificationChannels\WebPush\WebPushChannel;
6161

6262
class AccountApproved extends Notification
6363
{
@@ -68,7 +68,7 @@ class AccountApproved extends Notification
6868

6969
public function toWebPush($notifiable, $notification)
7070
{
71-
return (new WebPushMessage())
71+
return WebPushMessage::create()
7272
// ->id($notification->id)
7373
->title('Approved!')
7474
->icon('/approved-icon.png')
@@ -108,6 +108,24 @@ For a complete implementation with a Service Worker check this [demo](https://gi
108108

109109
The [Push API](https://developer.mozilla.org/en/docs/Web/API/Push_API) currently works on Chrome and Firefox.
110110

111+
## Changelog
112+
113+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
114+
115+
## Testing
116+
117+
``` bash
118+
$ composer test
119+
```
120+
121+
## Security
122+
123+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
124+
125+
## Contributing
126+
127+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
128+
111129
## Credits
112130

113131
- [Cretu Eusebiu](https://github.com/cretueusebiu)

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"illuminate/notifications": "^5.3@dev",
1717
"illuminate/support": "^5.3@dev",
1818
"illuminate/events": "^5.3@dev",
19+
"illuminate/database": "^5.3@dev",
1920
"minishlink/web-push": "^1.1"
2021
},
2122
"require-dev": {
@@ -24,12 +25,12 @@
2425
},
2526
"autoload": {
2627
"psr-4": {
27-
"NotificationChannels\\WebPushNotifications\\": "src"
28+
"NotificationChannels\\WebPush\\": "src"
2829
}
2930
},
3031
"autoload-dev": {
3132
"psr-4": {
32-
"NotificationChannels\\:WebPushNotifications\\Test\\": "tests"
33+
"NotificationChannels\\:WebPush\\Test\\": "tests"
3334
}
3435
},
3536
"config": {

src/Events/MessageWasSent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications\Events;
3+
namespace NotificationChannels\WebPush\Events;
44

55
use Illuminate\Notifications\Notification;
66

src/Events/SendingMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications\Events;
3+
namespace NotificationChannels\WebPush\Events;
44

55
use Illuminate\Notifications\Notification;
66

src/HasPushSubscriptions.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications;
3+
namespace NotificationChannels\WebPush;
44

55
trait HasPushSubscriptions
66
{
@@ -20,36 +20,42 @@ public function pushSubscriptions()
2020
* @param string $endpoint
2121
* @param string|null $key
2222
* @param string|null $token
23-
* @return \Eusebiu\WebPush\PushSubscription
23+
*
24+
* @return PushSubscription
2425
*/
2526
public function updatePushSubscription($endpoint, $key = null, $token = null)
2627
{
2728
$subscription = PushSubscription::findByEndpoint($endpoint);
2829

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+
}
4037

41-
return $subscription;
42-
}
38+
if ($subscription && $this->subscriptionBelongsToDifferentUser()) {
39+
$subscription->delete();
4340
}
4441

45-
// Just create a new subscription.
42+
4643
return $this->pushSubscriptions()->save(new PushSubscription([
4744
'endpoint' => $endpoint,
4845
'public_key' => $key,
4946
'auth_token' => $token,
5047
]));
5148
}
5249

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+
5359
/**
5460
* Delete subscription by endpoint.
5561
*
@@ -59,16 +65,16 @@ public function updatePushSubscription($endpoint, $key = null, $token = null)
5965
public function deleteSubscription($endpoint)
6066
{
6167
$this->pushSubscriptions()
62-
->where('endpoint', $endpoint)
63-
->delete();
68+
->where('endpoint', $endpoint)
69+
->delete();
6470
}
6571

6672
/**
6773
* Get all subscriptions.
6874
*
6975
* @return \Illuminate\Database\Eloquent\Collection
7076
*/
71-
public function routeNotificationForWebPushNotifications()
77+
public function routeNotificationForWebPush()
7278
{
7379
return $this->pushSubscriptions;
7480
}

src/PushSubscription.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications;
3+
namespace NotificationChannels\WebPush;
44

55
use Illuminate\Support\Facades\Config;
66
use Illuminate\Database\Eloquent\Model;

src/Channel.php renamed to src/WebPushChannel.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications;
3+
namespace NotificationChannels\WebPush;
44

55
use Minishlink\WebPush\WebPush;
66
use Illuminate\Notifications\Notification;
7-
use NotificationChannels\WebPushNotifications\Events\MessageWasSent;
8-
use NotificationChannels\WebPushNotifications\Events\SendingMessage;
7+
use NotificationChannels\WebPush\Events\MessageWasSent;
8+
use NotificationChannels\WebPush\Events\SendingMessage;
99

10-
class Channel
10+
class WebPushChannel
1111
{
12-
/**
13-
* @var \Minishlink\WebPush\WebPush
14-
*/
12+
/** @var \Minishlink\WebPush\WebPush */
1513
protected $webPush;
1614

1715
/**
18-
* Create a new Web Push channel instance.
19-
*
2016
* @param \Minishlink\WebPush\WebPush $webPush
2117
* @return void
2218
*/
@@ -40,7 +36,7 @@ public function send($notifiable, Notification $notification)
4036
return;
4137
}
4238

43-
$subscriptions = $notifiable->routeNotificationFor('WebPushNotifications');
39+
$subscriptions = $notifiable->routeNotificationFor('WebPush');
4440

4541
if ($subscriptions->isEmpty()) {
4642
return;
@@ -59,15 +55,23 @@ public function send($notifiable, Notification $notification)
5955

6056
$response = $this->webPush->flush();
6157

62-
// Delete the invalid subscriptions.
58+
$this->deleteInvalidSubscriptions($response, $subscriptions);
59+
60+
event(new MessageWasSent($notifiable, $notification));
61+
}
62+
63+
/**
64+
* @param $response
65+
* @param $subscriptions
66+
*/
67+
protected function deleteInvalidSubscriptions($response, $subscriptions)
68+
{
6369
if (is_array($response)) {
6470
foreach ($response as $index => $value) {
65-
if (! $value['success'] && isset($subscriptions[$index])) {
71+
if (!$value['success'] && isset($subscriptions[$index])) {
6672
$subscriptions[$index]->delete();
6773
}
6874
}
6975
}
70-
71-
event(new MessageWasSent($notifiable, $notification));
7276
}
7377
}

src/Message.php renamed to src/WebPushMessage.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications;
3+
namespace NotificationChannels\WebPush;
44

55
use Illuminate\Support\Arr;
66

7-
class Message
7+
class WebPushMessage
88
{
99
/**
1010
* The notification id.
@@ -42,12 +42,20 @@ class Message
4242
protected $actions = [];
4343

4444
/**
45-
* @param string $title
4645
* @param string $body
46+
* @return static
4747
*/
48-
public function __construct($title = '', $body = '')
48+
public static function create($body = '')
4949
{
50-
$this->title = $title;
50+
return static ($body);
51+
}
52+
53+
/**
54+
* @param string $body
55+
*/
56+
public function __construct($body = '')
57+
{
58+
$this->title = '';
5159
$this->body = $body;
5260
}
5361

src/Provider.php renamed to src/WebPushServiceProvider.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace NotificationChannels\WebPushNotifications;
3+
namespace NotificationChannels\WebPush;
44

55
use Minishlink\WebPush\WebPush;
66
use Illuminate\Support\ServiceProvider;
77

8-
class Provider extends ServiceProvider
8+
class WebPushServiceProvider extends ServiceProvider
99
{
1010
/**
1111
* Bootstrap the application services.
@@ -14,7 +14,7 @@ class Provider extends ServiceProvider
1414
*/
1515
public function boot()
1616
{
17-
$this->app->when(Channel::class)
17+
$this->app->when(WebPushChannel::class)
1818
->needs(WebPush::class)
1919
->give(function () {
2020
return new WebPush([
@@ -43,14 +43,4 @@ protected function definePublishing()
4343
], 'migrations');
4444
}
4545
}
46-
47-
/**
48-
* Register the service provider.
49-
*
50-
* @return void
51-
*/
52-
public function register()
53-
{
54-
//
55-
}
5646
}

0 commit comments

Comments
 (0)