Skip to content

Instantly share code, notes, and snippets.

@ismail1432
Last active March 6, 2023 16:48
Show Gist options
  • Save ismail1432/5ea012394ef1e117916db637dff58bcb to your computer and use it in GitHub Desktop.
Save ismail1432/5ea012394ef1e117916db637dff58bcb to your computer and use it in GitHub Desktop.
<?php
namespace App\FeatureFlag\Resolver;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
// In order to tag all classes that implements this interface
#[AutoconfigureTag()]
interface FeatureFlagResolverInterface
{
public function isEnabled(string $name): bool;
public function supports(string $name): bool;
}
// fetch the feature flag in database
final class DatabaseFeatureFlagResolver implements FeatureFlagResolverInterface
{
public function __construct(private readonly FeatureFlagRepository $featureFlagRepository) {}
public function isEnabled(string $name): bool
{
return $this->featureFlagRepository->getByName($name)->isEnabled();
}
public function supports(string $name): bool
{
// Does the feature is in the configuration Database source?
return \in_array($name, DatabaseStorageFeatureFlagEnum::names());
}
}
// fetch the feature flag in Symfony parameter
final class ParameterFeatureFlagResolver implements FeatureFlagResolverInterface
{
public function __construct(private readonly ParameterBagInterface $parameterBag){}
public function isEnabled(string $name): bool
{
return $this->parameterBag->get($name);
}
public function supports(string $name): bool
{
// Does the feature is in the configuration Parameter source?
return \in_array($name, ParameterFeatureFlagEnum::names());
}
}
// return the feature flag depending a period
final class PeriodFeatureFlagResolver implements FeatureFlagResolverInterface
{
public function isEnabled(string $name): bool
{
$now = new \DateTime();
return $now > new \DateTime("24-07-2023 23:00")
&& $now < new \DateTime("25-07-2023 08:00");
}
public function supports(string $name): bool
{
// Does the feature is in the configuration Period source?
return \in_array($name, PeriodFeatureFlagEnum::names(), 1);
}
}
// return the feature flag with random true/false to simulate A/B testing
final class ABTestingFeatureFlagResolver implements FeatureFlagResolverInterface
{
public function isEnabled(string $name): bool
{
return random_int(0, 1);
}
public function supports(string $name): bool
{
// Does the feature is in the configuration A/B testing source?
return \in_array($name, ABTestingFeatureFlagEnum::names());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment