Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

2.2 KB
Raw Blame
<?php

declare(strict_types=1);

namespace HiEvents\Services\Application\Handlers\EventOccurrence;

use HiEvents\DomainObjects\Enums\EventType;
use HiEvents\DomainObjects\Generated\EventDomainObjectAbstract;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Services\Application\Handlers\EventOccurrence\DTO\GenerateOccurrencesDTO;
use HiEvents\Services\Domain\Event\EventOccurrenceGeneratorService;
use HiEvents\Services\Infrastructure\Lock\TransactionLockService;
use Illuminate\Database\DatabaseManager;
use Throwable;

class GenerateOccurrencesFromRuleHandler
{
    public function __construct(
        private readonly EventOccurrenceGeneratorService $generatorService,
        private readonly EventRepositoryInterface $eventRepository,
        private readonly DatabaseManager $databaseManager,
        private readonly TransactionLockService $transactionLockService,
    ) {}

    /**
     * @throws Throwable
     */
    public function handle(GenerateOccurrencesDTO $dto): void
    {
        $this->databaseManager->transaction(function () use ($dto) {
            $this->transactionLockService->lockEvent($dto->event_id);

            $event = $this->eventRepository->findByIdLocked($dto->event_id);

            $rule = $this->mergeLiveExclusions($dto->recurrence_rule, $event->getRecurrenceRule() ?? []);

            $this->eventRepository->updateFromArray(
                id: $event->getId(),
                attributes: [
                    EventDomainObjectAbstract::RECURRENCE_RULE => $rule,
                    EventDomainObjectAbstract::TYPE => EventType::RECURRING->name,
                ],
            );

            $event->setRecurrenceRule($rule);

            $this->generatorService->generate($event, $rule);
        });
    }

    private function mergeLiveExclusions(array $submittedRule, array $liveRule): array
    {
        foreach (['excluded_occurrences', 'excluded_dates', 'additional_dates'] as $key) {
            $merged = array_values(array_unique(array_merge(
                $liveRule[$key] ?? [],
                $submittedRule[$key] ?? [],
            ), SORT_REGULAR));

            if ($merged !== []) {
                $submittedRule[$key] = $merged;
            }
        }

        return $submittedRule;
    }
}