Skip to content

Commit

Permalink
Feature/soft bounces (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Apr 28, 2023
1 parent b1897a8 commit cd99419
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"php": "^8.1",
"illuminate/support": "^9.0|^10.0",
"symfony/sendgrid-mailer": "^6.0",
"spatie/laravel-mailcoach": "^6.0"
"spatie/laravel-mailcoach": "^6.18"
},
"require-dev": {
"ext-json": "*",
Expand Down
19 changes: 19 additions & 0 deletions src/Enums/BounceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\MailcoachSendgridFeedback\Enums;

/** reference: https://docs.sendgrid.com/for-developers/tracking-events/event */
enum BounceType: string
{
case Deferred = 'Deferred';
case Bounce = 'Bounce';
case Blocked = 'Blocked';

public static function softBounces(): array
{
return [
self::Deferred->value,
self::Blocked->value,
];
}
}
2 changes: 2 additions & 0 deletions src/SendgridEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Spatie\MailcoachSendgridFeedback\SendgridEvents\OtherEvent;
use Spatie\MailcoachSendgridFeedback\SendgridEvents\PermanentBounceEvent;
use Spatie\MailcoachSendgridFeedback\SendgridEvents\SendgridEvent;
use Spatie\MailcoachSendgridFeedback\SendgridEvents\SoftBounceEvent;

class SendgridEventFactory
{
Expand All @@ -16,6 +17,7 @@ class SendgridEventFactory
ComplaintEvent::class,
OpenEvent::class,
PermanentBounceEvent::class,
SoftBounceEvent::class,
];

public static function createForPayload(array $payload): SendgridEvent
Expand Down
24 changes: 24 additions & 0 deletions src/SendgridEvents/SoftBounceEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Spatie\MailcoachSendgridFeedback\SendgridEvents;

use Illuminate\Support\Arr;
use Spatie\Mailcoach\Domain\Shared\Models\Send;
use Spatie\MailcoachSendgridFeedback\Enums\BounceType;

class SoftBounceEvent extends SendgridEvent
{
public function canHandlePayload(): bool
{
return in_array($this->event, BounceType::softBounces(), true);
}

public function handle(Send $send)
{
if (Arr::get($this->payload, 'email') !== $send->subscriber->email) {
return;
}

$send->registerBounce($this->getTimestamp(), softBounce: true);
}
}
53 changes: 53 additions & 0 deletions tests/SendgridEvents/SoftBounceEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Spatie\MailcoachSendgridFeedback\Tests\SendgridEvents;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Spatie\Mailcoach\Database\Factories\SendFactory;
use Spatie\Mailcoach\Domain\Audience\Models\Subscriber;
use Spatie\Mailcoach\Domain\Campaign\Events\SoftBounceRegisteredEvent;
use Spatie\MailcoachSendgridFeedback\SendgridEvents\SoftBounceEvent;
use Spatie\MailcoachSendgridFeedback\Tests\TestCase;

class SoftBounceEventTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function it_can_handle_a_soft_bounce_event()
{
Event::fake();

$event = new SoftBounceEvent([
'event' => 'Blocked',
'type' => 'blocked',
'email' => '[email protected]',
'timestamp' => 1610000000,
]);

$this->assertTrue($event->canHandlePayload());

$send = SendFactory::new()
->for(Subscriber::factory()->state(['email' => '[email protected]']))
->create();

$event->handle($send);

Event::assertDispatched(SoftBounceRegisteredEvent::class);
}

/**
* @test
*/
public function it_cannot_handle_soft_bounces()
{
Event::fake();

$event = new SoftBounceEvent([
'event' => 'Bounce',
]);

$this->assertFalse($event->canHandlePayload());
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Spatie\MailcoachMailgunFeedback\MailcoachMailgunFeedbackServiceProvider;
use Spatie\MailcoachPostmarkFeedback\MailcoachPostmarkFeedbackServiceProvider;
use Spatie\MailcoachSendgridFeedback\MailcoachSendgridFeedbackServiceProvider;
use Spatie\MailcoachSendinblueFeedback\MailcoachSendinblueFeedbackServiceProvider;
use Spatie\MailcoachSesFeedback\MailcoachSesFeedbackServiceProvider;

class TestCase extends Orchestra
Expand All @@ -37,6 +38,7 @@ protected function getPackageProviders($app)
MailcoachSesFeedbackServiceProvider::class,
MailcoachSendgridFeedbackServiceProvider::class,
MailcoachPostmarkFeedbackServiceProvider::class,
MailcoachSendinblueFeedbackServiceProvider::class,
MailcoachEditorServiceProvider::class,
MailcoachServiceProvider::class,
];
Expand Down

0 comments on commit cd99419

Please sign in to comment.