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

declare(strict_types=1);

namespace HiEvents\Services\Application\Handlers\Admin\FeatureFlags;

use HiEvents\DomainObjects\Enums\FeatureFlag;
use HiEvents\DomainObjects\Generated\FeatureFlagDomainObjectAbstract;
use HiEvents\Exceptions\ResourceNotFoundException;
use HiEvents\Repository\DTO\FeatureFlagSummaryDTO;
use HiEvents\Repository\Interfaces\FeatureFlagRepositoryInterface;

class UpdateFeatureFlagHandler
{
    public function __construct(
        private readonly FeatureFlagRepositoryInterface $featureFlagRepository,
    ) {}

    /**
     * @throws ResourceNotFoundException
     */
    public function handle(FeatureFlag $flag, bool $enabledByDefault): FeatureFlagSummaryDTO
    {
        $this->featureFlagRepository->updateWhere(
            attributes: [FeatureFlagDomainObjectAbstract::ENABLED_BY_DEFAULT => $enabledByDefault],
            where: [FeatureFlagDomainObjectAbstract::KEY => $flag->value],
        );

        return $this->featureFlagRepository->findAllWithOverrideCounts()
            ->firstWhere('key', $flag->value)
            ?? throw new ResourceNotFoundException(__('Feature flag not found'));
    }
}