Skip to content

Commit

Permalink
Merge branch 'PPF-57/sent-reminder-email' into PPF-53/extra-mails
Browse files Browse the repository at this point in the history
  • Loading branch information
Koen Eelen committed Sep 19, 2024
2 parents 6016d24 + d57345d commit bc4ab5c
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 62 deletions.
15 changes: 0 additions & 15 deletions app/Domain/Mail/Addresses.php

This file was deleted.

7 changes: 1 addition & 6 deletions app/Domain/Mail/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ private function getFrom(): Address
return new Address(config('mail.from.address'), config('mail.from.name'));
}

private function getAddresses(Contact $contact): Addresses
{
return new Addresses([new Address($contact->email, trim($contact->firstName . ' ' . $contact->lastName))]);
}

private function getIntegrationVariables(Contact $contact, Integration $integration): array
{
return [
Expand Down Expand Up @@ -126,7 +121,7 @@ public function sendMail(Integration $integration, Template $template): void
foreach ($this->getUniqueContactsWithPreferredContactType($integration, ContactType::Technical) as $contact) {
$this->mailer->send(
$this->getFrom(),
$this->getAddresses($contact),
new Address($contact->email, trim($contact->firstName . ' ' . $contact->lastName)),
$template->id,
$template->subject,
$this->getIntegrationVariables($contact, $integration)
Expand Down
2 changes: 1 addition & 1 deletion app/Domain/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface Mailer
{
public function send(Address $from, Addresses $to, int $templateId, string $subject, array $variables = []): void;
public function send(Address $from, Address $to, int $templateId, string $subject, array $variables = []): void;
}
1 change: 1 addition & 0 deletions app/Mails/MailJet/MailjetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ final class MailjetConfig
public const API_KEY = 'mailjet.api.key';
public const API_SECRET = 'mailjet.api.secret';
public const SANDBOX_MODE = 'mailjet.sandbox_mode';
public const SANDBOX_ALLOWED_DOMAINS = 'mailjet.sandbox_allowed_domains';
public const MAILJET_TEMPLATES = 'mailjet.templates';
}
36 changes: 8 additions & 28 deletions app/Mails/MailJet/MailjetMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Mails\MailJet;

use App\Domain\Mail\Addresses;
use App\Domain\Mail\Mailer;
use App\Domain\Mail\MailNotSend;
use Mailjet\Client;
Expand All @@ -17,22 +16,25 @@
public function __construct(
private Client $client,
private LoggerInterface $logger,
private bool $sandboxMode
private SandboxMode $sandboxMode,
) {

}

public function send(Address $from, Addresses $to, int $templateId, string $subject, array $variables = []): void
public function send(Address $from, Address $to, int $templateId, string $subject, array $variables = []): void
{
$body = [
'SandboxMode' => $this->sandboxMode,
'SandboxMode' => $this->sandboxMode->forAddress($to),
'Messages' => [
[
'From' => [
'Email' => $from->getAddress(),
'Name' => $from->getName(),
],
'To' => $this->buildReceiverList($to),
'To' => [
'Email' => $to->getAddress(),
'Name' => $to->getName(),
],
'TemplateID' => $templateId,
'TemplateLanguage' => true,
'Subject' => $subject,
Expand All @@ -52,29 +54,7 @@ public function send(Address $from, Addresses $to, int $templateId, string $subj
}

$this->logger->info(
sprintf(
'Mail "%s" sent successfully to %s',
$subject,
implode(', ', $to->map(
function (Address $address) {
return $address->getAddress();
}
)->toArray())
)
sprintf('Mail "%s" sent successfully to %s', $subject, $to->getAddress())
);
}

private function buildReceiverList(Addresses $addresses): array
{
$output = [];

foreach ($addresses as $address) {
$output[] = [
'Email' => $address->getAddress(),
'Name' => $address->getName(),
];
}

return $output;
}
}
29 changes: 29 additions & 0 deletions app/Mails/MailJet/SandboxMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Mails\MailJet;

use Symfony\Component\Mime\Address;

final readonly class SandboxMode
{
public function __construct(private bool $sandboxMode, private array $allowedDomains)
{
}

public function forAddress(Address $address): bool
{
if (!$this->sandboxMode) {
return false;
}

foreach ($this->allowedDomains as $domain) {
if (str_ends_with($address->getAddress(), $domain)) {
return false;
}
}

return true;
}
}
7 changes: 5 additions & 2 deletions app/Mails/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Domain\Mail\MailManager;
use App\Mails\MailJet\MailjetConfig;
use App\Mails\MailJet\MailjetMailer;
use App\Mails\MailJet\SandboxMode;
use App\Mails\Template\Templates;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
Expand All @@ -38,11 +39,13 @@ public function register(): void
['version' => 'v3.1']
),
$this->app->get(LoggerInterface::class),
config(MailjetConfig::SANDBOX_MODE)
new SandboxMode(
config(MailjetConfig::SANDBOX_MODE),
config(MailjetConfig::SANDBOX_ALLOWED_DOMAINS)
)
);
});


$this->app->singleton(MailManager::class, function () {
return new MailManager(
$this->app->get(Mailer::class),
Expand Down
6 changes: 3 additions & 3 deletions app/Mails/Template/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
*/
final class Templates extends Collection
{
public static function build(array $mails): self
public static function build(array $templates): self
{
$collection = new self();

foreach ($mails as $type => $config) {
$collection->put($type, new Template($type, (int)$config['id'], $config['enabled'], $config['subject']));
foreach ($templates as $type => $template) {
$collection->put($type, new Template($type, (int)$template['id'], $template['enabled'], $template['subject']));
}

return $collection;
Expand Down
2 changes: 1 addition & 1 deletion config/mailjet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);


use App\Mails\Template\TemplateName;

return [
'enabled' => env('MAILJET_TRANSACTIONAL_EMAILS_ENABLED', false),
'sandbox_mode' => env('MAILJET_SANDBOX_MODE', true),
'sandbox_allowed_domains' => array_map(static fn ($value) => trim($value), explode(',', env('MAILJET_SANDBOX_ALLOWED_DOMAINS', ''))),
'api' => [
'key' => env('MAILJET_API_KEY'),
'secret' => env('MAILJET_API_SECRET'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public static function dataProviderGetIntegrationsThatHaveNotBeenActivatedYet():
return [
'Should not be selected: wrong type' => [
IntegrationType::EntryApi,
IntegrationStatus::Active,
IntegrationStatus::Draft,
Carbon::now()->subYears(2),
null,
true,
Expand Down
6 changes: 1 addition & 5 deletions tests/Domain/Mail/MailManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use App\Domain\Integrations\IntegrationStatus;
use App\Domain\Integrations\IntegrationType;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Domain\Mail\Addresses;
use App\Domain\Mail\Mailer;
use App\Domain\Mail\MailManager;
use App\Mails\Template\TemplateName;
Expand Down Expand Up @@ -150,10 +149,7 @@ public function testSendMail(
->method('send')
->with(
new Address(config('mail.from.address'), config('mail.from.name')),
$this->callback(function (Addresses $addresses) use (&$currentEmail) {
/** @var Address $address */
$address = $addresses->first();

$this->callback(function (Address $address) use (&$currentEmail) {
if (!isset($this->contacts[$address->getAddress()])) {
return false;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Mails/MailJet/SandboxModeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tests\Mails\MailJet;

use App\Mails\MailJet\SandboxMode;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;

final class SandboxModeTest extends TestCase
{
#[DataProvider('sandboxModeProvider')]
public function test_get_sandbox_mode(bool $sandboxMode, array $allowedDomains, Address $to, bool $expected): void
{
$this->assertSame(
$expected,
(new SandboxMode($sandboxMode, $allowedDomains))->forAddress($to)
);
}

public static function sandboxModeProvider(): array
{
$allowedDomains = ['publiq.be', '[email protected]'];

return [
'Sandbox mode is off and domain is not allowed, should return false' => [
false,
$allowedDomains,
new Address('[email protected]'),
false,
],
'Sandbox mode is off and domain is allowed, should return false' => [
false,
$allowedDomains,
new Address('[email protected]'),
false,
],
'Sandbox mode is on, but domain is allowed, should return false' => [
true,
$allowedDomains,
new Address('[email protected]'),
false,
],
'Sandbox mode is on, and domain is not allowed, should return true' => [
true,
$allowedDomains,
new Address('[email protected]'),
true,
],
'Sandbox mode is on, entire email matches, should return false' => [
true,
$allowedDomains,
new Address('[email protected]'),
false,
],
];
}
}

0 comments on commit bc4ab5c

Please sign in to comment.