74 lines
1.9 KiB
PHP
74 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Lib\PrivateS3Disk;
|
|
use App\Mail\SendContactMail;
|
|
use Domain\Models\ValueObjects\ContactFormData;
|
|
use Illuminate\Contracts\Mail\Mailer;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Mail\Mailables\Address;
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
|
|
class SendContactMailJob implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
/** 'failed_jobs' after */
|
|
public int $tries = 5;
|
|
|
|
public function __construct(
|
|
public readonly ContactFormData $data,
|
|
public readonly string $recipientEmail,
|
|
public readonly ?string $recipientName = null,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Attempts delays.
|
|
*
|
|
* @return array<int, int>
|
|
*/
|
|
public function backoff(): array
|
|
{
|
|
return [10, 30, 60, 300];
|
|
}
|
|
|
|
public function handle(
|
|
Mailer $mailer,
|
|
StorageService $storageService,
|
|
PrivateS3Disk $privateS3Disk,
|
|
): void {
|
|
$mailer->to(new Address($this->recipientEmail, $this->recipientName))
|
|
->send(new SendContactMail($this->data));
|
|
|
|
$this->cleanupAttachment($storageService, $privateS3Disk);
|
|
}
|
|
|
|
public function failed(\Throwable $exception): void
|
|
{
|
|
$this->cleanupAttachment(
|
|
app()->make(StorageService::class),
|
|
app()->make(PrivateS3Disk::class),
|
|
);
|
|
}
|
|
|
|
private function cleanupAttachment(
|
|
StorageService $storageService,
|
|
PrivateS3Disk $privateS3Disk,
|
|
): void {
|
|
if ($this->data->file === null) {
|
|
return;
|
|
}
|
|
|
|
$filePath = new StorageFilePath($this->data->file->path);
|
|
|
|
if ($storageService->fileExistsOnDisk($filePath, $privateS3Disk->disk)) {
|
|
$storageService->removeFromDisk($filePath, $privateS3Disk->disk);
|
|
}
|
|
}
|
|
}
|