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

declare(strict_types=1);

namespace HiEvents\Services\Domain\FeatureFlag;

use HiEvents\DomainObjects\Enums\FeatureFlag;
use HiEvents\Enterprise\Licensing\LicenceService;
use HiEvents\Exceptions\FeatureNotEnabledException;
use HiEvents\Repository\DTO\AccountFeatureFlagDTO;
use HiEvents\Repository\Interfaces\FeatureFlagRepositoryInterface;
use Illuminate\Support\Collection;

class FeatureFlagService
{
    public function __construct(
        private readonly FeatureFlagRepositoryInterface $featureFlagRepository,
        private readonly LicenceService $licenceService,
    ) {}

    /**
     * @return FeatureFlag[]
     */
    public function configurableFlags(): array
    {
        return $this->licenceService->isCloud() ? FeatureFlag::cases() : [];
    }

    /**
     * @return Collection<string, AccountFeatureFlagDTO>
     */
    public function getFlagsForAccount(int $accountId): Collection
    {
        $configurable = $this->configurableFlags();

        if ($configurable === []) {
            return collect();
        }

        $stored = $this->featureFlagRepository->findForAccount($accountId)->keyBy('key');

        return collect($configurable)->mapWithKeys(static fn (FeatureFlag $flag) => [
            $flag->value => $stored->get($flag->value) ?? new AccountFeatureFlagDTO(
                key: $flag->value,
                enabledByDefault: false,
                override: null,
            ),
        ]);
    }

    /**
     * @return array<string, bool>
     */
    public function getEnabledFlagsForAccount(int $accountId): array
    {
        $configured = $this->getFlagsForAccount($accountId);

        return collect(FeatureFlag::cases())
            ->mapWithKeys(fn (FeatureFlag $flag) => [
                $flag->value => $this->isLicensed($flag) && ($configured->get($flag->value)?->isEnabled() ?? true),
            ])
            ->all();
    }

    public function isEnabled(FeatureFlag $flag, int $accountId): bool
    {
        return $this->getEnabledFlagsForAccount($accountId)[$flag->value];
    }

    /**
     * @throws FeatureNotEnabledException
     */
    public function assertEnabled(FeatureFlag $flag, int $accountId): void
    {
        if (! $this->isEnabled($flag, $accountId)) {
            throw new FeatureNotEnabledException;
        }
    }

    private function isLicensed(FeatureFlag $flag): bool
    {
        return $this->licenceService->allows($flag->licensedFeature());
    }
}