172 lines
5.1 KiB
PHP
172 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\MailToTest;
|
|
use App\Enums\Shop\OrderType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\SendTestMailRequest;
|
|
use App\Models\Order;
|
|
use App\Models\SellItem;
|
|
use App\Models\User;
|
|
use App\Notifications\Customer\ResetPassword;
|
|
use App\Notifications\Customer\UpdatePassword;
|
|
use App\Notifications\Customer\VerifyEmail;
|
|
use App\Notifications\Shop\NewOrder;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Contracts\Auth\CanResetPassword;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Contracts\Mail\Mailable;
|
|
use Illuminate\Mail\Mailables\Address;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Notifications\Notification as BaseNotification;
|
|
use Override;
|
|
|
|
class MailTesterController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->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);
|
|
}
|
|
}
|