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 ⭐️

1.3 KB
Raw Blame
<?php

namespace HiEvents\Services\Application\Handlers\Webhook;

use HiEvents\DomainObjects\WebhookDomainObject;
use HiEvents\Services\Application\Handlers\Webhook\DTO\CreateWebhookDTO;
use HiEvents\Services\Domain\CreateWebhookService;
use Illuminate\Database\DatabaseManager;
use Throwable;

class CreateWebhookHandler
{
    public function __construct(
        private readonly CreateWebhookService $createWebhookService,
        private readonly DatabaseManager $databaseManager,
    ) {}

    /**
     * @throws Throwable
     */
    public function handle(CreateWebhookDTO $upsertWebhookDTO): mixed
    {
        return $this->databaseManager->transaction(fn () => $this->createWebhook($upsertWebhookDTO));
    }

    private function createWebhook(CreateWebhookDTO $upsertWebhookDTO): WebhookDomainObject
    {
        $webhookDomainObject = (new WebhookDomainObject)
            ->setUrl($upsertWebhookDTO->url)
            ->setEventTypes($upsertWebhookDTO->eventTypes)
            ->setEventId($upsertWebhookDTO->eventId)
            ->setOrganizerId($upsertWebhookDTO->organizerId)
            ->setUserId($upsertWebhookDTO->userId)
            ->setAccountId($upsertWebhookDTO->accountId)
            ->setStatus($upsertWebhookDTO->status->value);

        return $this->createWebhookService->createWebhook($webhookDomainObject);
    }
}