ldc-legaulois/app/Services/Mail/MailTesterService.php
2026-10-19 11:17:25 +02:00

114 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Mail;
use App\Enums\MailToTest;
use App\Enums\Shop\OrderType;
use App\Models\Order;
use App\Models\SellItem;
use App\Notifications\Customer\DestroyCustomer;
use App\Notifications\Customer\DestroyedCustomer;
use App\Notifications\Customer\ResetPassword;
use App\Notifications\Customer\UpdatePassword;
use App\Notifications\Customer\VerifyEmail;
use App\Notifications\Shop\NewOrder;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Override;
class MailTesterService
{
public function send(MailToTest $mailType, Address $recipient, string $firstname, string $lastname): void
{
$notifiable = $this->createDummyNotifiable($recipient, $firstname, $lastname);
$notification = $this->resolveNotification($mailType, $firstname);
$notifiable->notifyNow($notification);
}
private function resolveNotification(MailToTest $mailType, string $firstname): Notification
{
return match ($mailType) {
MailToTest::ValidateAccount => new VerifyEmail($firstname),
MailToTest::ForgottenPassword => new ResetPassword('token-test-12345', $firstname),
MailToTest::UpdatePassword => new UpdatePassword($firstname),
MailToTest::OrderRecap => $this->createNewOrderDummy($firstname),
MailToTest::DestroyCustomer => new DestroyCustomer($firstname),
MailToTest::DestroyedCustomer => new DestroyedCustomer($firstname),
};
}
private function createNewOrderDummy(string $firstname): NewOrder
{
/** @var Order $dummyOrder */
$dummyOrder = Order::factory()->make([
'customer_id' => 1,
'firstname' => $firstname,
'payementType' => OrderType::check,
]);
$dummyItems = SellItem::factory()->count(2)->make()->map(function (SellItem $item) {
$item->setRelation('pivot', (object) ['quantity' => 1]);
return $item;
});
$dummyOrder->setRelation('sellItems', $dummyItems);
return new NewOrder($dummyOrder);
}
private function createDummyNotifiable(Address $sendTo, string $firstname, string $lastname): mixed
{
$username = "{$firstname} {$lastname}";
return new readonly class ($sendTo, $username, $firstname) implements CanResetPassword
{
use Notifiable;
public object $apiData;
public object $api_data;
public function __construct(
public Address $address,
public string $username,
string $firstname,
) {
$this->api_data = (object) ['firstname' => $firstname];
$this->apiData = $this->api_data;
}
#[Override]
public function getEmailForPasswordReset(): string
{
return $this->address->address;
}
#[Override]
public function sendPasswordResetNotification($token): void
{
throw new \BadMethodCallException('Not implemented in testing mock.');
}
public function getKey(): int
{
return 1;
}
public function getEmailForVerification(): string
{
return $this->address->address;
}
public function routeNotificationForMail(): string
{
return $this->address->address;
}
};
}
}