events/Classes/Service/DestinationDataImportService/LocationAssignment.php

47 lines
1.2 KiB
PHP
Raw Normal View History

2022-08-02 15:56:18 +02:00
<?php
declare(strict_types=1);
namespace WerkraumMedia\Events\Service\DestinationDataImportService;
2022-08-02 15:56:18 +02:00
use WerkraumMedia\Events\Domain\Model\Location;
use WerkraumMedia\Events\Domain\Repository\LocationRepository;
2022-08-02 15:56:18 +02:00
final class LocationAssignment
2022-08-02 15:56:18 +02:00
{
public function __construct(
private readonly LocationRepository $repository
2022-08-02 15:56:18 +02:00
) {
}
public function getLocation(array $event): ?Location
{
$newLocation = new Location(
$event['name'] ?? '',
$event['street'] ?? '',
$event['zip'] ?? '',
$event['city'] ?? '',
$event['district'] ?? '',
$event['country'] ?? '',
$event['phone'] ?? '',
(string)($event['geo']['main']['latitude'] ?? ''),
(string)($event['geo']['main']['longitude'] ?? ''),
2022-08-02 15:56:18 +02:00
-1
);
if ($newLocation->isValid() === false) {
return null;
}
$existingLocation = $this->repository->findOneByGlobalId($newLocation->getGlobalId());
if ($existingLocation === null) {
return $newLocation;
}
$existingLocation->updateFromLocation($newLocation);
return $existingLocation;
2022-08-02 15:56:18 +02:00
}
}