127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Event\Services;
|
|
|
|
use App\Domains\Event\Enums\EventDateChangeType;
|
|
use App\Domains\Event\Models\EventDateChange;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class EventDateNoticeFormatter
|
|
{
|
|
public function __construct(private readonly EventDateTextFormatter $dateTextFormatter) {}
|
|
|
|
/**
|
|
* @param Collection<int, EventDateChange> $changes
|
|
* @return list<array{
|
|
* type: string,
|
|
* change_ids: list<int>,
|
|
* title: string,
|
|
* message: list<array{text: string, bold: bool}>
|
|
* }>
|
|
*/
|
|
public function format(Collection $changes): array
|
|
{
|
|
return collect([
|
|
$this->suspensionNotice(
|
|
$changes->where('change_type', EventDateChangeType::Suspended)
|
|
),
|
|
$this->rescheduleNotice(
|
|
$changes->where('change_type', EventDateChangeType::Rescheduled)
|
|
),
|
|
])->filter()->values()->all();
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, EventDateChange> $changes
|
|
* @return array{type: string, change_ids: list<int>, title: string, message: list<array{text: string, bold: bool}>}|null
|
|
*/
|
|
private function suspensionNotice(Collection $changes): ?array
|
|
{
|
|
$dates = $this->formatDates($changes, 'previous_date');
|
|
|
|
if ($dates === null) {
|
|
return null;
|
|
}
|
|
|
|
$plural = $changes->count() > 1;
|
|
|
|
return [
|
|
'type' => EventDateChangeType::Suspended->value,
|
|
'change_ids' => $this->changeIds($changes),
|
|
'title' => $plural ? 'FECHAS CANCELADAS!' : 'FECHA CANCELADA!',
|
|
'message' => [
|
|
['text' => $plural ? 'Las fechas del ' : 'La fecha del ', 'bold' => false],
|
|
['text' => $dates, 'bold' => true],
|
|
['text' => $plural ? ' han sido canceladas.' : ' ha sido cancelada.', 'bold' => false],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, EventDateChange> $changes
|
|
* @return array{type: string, change_ids: list<int>, title: string, message: list<array{text: string, bold: bool}>}|null
|
|
*/
|
|
private function rescheduleNotice(Collection $changes): ?array
|
|
{
|
|
$changes = $changes->whereNotNull('new_date');
|
|
$sourceDates = $this->formatDates($changes, 'previous_date');
|
|
$destinationDates = $this->formatDates($changes, 'new_date');
|
|
|
|
if ($sourceDates === null || $destinationDates === null) {
|
|
return null;
|
|
}
|
|
|
|
$plural = $changes->count() > 1;
|
|
$message = [
|
|
['text' => $plural ? 'Las fechas del ' : 'La fecha del ', 'bold' => false],
|
|
['text' => $sourceDates, 'bold' => true],
|
|
[
|
|
'text' => $plural ? ' han sido reprogramadas para el ' : ' ha sido reprogramada para el ',
|
|
'bold' => false,
|
|
],
|
|
['text' => $destinationDates, 'bold' => true],
|
|
];
|
|
|
|
if ($plural) {
|
|
$message[] = ['text' => ', ', 'bold' => false];
|
|
$message[] = ['text' => 'respectivamente', 'bold' => true];
|
|
}
|
|
|
|
$message[] = ['text' => '.', 'bold' => false];
|
|
|
|
return [
|
|
'type' => EventDateChangeType::Rescheduled->value,
|
|
'change_ids' => $this->changeIds($changes),
|
|
'title' => $plural ? 'FECHAS REPROGRAMADAS!' : 'FECHA REPROGRAMADA!',
|
|
'message' => $message,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, EventDateChange> $changes
|
|
*/
|
|
private function formatDates(Collection $changes, string $attribute): ?string
|
|
{
|
|
return $this->dateTextFormatter->formatForSentence(
|
|
$changes
|
|
->pluck($attribute)
|
|
->filter()
|
|
->map(fn ($date): string => $date->format('Y-m-d'))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, EventDateChange> $changes
|
|
* @return list<int>
|
|
*/
|
|
private function changeIds(Collection $changes): array
|
|
{
|
|
return $changes
|
|
->pluck('id')
|
|
->filter(fn ($id): bool => $id !== null)
|
|
->map(fn ($id): int => (int) $id)
|
|
->values()
|
|
->all();
|
|
}
|
|
}
|