src/Repository/ServerFeature/ServerFeatureStateRepository.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\Repository\ServerFeature;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Slivki\Entity\ServerFeatureState;
  7. use Slivki\Exception\ServerFeatureStateNotFoundException;
  8. use function array_key_exists;
  9. use function count;
  10. final class ServerFeatureStateRepository extends ServiceEntityRepository implements ServerFeatureStateRepositoryInterface
  11. {
  12.     /**
  13.      * @var array<int, ServerFeatureState>
  14.      */
  15.     private array $runtimeCache = [];
  16.     public function __construct(ManagerRegistry $registry)
  17.     {
  18.         parent::__construct($registryServerFeatureState::class);
  19.     }
  20.     public function getById(int $featureStateId): ServerFeatureState
  21.     {
  22.         if (array_key_exists($featureStateId$this->runtimeCache)) {
  23.             return $this->runtimeCache[$featureStateId];
  24.         }
  25.         $queryBuilder $this->createQueryBuilder('sfs');
  26.         $expr $queryBuilder->expr();
  27.         $serverFeatureState $queryBuilder
  28.             ->andWhere($expr->eq('sfs.id'':id'))
  29.             ->setParameter(':id'$featureStateId)
  30.             ->getQuery()
  31.             ->getOneOrNullResult();
  32.         if (!$serverFeatureState instanceof ServerFeatureState) {
  33.             throw new ServerFeatureStateNotFoundException();
  34.         }
  35.         $this->runtimeCache[$featureStateId] = $serverFeatureState;
  36.         return $this->runtimeCache[$featureStateId];
  37.     }
  38.     public function findAll(): array
  39.     {
  40.         if (count($this->runtimeCache) > 0) {
  41.             return $this->runtimeCache;
  42.         }
  43.         $queryBuilder $this->createQueryBuilder('sfs');
  44.         /** @var array<ServerFeatureState> $serverFeatures */
  45.         $serverFeatures $queryBuilder
  46.             ->getQuery()
  47.             ->getResult();
  48.         foreach ($serverFeatures as $serverFeature) {
  49.             $this->runtimeCache[$serverFeature->getId()] = $serverFeature;
  50.         }
  51.         return $this->runtimeCache;
  52.     }
  53. }