src/Repository/CityRepository.php line 55

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-19
  5.  * Time: 23:05
  6.  */
  7. namespace App\Repository;
  8. use App\Entity\Location\City;
  9. use App\Repository\ReadModel\CityReadModel;
  10. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. class CityRepository extends ServiceEntityRepository
  13. {
  14.     use EntityIteratorTrait;
  15.     public function __construct(ManagerRegistry $registry)
  16.     {
  17.         parent::__construct($registry, City::class);
  18.     }
  19.     public function findExisting(int $id): ?City
  20.     {
  21.         return $this->findExistingByIds([$id])[$id] ?? null;
  22.     }
  23.     public function findExistingByIds(array $ids): array
  24.     {
  25.         if (!$ids) {
  26.             return [];
  27.         }
  28.         return $this->createQueryBuilder('city', 'city.id')
  29.             ->andWhere('city.id IN (:ids)')
  30.             ->setParameter('ids', array_values(array_unique($ids)))
  31.             ->getQuery()
  32.             ->setCacheable(false)
  33.             ->getResult();
  34.     }
  35.     public function ofUriIdentity(string $uriIdentity): ?City
  36.     {
  37.         $qb = $this->createQueryBuilder('city')
  38.             ->andWhere('city.uriIdentity = :uri')
  39.             ->setParameter('uri', $uriIdentity)
  40.             ->setMaxResults(1)
  41.         ;
  42.         $query = $qb->getQuery()
  43.             ->useResultCache(true)
  44.             ->setResultCacheLifetime(3600)
  45.         ;
  46.         return  $query->getOneOrNullResult();
  47.     }
  48.     public function ofName(string $name, string $locale = 'ru'): ?City
  49.     {
  50.         $qb = $this->createQueryBuilder('city')
  51.             ->andWhere('JSON_EXTRACT(city.name, :json_path) = :name')
  52.             ->setParameter('json_path', '$.'.$locale)
  53.             ->setParameter('name', $name)
  54.             ->setMaxResults(1)
  55.         ;
  56.         $query = $qb->getQuery();
  57.         return $query->getOneOrNullResult();
  58.     }
  59.     public function cityCountries(): array
  60.     {
  61.         $qb = $this->createQueryBuilder('city')
  62.             ->distinct(true)
  63.             ->select('city.countryCode')
  64.         ;
  65.         $query = $qb->getQuery()
  66.             ->useResultCache(true)
  67.             ->setResultCacheLifetime(3600)
  68.         ;
  69.         $result = $query->getScalarResult();
  70.         return array_column($result, 'countryCode');
  71.     }
  72.     public function getAll(): array
  73.     {
  74.         $qb = $this->createQueryBuilder('city')
  75.             ->select('city.id, city.name, city.uriIdentity, city.countryCode')
  76.         ;
  77.         $result = array_map(function($profile): CityReadModel {
  78.             return $this->hydrateCityRow($profile);
  79.         }, $qb->getQuery()->getResult());
  80.         return $result;
  81.     }
  82.     public function findAll(?string $region = null): array
  83.     {
  84.         $criteria = [];
  85.         if (null !== $region) {
  86.             $criteria['region'] = $region;
  87.         }
  88.         return $this->findBy($criteria);
  89.     }
  90.     private function hydrateCityRow(array $row): CityReadModel
  91.     {
  92.         $view = new CityReadModel();
  93.         $view->id = $row['id'];
  94.         $view->name = $row['name'];
  95.         $view->uriIdentity = $row['uriIdentity'];
  96.         $view->countryCode = $row['countryCode'];
  97.         return $view;
  98.     }
  99. }