forked from ope-tech/laravel-ses
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add callback on init message (#29)
* Add initMessageCallback and move shared methods in a trait class * Update SesMailerInterface.php * Add test * Update `SesMail` facade by adding all available methods in the docblock
- Loading branch information
Showing
6 changed files
with
132 additions
and
101 deletions.
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
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
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,74 @@ | ||
<?php | ||
|
||
namespace Juhasev\LaravelSes; | ||
|
||
use Closure; | ||
use Illuminate\Support\Carbon; | ||
use Juhasev\LaravelSes\Contracts\SentEmailContract; | ||
use Juhasev\LaravelSes\Exceptions\LaravelSesTooManyRecipientsException; | ||
use Juhasev\LaravelSes\Factories\Events\SesSentEvent; | ||
use Symfony\Component\Mime\Email; | ||
|
||
/** | ||
* @method getBatchId() | ||
* @mixin TrackingTrait | ||
*/ | ||
trait SesMailerTrait | ||
{ | ||
private ?Closure $initMessageCallback = null; | ||
|
||
/** | ||
* Init message (this is always called) | ||
* Creates database entry for the sent email | ||
* | ||
* @param Email $message | ||
* @return SentEmailContract | ||
* @throws LaravelSesTooManyRecipientsException | ||
*/ | ||
public function initMessage(Email $message): SentEmailContract | ||
{ | ||
$this->checkNumberOfRecipients($message); | ||
|
||
$sentEmailModel = ModelResolver::get('SentEmail')::create([ | ||
'message_id' => $message->generateMessageId(), | ||
'email' => $message->getTo()[0]->getAddress(), | ||
'batch_id' => $this->getBatchId(), | ||
'sent_at' => Carbon::now(), | ||
'delivery_tracking' => $this->deliveryTracking, | ||
'complaint_tracking' => $this->complaintTracking, | ||
'bounce_tracking' => $this->bounceTracking | ||
]); | ||
|
||
if (($callback = $this->initMessageCallback) !== null) { | ||
$callback($sentEmailModel); | ||
} | ||
|
||
return $sentEmailModel; | ||
} | ||
|
||
/** | ||
* Check message recipient for tracking | ||
* Open tracking etc won't work if emails are sent to more than one recipient at a time | ||
* | ||
* @param Email $message | ||
* @throws LaravelSesTooManyRecipientsException | ||
*/ | ||
protected function checkNumberOfRecipients(Email $message): void | ||
{ | ||
if (sizeOf($message->getTo()) > 1) { | ||
throw new LaravelSesTooManyRecipientsException("Tried to send to too many emails only one email may be set"); | ||
} | ||
} | ||
|
||
public function useInitMessageCallback(Closure $callback): self | ||
{ | ||
$this->initMessageCallback = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
protected function sendEvent(SentEmailContract $sentEmail): void | ||
{ | ||
event(new SesSentEvent($sentEmail)); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Juhasev\LaravelSes\Tests\Unit; | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use Juhasev\LaravelSes\Contracts\SentEmailContract; | ||
use Juhasev\LaravelSes\Exceptions\LaravelSesTooManyRecipientsException; | ||
use Juhasev\LaravelSes\Facades\SesMail; | ||
use Juhasev\LaravelSes\Factories\Events\SesSentEvent; | ||
|
@@ -11,7 +12,7 @@ | |
|
||
class SesMailerTest extends UnitTestCase | ||
{ | ||
public function testSendEmailEventIsSent() | ||
public function testSendEmailEventIsSent(): void | ||
{ | ||
SesMail::fake(); | ||
Event::fake(); | ||
|
@@ -25,7 +26,7 @@ public function testSendEmailEventIsSent() | |
SesMail::assertSent(TestMailable::class); | ||
} | ||
|
||
public function testExceptionIsThrownWhenTryingToSendToMoreThanOnePerson() | ||
public function testExceptionIsThrownWhenTryingToSendToMoreThanOnePerson(): void | ||
{ | ||
$this->expectException(LaravelSesTooManyRecipientsException::class); | ||
|
||
|
@@ -37,7 +38,7 @@ public function testExceptionIsThrownWhenTryingToSendToMoreThanOnePerson() | |
])->send(new TestMailable()); | ||
} | ||
|
||
public function testTrackingSettingsAreSetCorrectly() | ||
public function testTrackingSettingsAreSetCorrectly(): void | ||
{ | ||
SesMail::enableOpenTracking() | ||
->enableLinkTracking() | ||
|
@@ -85,4 +86,26 @@ public function testTrackingSettingsAreSetCorrectly() | |
'complaintTracking' => false, | ||
], SesMail::trackingSettings()); | ||
} | ||
|
||
public function testSendEmailWithInitMessageCallback(): void | ||
{ | ||
SesMail::fake(); | ||
|
||
$customObject = new CustomObject(); | ||
|
||
$this->assertNull($customObject->messageId); | ||
|
||
SesMail::enableAllTracking() | ||
->useInitMessageCallback( | ||
static fn (SentEmailContract $model) => $customObject->messageId = $model->getMessageId() | ||
) | ||
->to('[email protected]') | ||
->send(new TestMailable()); | ||
|
||
$this->assertNotNull($customObject->messageId); | ||
} | ||
} | ||
|
||
class CustomObject { | ||
public ?string $messageId = null; | ||
} |