src/Cms/DomainBundle/Doctrine/DomainRepository.php line 93

Open in your IDE?
  1. <?php
  2. namespace Cms\DomainBundle\Doctrine;
  3. use Cms\CoreBundle\Util\Doctrine\EntityRepository;
  4. use Cms\DomainBundle\Entity\Domain;
  5. use Cms\DomainBundle\Entity\SslCertificate;
  6. use Cms\DomainBundle\Entity\SslCertificates\CustomSslCertificate;
  7. use Cms\DomainBundle\Entity\SslCertificates\LetsEncryptSslCertificate;
  8. /**
  9.  * Class DomainRepository
  10.  * @package Cms\DomainBundle\Doctrine
  11.  *
  12.  * @method Domain|null find($id, $lockMode = null, $lockVersion = null);
  13.  * @method Domain findExact($id);
  14.  * @method Domain[] findAll();
  15.  */
  16. final class DomainRepository extends EntityRepository
  17. {
  18.     /**
  19.      * @param int|string $value
  20.      * @return Domain
  21.      * @throws \Exception
  22.      */
  23.     public function findExactByIdOrHost($value)
  24.     {
  25.         $domain $this->findByIdOrHost($value);
  26.         if (empty($domain)) {
  27.             throw new \Exception();
  28.         }
  29.         return $domain;
  30.     }
  31.     /**
  32.      * @param int|string $value
  33.      * @return Domain|null
  34.      * @throws \Exception
  35.      */
  36.     public function findByIdOrHost($value)
  37.     {
  38.         if (preg_match('/^[1-9]\d*$/'$value) === 1) {
  39.             $domain $this->find($value);
  40.         } else {
  41.             $domain $this->findOneByHost($value);
  42.         }
  43.         return $domain;
  44.     }
  45.     /**
  46.      * @param string $host
  47.      * @return Domain
  48.      * @throws \Exception
  49.      */
  50.     public function findOneByHost($host)
  51.     {
  52.         // need to break the hostname apart
  53.         if (preg_match('/^(.+?)\.(.+)$/'$host$matches) !== 1) {
  54.             throw new \Exception(sprintf(
  55.                 'Unsupported hostname lookup for: %s',
  56.                 $host
  57.             ));
  58.         }
  59.         // create builder
  60.         $qb $this->createQueryBuilder('domain');
  61.         $qb
  62.             // need to join on apexes
  63.             ->leftJoin('domain.apex''apex')
  64.             // check case of empty subdomain with full apex
  65.             ->orWhere($qb->expr()->andX(
  66.                 'domain.name = :fullDomain',
  67.                 'apex.host = :fullApex'
  68.             ))
  69.             // check for when first piece is a subdomain and rest is apex
  70.             ->orWhere($qb->expr()->andX(
  71.                 'domain.name = :shortDomain',
  72.                 'apex.host = :shortApex'
  73.             ))
  74.             // parameters for everything
  75.             ->setParameters(array(
  76.                 'fullDomain' => '',
  77.                 'fullApex' => $host,
  78.                 'shortDomain' => $matches[1],
  79.                 'shortApex' => $matches[2],
  80.             ))
  81.         ;
  82.         // done
  83.         return $this->queryOne($qb);
  84.     }
  85.     public function findWithSorting()
  86.     {
  87.         $qb $this->createQueryBuilder('domain');
  88.         $qb
  89.             ->leftJoin('domain.apex''apex')
  90.             ->leftJoin('domain.certificate''certificate')
  91.             ->leftJoin('domain.tenant''tenant')
  92.             ->addOrderBy('tenant.name''ASC')
  93.             ->addOrderBy('apex.host''ASC')
  94.             ->addOrderBy('domain.name''ASC')
  95.         ;
  96.         return $this->queryMany($qb);
  97.     }
  98. }