Skip to content

Commit

Permalink
Merge pull request #24 from francoism90/main
Browse files Browse the repository at this point in the history
Add fake argument
  • Loading branch information
freekmurze authored Dec 9, 2024
2 parents 01908ac + 143ee3b commit 97d8586
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Concerns/UsesMailcoachMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\MailcoachMailer\Concerns;

use Spatie\MailcoachMailer\Headers\FakeHeader;
use Spatie\MailcoachMailer\Headers\MailerHeader;
use Spatie\MailcoachMailer\Headers\ReplacementHeader;
use Spatie\MailcoachMailer\Headers\TransactionalMailHeader;
Expand All @@ -12,14 +13,15 @@ trait UsesMailcoachMail
{
private bool $usingMailcoachMail = false;

public function mailcoachMail(string $mailName, array $replacements = [], ?string $mailer = null): self
public function mailcoachMail(string $mailName, array $replacements = [], ?string $mailer = null, ?bool $fake = null): self
{
$this->usingMailcoachMail = true;

$this->html = 'use-mailcoach-mail';

$this->replacing($replacements);
$this->usingMailer($mailer);
$this->faking($fake);

$this->withSymfonyMessage(function (Email $email) use ($mailName) {
$transactionalHeader = new TransactionalMailHeader($mailName);
Expand Down Expand Up @@ -70,6 +72,25 @@ public function replacing(array|string $key, string|array|null $value = null): s
return $this;
}

public function faking(?bool $value): self
{
if (! $value) {
return $this;
}

$this->withSymfonyMessage(function (Email $email) use ($value) {
$fakeHeader = new FakeHeader($value);

if ($email->getHeaders()->has($fakeHeader->getName())) {
$email->getHeaders()->remove($fakeHeader->getName());
}

$email->getHeaders()->add($fakeHeader);
});

return $this;
}

protected function buildSubject($message): self
{
if (! $this->usingMailcoachMail) {
Expand Down
20 changes: 20 additions & 0 deletions src/Notifications/MailcoachMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Arr;
use Spatie\MailcoachMailer\Headers\FakeHeader;
use Spatie\MailcoachMailer\Headers\MailerHeader;
use Spatie\MailcoachMailer\Headers\ReplacementHeader;
use Spatie\MailcoachMailer\Headers\TransactionalMailHeader;
Expand All @@ -15,6 +16,8 @@ class MailcoachMessage extends MailMessage

public array $replacements = [];

public bool $fake = false;

public function usingMail(string $mailName): self
{
$this->mailName = $mailName;
Expand Down Expand Up @@ -65,4 +68,21 @@ public function replacing(array|string $key, string|array|null $value = null): s

return $this;
}

public function faking(bool $value): self
{
$this->fake = $value;

$this->withSymfonyMessage(function (Email $email) use ($value) {
$fakeHeader = new FakeHeader($mailer);

if ($email->getHeaders()->has($fakeHeader->getName())) {
$email->getHeaders()->remove($fakeHeader->getName());
}

$email->getHeaders()->add($fakeHeader);
});

return $this;
}
}

0 comments on commit 97d8586

Please sign in to comment.