-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters