Created
September 14, 2025 10:03
-
-
Save juanwilde/c7436df02106fcdd7e6da0bf3f2edf56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| public function execute(): void | |
| { | |
| $this->logger->info(sprintf('Providers: %s', iterator_count($this->providers))); | |
| foreach ($this->providers as $provider) { | |
| try { | |
| $events = iterator_to_array($provider->fetchEvents()); | |
| $this->processBatch($events); | |
| } catch (Throwable $e) { | |
| $this->logger->error(sprintf('Provider %s failed with error <%s>', $provider->getName(), $e->getMessage())); | |
| continue; | |
| } | |
| } | |
| } | |
| /** | |
| * @param array<EventDTO> $eventDTOs | |
| */ | |
| private function processBatch(array $eventDTOs): void | |
| { | |
| // Get all external IDs to check for existing events in one query | |
| $externalIds = array_map(fn($dto) => $dto->externalId, $eventDTOs); | |
| $existingEvents = $this->eventRepository->findByExternalIds($externalIds); | |
| // Create a map for quick lookup | |
| $existingMap = []; | |
| foreach ($existingEvents as $event) { | |
| $key = $event->getEventProvider()->value . ':' . $event->getExternalId(); | |
| $existingMap[$key] = $event; | |
| } | |
| foreach ($eventDTOs as $eventDTO) { | |
| $key = $eventDTO->eventProvider->value . ':' . $eventDTO->externalId; | |
| if (isset($existingMap[$key])) { | |
| $existingMap[$key]->updateFromDTO($eventDTO); | |
| } else { | |
| $this->eventRepository->persist(Event::newEventFromDTO($eventDTO)); | |
| } | |
| } | |
| $this->eventRepository->flush(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment