@@ -13,13 +13,20 @@ class WebPushChannel
13
13
*/
14
14
protected $ webPush ;
15
15
16
+ /**
17
+ * @var \NotificationChannels\WebPush\ReportHandlerInterface
18
+ */
19
+ protected $ reportHandler ;
20
+
16
21
/**
17
22
* @param \Minishlink\WebPush\WebPush $webPush
23
+ * @param \NotificationChannels\WebPush\ReportHandlerInterface $webPush
18
24
* @return void
19
25
*/
20
- public function __construct (WebPush $ webPush )
26
+ public function __construct (WebPush $ webPush, ReportHandlerInterface $ reportHandler )
21
27
{
22
28
$ this ->webPush = $ webPush ;
29
+ $ this ->reportHandler = $ reportHandler ;
23
30
}
24
31
25
32
/**
@@ -31,50 +38,61 @@ public function __construct(WebPush $webPush)
31
38
*/
32
39
public function send ($ notifiable , Notification $ notification )
33
40
{
41
+ /** @var \Illuminate\Database\Eloquent\Collection $subscriptions */
34
42
$ subscriptions = $ notifiable ->routeNotificationFor ('WebPush ' );
35
43
36
- if (! $ subscriptions || $ subscriptions -> isEmpty ( )) {
44
+ if (empty ( $ subscriptions )) {
37
45
return ;
38
46
}
39
47
40
- /** @var \NotificationChannels\WebPush\WebPushMessage $webPushMessage */
41
- $ webPushMessage = $ notification ->toWebPush ($ notifiable , $ notification );
42
- $ options = $ webPushMessage -> getOptions ( );
43
- $ payload = json_encode ( $ webPushMessage -> toArray () );
48
+ /** @var \NotificationChannels\WebPush\WebPushMessage $message */
49
+ $ message = $ notification ->toWebPush ($ notifiable , $ notification );
50
+ $ payload = json_encode ( $ message -> toArray () );
51
+ $ options = $ message -> getOptions ( );
44
52
45
- $ subscriptions ->each (function (PushSubscription $ pushSubscription ) use ($ payload , $ options ) {
53
+ /** @var \NotificationChannels\WebPush\PushSubscription $subscription */
54
+ foreach ($ subscriptions as $ subscription ) {
46
55
$ this ->webPush ->sendNotification (new Subscription (
47
- $ pushSubscription ->endpoint ,
48
- $ pushSubscription ->public_key ,
49
- $ pushSubscription ->auth_token ,
50
- $ pushSubscription ->content_encoding
56
+ $ subscription ->endpoint ,
57
+ $ subscription ->public_key ,
58
+ $ subscription ->auth_token ,
59
+ $ subscription ->content_encoding
51
60
), $ payload , false , $ options );
52
- });
61
+ }
53
62
54
63
$ reports = $ this ->webPush ->flush ();
55
64
56
- $ this ->deleteInvalidSubscriptions ($ reports , $ subscriptions );
65
+ $ this ->handleReports ($ reports , $ subscriptions );
57
66
}
58
67
59
68
/**
60
- * @param \Minishlink\WebPush\MessageSentReport[] $reports
69
+ * Handle the reports.
70
+ *
71
+ * @param \Generator $reports
61
72
* @param \Illuminate\Database\Eloquent\Collection $subscriptions
62
73
* @return void
63
74
*/
64
- protected function deleteInvalidSubscriptions ($ reports , $ subscriptions )
75
+ protected function handleReports ($ reports , $ subscriptions )
65
76
{
77
+ /** @var \Minishlink\WebPush\MessageSentReport $report */
66
78
foreach ($ reports as $ report ) {
67
- if (is_null ( $ report) || $ report -> isSuccess ( )) {
68
- continue ;
79
+ if ($ report && $ subscription = $ this -> findSubscription ( $ subscriptions , $ report )) {
80
+ $ this -> reportHandler -> handleReport ( $ report , $ subscription ) ;
69
81
}
82
+ }
83
+ }
70
84
71
- /* @var \Minishlink\WebPush\MessageSentReport $report */
72
- $ subscriptions ->each (function ($ subscription ) use ($ report ) {
73
- if ($ subscription ->endpoint === $ report ->getEndpoint ()) {
74
- logger ()->warning ('deleting subscription cause of ' .$ report ->getReason ());
75
- $ subscription ->delete ();
76
- }
77
- });
85
+ /**
86
+ * @param \Illuminate\Database\Eloquent\Collection $subscriptions
87
+ * @param \Minishlink\WebPush\MessageSentReport $report
88
+ * @return void
89
+ */
90
+ protected function findSubscription ($ subscriptions , $ report )
91
+ {
92
+ foreach ($ subscriptions as $ subscription ) {
93
+ if ($ subscription ->endpoint === $ report ->getEndpoint ()) {
94
+ return $ subscription ;
95
+ }
78
96
}
79
97
}
80
98
}
0 commit comments