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.4 KB
Raw Blame
<?php

namespace HiEvents\Jobs\Event;

use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\Exceptions\EventStatisticsVersionMismatchException;
use HiEvents\Services\Domain\EventStatistics\EventStatisticsIncrementService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Throwable;

class UpdateEventStatisticsJob implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected OrderDomainObject $order;

    public int $tries = 5;

    public int $backoff = 10; // seconds

    public int $uniqueFor = 60; // seconds

    public function __construct(OrderDomainObject $order)
    {
        $this->order = $order;
    }

    public function uniqueId(): string
    {
        return (string) $this->order->getId();
    }

    /**
     * @throws EventStatisticsVersionMismatchException|Throwable
     */
    public function handle(EventStatisticsIncrementService $service): void
    {
        $service->incrementForOrder($this->order);
    }

    public function failed(Throwable $exception): void
    {
        logger()?->error('Failed to update event statistics', [
            'order' => $this->order->toArray(),
            'exception' => $exception,
        ]);
    }
}