<?php
namespace App\Activator;
use App\Entity\Feature;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\QueryException;
use Flagception\Activator\FeatureActivatorInterface;
use Flagception\Model\Context;
class AdminActivator implements FeatureActivatorInterface
{
/**
* @var EntityManagerInterface
*/
private $manager;
/**
* @var array
*/
private $features = [];
public function __construct(
EntityManagerInterface $manager
) {
$this->manager = $manager;
}
/**
* Get unique activator name.
*
* @return string
*/
public function getName()
{
return 'admin';
}
/**
* Check if the given feature name is active
* Optional the context object can contain further options to check.
*
* @param string $name
*/
public function isActive($name, Context $context): bool
{
if (!isset($this->getFeatures()[$name]) || !$this->getFeatures()[$name]) {
return false;
}
return true;
}
/**
* @throws QueryException
*/
public function isExternalATSEnabled(): bool
{
$enabledAtsFeatures = $this->manager->getRepository(Feature::class)->findEnabledExternalATSFeatures();
return \count($enabledAtsFeatures) > 0;
}
public function getFeatures(): array
{
if ([] === $this->features) {
$featureCandidates = $this->manager->getRepository(Feature::class)->getAdminActivatorCandidates();
foreach ($featureCandidates as $feature) {
$this->features[$feature['name']] = $feature['active'];
}
}
return $this->features;
}
}