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 ⭐️
PHP attendize-alternativedoor-managementevent-check-inevent-managementevent-registrationevent-ticketingeventbrite-alternativeeventslaravelopen-sourcephpreactself-hostedsell-ticketssell-tickets-onlineticket-sellingticketing-systemtickettailor-alternativetito-alternative
<?php
declare(strict_types=1);
namespace HiEvents\Services\Application\Handlers\EventOccurrence;
use HiEvents\DomainObjects\Generated\EventOccurrenceDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\ProductDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\ProductOccurrenceVisibilityDomainObjectAbstract;
use HiEvents\Exceptions\ResourceNotFoundException;
use HiEvents\Repository\Interfaces\EventOccurrenceRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductOccurrenceVisibilityRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductRepositoryInterface;
use HiEvents\Services\Application\Handlers\EventOccurrence\DTO\UpdateProductVisibilityDTO;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Collection;
use Throwable;
class UpdateProductVisibilityHandler
{
public function __construct(
private readonly ProductOccurrenceVisibilityRepositoryInterface $visibilityRepository,
private readonly ProductRepositoryInterface $productRepository,
private readonly EventOccurrenceRepositoryInterface $occurrenceRepository,
private readonly DatabaseManager $databaseManager,
) {}
/**
* @throws Throwable
*/
public function handle(UpdateProductVisibilityDTO $dto): Collection
{
$occurrence = $this->occurrenceRepository->findFirstWhere([
EventOccurrenceDomainObjectAbstract::ID => $dto->event_occurrence_id,
EventOccurrenceDomainObjectAbstract::EVENT_ID => $dto->event_id,
]);
if (! $occurrence) {
throw new ResourceNotFoundException(
__('Occurrence :id not found for this event', ['id' => $dto->event_occurrence_id])
);
}
$selectedProductIds = array_map('intval', $dto->product_ids);
return $this->databaseManager->transaction(function () use ($dto, $selectedProductIds) {
$this->visibilityRepository->deleteWhere([
ProductOccurrenceVisibilityDomainObjectAbstract::EVENT_OCCURRENCE_ID => $dto->event_occurrence_id,
]);
$allProducts = $this->productRepository->findWhere([
ProductDomainObjectAbstract::EVENT_ID => $dto->event_id,
]);
$allProductIds = $allProducts->map(fn ($product) => $product->getId())->sort()->values()->toArray();
$sortedSelectedProductIds = collect($selectedProductIds)->sort()->values()->toArray();
$invalidIds = array_diff($sortedSelectedProductIds, $allProductIds);
if (! empty($invalidIds)) {
throw new ResourceNotFoundException(
__('One or more product IDs do not belong to this event')
);
}
if ($allProductIds === $sortedSelectedProductIds) {
return collect();
}
foreach (array_unique($selectedProductIds) as $productId) {
$this->visibilityRepository->create([
ProductOccurrenceVisibilityDomainObjectAbstract::EVENT_OCCURRENCE_ID => $dto->event_occurrence_id,
ProductOccurrenceVisibilityDomainObjectAbstract::PRODUCT_ID => $productId,
]);
}
return $this->visibilityRepository->findWhere([
ProductOccurrenceVisibilityDomainObjectAbstract::EVENT_OCCURRENCE_ID => $dto->event_occurrence_id,
]);
});
}
}