Files
shopit-back/app/Domains/Ticketing/Event/Services/EventAddressService.php

38 lines
1.4 KiB
PHP

<?php
namespace App\Domains\Ticketing\Event\Services;
use App\Domains\Core\Address\Models\Address;
use App\Domains\Ticketing\Event\Models\Event;
class EventAddressService
{
/** Called inside the event update transaction, after validating the legacy location fields. */
public function syncLocation(Event $event, bool $coordinatesProvided = false): void
{
$address = $event->address;
$coordinates = $coordinatesProvided || $address === null ? $event->exact_location : $address->coordinates();
$values = [
'address_text' => $event->location,
'latitude' => $coordinates['latitude'] ?? null,
'longitude' => $coordinates['longitude'] ?? null,
];
if ($address !== null && ! (clone $address)->fill($values)->isDirty()) {
return;
}
// Editing one event must not change the location of another event or a tenant.
if ($address === null || $address->tenants()->exists()
|| $address->events()->where('events.id', '!=', $event->id)->exists()) {
$address = Address::create(['label' => $address?->label ?? 'Ubicación del evento', ...$values]);
$event->address()->associate($address);
$event->save();
} else {
$address->update($values);
}
$event->setRelation('address', $address);
}
}