middleware('can:testMails,' . User::class); } public function listMails(): View { return view('bo.mail-testing'); } public function sendMail(SendTestMailRequest $request): RedirectResponse { $firstname = 'Jean'; $lastname = 'Dupuis'; $fullname = "{$firstname} {$lastname}"; $recipient = $request->getToAddress($fullname); $notifiable = $this->generateDummyNotifiable( $recipient, $firstname, $lastname, ); $mailableOrNotification = $this->getMailToSend( $request->getMailToTest(), $firstname, ); if ($this->sendMailToRecipient($mailableOrNotification, $notifiable)) { return back()->with('success', __("Le mail à été envoyé à l'adresse {$recipient->address}")); } return back()->with('error', 'Échec lors de l\'envoi du mail'); } private function sendMailToRecipient(Mailable | BaseNotification $mail, mixed $dummyNotifiable): bool { /** @mixin \Illuminate\Notifications\Notifiable $dummyNotifiable */ if ($mail instanceof Mailable) { return Mail::to($dummyNotifiable->address)->send($mail) !== null; } $dummyNotifiable->notifyNow($mail); return true; } private function getMailToSend(MailToTest $mailToTest, string $firstname): Mailable | BaseNotification { return match ($mailToTest) { MailToTest::ValidateAccount => $this->fromValidateAccountNotification(), MailToTest::ForgottenPassword => $this->fromForgottenPassword(), MailToTest::UpdatePassword => $this->fromUpdatePassword(), MailToTest::OrderRecap => $this->fromNewOrder($firstname), }; } private function generateDummyNotifiable(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() { return $this->address->address; } #[Override] public function sendPasswordResetNotification($token) { throw new \Exception('Not implemented'); } public function getKey(): int { return 1; } public function getEmailForVerification(): string { return $this->address->address; } public function routeNotificationForMail(): string { return $this->address->address; } }; } private function fromValidateAccountNotification(): BaseNotification { return new VerifyEmail(); } private function fromForgottenPassword(): BaseNotification { return new ResetPassword('token'); } private function fromUpdatePassword(): BaseNotification { return new UpdatePassword(); } private function fromNewOrder(string $firstname): BaseNotification { /** @var \App\Models\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); } }