]> _ Git - psq.git/commitdiff
async webhook
authorLouis Jeckel <louis.jeckel@outlook.cm>
Tue, 14 Apr 2020 14:43:47 +0000 (16:43 +0200)
committerLouis Jeckel <louis.jeckel@outlook.cm>
Tue, 14 Apr 2020 14:43:47 +0000 (16:43 +0200)
app/Events/DispatchMailgunEvent.php [new file with mode: 0644]
app/Events/MailgunWebhook.php [new file with mode: 0644]
app/Events/ProcessMailgunWebhook.php [deleted file]
app/Http/Controllers/WebhookController.php
app/Listeners/ProcessMailgunWebhook.php [new file with mode: 0644]
app/Providers/EventServiceProvider.php

diff --git a/app/Events/DispatchMailgunEvent.php b/app/Events/DispatchMailgunEvent.php
new file mode 100644 (file)
index 0000000..b7cb510
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+namespace App\Events;
+
+use App\EmailBatch;
+use App\MailgunEvent;
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
+
+class DispatchMailgunEvent implements ShouldBroadcast
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+
+    /**
+     * @var EmailBatch
+     */
+    private $batch_id;
+
+    public $data;
+
+    private $broadcast;
+
+
+    /**
+     * Create a new event instance.
+     *
+     * @param $webhookData
+     */
+    public function __construct($webhookData)
+    {
+        $tags = Arr::get($webhookData, 'event-data.tags');
+
+        $broadcast = in_array(env('APP_ENV'), $tags, true);
+
+
+        /** @todo check webhook key */
+
+        $this->batch_id = Str::after(Arr::first($tags, function($tag) {
+            return Str::startsWith($tag, 'batch_id_');
+        }), 'batch_id_');
+
+        $event = MailgunEvent::updateOrCreate([
+            'event' => Arr::get($webhookData, 'event-data.event'),
+            'recipient' => Arr::get($webhookData, 'event-data.recipient'),
+            'email_batch_id' => $this->batch_id
+        ]);
+
+        $event->touch();
+
+        $this->data = $event;
+    }
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new PrivateChannel('emailBatch.'.$this->batch_id);
+    }
+
+    public function broadcastWhen()
+    {
+        return $this->broadcast;
+    }
+
+    /**
+     * The event's broadcast name.
+     */
+    public function broadcastAs()
+    {
+        return 'mailgun.event';
+    }
+}
diff --git a/app/Events/MailgunWebhook.php b/app/Events/MailgunWebhook.php
new file mode 100644 (file)
index 0000000..a3f4f91
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Events;
+
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class MailgunWebhook
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $webhookData;
+
+    /**
+     * Create a new event instance.
+     *
+     * @param $webhookData
+     */
+    public function __construct($webhookData)
+    {
+        $this->webhookData = $webhookData;
+    }
+
+
+}
diff --git a/app/Events/ProcessMailgunWebhook.php b/app/Events/ProcessMailgunWebhook.php
deleted file mode 100644 (file)
index 9346d65..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-
-namespace App\Events;
-
-use App\EmailBatch;
-use App\MailgunEvent;
-use Illuminate\Broadcasting\Channel;
-use Illuminate\Broadcasting\InteractsWithSockets;
-use Illuminate\Broadcasting\PresenceChannel;
-use Illuminate\Broadcasting\PrivateChannel;
-use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
-use Illuminate\Foundation\Events\Dispatchable;
-use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Arr;
-use Illuminate\Support\Str;
-
-class ProcessMailgunWebhook implements ShouldBroadcast
-{
-    use Dispatchable, InteractsWithSockets, SerializesModels;
-
-
-    /**
-     * @var EmailBatch
-     */
-    private $batch_id;
-
-    public $data;
-
-    private $broadcast;
-
-
-    /**
-     * Create a new event instance.
-     *
-     * @return void
-     */
-    public function __construct($webhookData)
-    {
-        $tags = Arr::get($webhookData, 'event-data.tags');
-
-        $broadcast = in_array(env('APP_ENV'), $tags, true);
-
-
-        /** @todo check webhook key */
-
-        $this->batch_id = Str::after(Arr::first($tags, function($tag) {
-            return Str::startsWith($tag, 'batch_id_');
-        }), 'batch_id_');
-
-        $event = MailgunEvent::updateOrCreate([
-            'event' => Arr::get($webhookData, 'event-data.event'),
-            'recipient' => Arr::get($webhookData, 'event-data.recipient'),
-            'email_batch_id' => $this->batch_id
-        ]);
-
-        $event->touch();
-
-        $this->data = $event;
-    }
-
-    /**
-     * Get the channels the event should broadcast on.
-     *
-     * @return \Illuminate\Broadcasting\Channel|array
-     */
-    public function broadcastOn()
-    {
-        return new PrivateChannel('emailBatch.'.$this->batch_id);
-    }
-
-    public function broadcastWhen()
-    {
-        return $this->broadcast;
-    }
-
-    /**
-     * The event's broadcast name.
-     */
-    public function broadcastAs()
-    {
-        return 'mailgun.event';
-    }
-}
index bb253537bdea345622eca8e11d104717c68245f1..cb802680706c4a54e8c9a7536b52ebb5c31d29c9 100644 (file)
@@ -2,14 +2,15 @@
 
 namespace App\Http\Controllers;
 
-use App\Events\ProcessMailgunWebhook;
+use App\Events\DispatchMailgunEvent;
+use App\Events\MailgunWebhook;
 use Illuminate\Http\Request;
 
 class WebhookController extends Controller
 {
     public function mailgunEvent(Request $request)
     {
-        event(new ProcessMailgunWebhook($request->toArray()));
+        event(new MailgunWebhook($request->toArray()));
 
         return response('ok', 200);
     }
@@ -27,7 +28,7 @@ class WebhookController extends Controller
             ]
         ];
 
-        event(new ProcessMailgunWebhook($data));
+        event(new DispatchMailgunEvent($data));
 
         return response('ok', 200);
 
diff --git a/app/Listeners/ProcessMailgunWebhook.php b/app/Listeners/ProcessMailgunWebhook.php
new file mode 100644 (file)
index 0000000..8b18d7e
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\DispatchMailgunEvent;
+use App\Events\MailgunWebhook;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+
+class ProcessMailgunWebhook implements ShouldQueue
+{
+    /**
+     * Create the event listener.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Handle the event.
+     *
+     * @param  MailgunWebhook  $event
+     * @return void
+     */
+    public function handle(MailgunWebhook $event)
+    {
+        event(new DispatchMailgunEvent($event->webhookData));
+    }
+}
index 45306b96cce94a33fd9cebfce4ed9f1a75413c10..e3c91a04fdf22af78f779073ebce3338b18c79d7 100644 (file)
@@ -6,8 +6,10 @@ use Illuminate\Auth\Events\Registered;
 use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
 use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
 use Illuminate\Support\Facades\Event;
-use App\Events\ProcessMailgunWebhook;
+use App\Events\DispatchMailgunEvent;
 use App\Events\ProcessBatch;
+use App\Events\MailgunWebhook;
+use App\Listeners\ProcessMailgunWebhook;
 
 class EventServiceProvider extends ServiceProvider
 {
@@ -20,7 +22,8 @@ class EventServiceProvider extends ServiceProvider
         Registered::class => [
             SendEmailVerificationNotification::class,
         ],
-        ProcessMailgunWebhook::class => [],
+        MailgunWebhook::class => [ProcessMailgunWebhook::class],
+        DispatchMailgunEvent::class => [],
         ProcessBatch::class => [],
     ];