src/Cms/TenantBundle/Doctrine/TenantRepository.php line 96

Open in your IDE?
  1. <?php
  2. namespace Cms\TenantBundle\Doctrine;
  3. use Cms\CoreBundle\Model\Search\SearchableTrait;
  4. use Cms\CoreBundle\Util\Doctrine\EntityRepository;
  5. use Cms\DomainBundle\Entity\Domain;
  6. use Cms\TenantBundle\Entity\Tenant;
  7. use Cms\TenantBundle\Model\ProductsBitwise;
  8. use Common\Util\Strings;
  9. /**
  10.  * Class TenantRepository
  11.  * @package Cms\TenantBundle\Doctrine
  12.  *
  13.  * @method Tenant findExact($id)
  14.  * @method array|Tenant[] findAll()
  15.  */
  16. final class TenantRepository extends EntityRepository
  17. {
  18.     use SearchableTrait;
  19.     /**
  20.      * @param ProductsBitwise $products
  21.      * @return array|Tenant[]
  22.      */
  23.     public function findAllWithProducts(ProductsBitwise $products)
  24.     {
  25.         return $this->queryMany(
  26.             $this->createQueryBuilder('tenants')
  27.                 ->andWhere('BIT_AND(tenants.products, :products) > 0')
  28.                 ->setParameter('products'$products->getMask())
  29.                 ->addOrderBy('tenants.name''ASC')
  30.         );
  31.     }
  32.     /**
  33.      * @param string|int $value
  34.      * @return Tenant
  35.      */
  36.     public function findExactByIdOrSlug($value)
  37.     {
  38.         $value strval($value);
  39.         if (preg_match('/^[1-9]\d*$/'$value) === 1) {
  40.             return $this->findExact(intval($value));
  41.         }
  42.         return $this->findExactBySlug($value);
  43.     }
  44.     /**
  45.      * @param string $slug
  46.      * @return Tenant
  47.      * @throws \Exception
  48.      */
  49.     public function findExactBySlug($slug)
  50.     {
  51.         $tenant $this->findOneBySlug($slug);
  52.         if ($tenant === null) {
  53.             throw new \Exception();
  54.         }
  55.         return $tenant;
  56.     }
  57.     /**
  58.      * @param string $host
  59.      * @return Tenant
  60.      * @throws \Exception
  61.      */
  62.     public function findOneByHost($host)
  63.     {
  64.         if (preg_match('/^(.+?)\.([^.]+?\.[^.]+?)$/'$host$matches) !== 1) {
  65.             throw new \Exception();
  66.         }
  67.         $qb $this->_em->createQueryBuilder();
  68.         $qb
  69.             ->select('domain')
  70.             ->from(Domain::class, 'domain')
  71.             ->leftJoin('domain.apex''apex')
  72.             ->andWhere('apex.host = :host')
  73.             ->setParameter('host'$matches[2])
  74.             ->andWhere('domain.name = :name')
  75.             ->setParameter('name'$matches[1]);
  76.         $domain $this->queryOne($qb);/** @var Domain $domain */
  77.         if ($domain === null) {
  78.             return null;
  79.         }
  80.         return $domain->getTenant();
  81.     }
  82.     /**
  83.      * @param string $slug
  84.      * @return Tenant
  85.      */
  86.     public function findOneBySlug($slug)
  87.     {
  88.         return $this->findOneBy(array(
  89.             'slug' => $slug,
  90.         ));
  91.     }
  92.     /**
  93.      * @param string $uid
  94.      * @return Tenant
  95.      */
  96.     public function findOneByUid($uid)
  97.     {
  98.         return $this->findOneBy(array(
  99.             'uid' => $uid,
  100.         ));
  101.     }
  102.     /**
  103.      * @param string $slug
  104.      * @return bool
  105.      */
  106.     public function isSlugTaken($slug)
  107.     {
  108.         $result $this
  109.             ->getEntityManager()
  110.             ->createQuery("
  111.                 SELECT
  112.                     1
  113.                 FROM
  114.                     SharedTenantBundle:Tenant t
  115.                 WHERE
  116.                     t.slug = :slug
  117.             ")
  118.             ->setParameter('slug'$slug)
  119.             ->getScalarResult();
  120.         return (count($result) != 0);
  121.     }
  122.     /**
  123.      * @param string $name
  124.      * @return bool
  125.      */
  126.     public function isNameTaken($name)
  127.     {
  128.         $result $this
  129.             ->getEntityManager()
  130.             ->createQuery("
  131.                 SELECT
  132.                     1
  133.                 FROM
  134.                     SharedTenantBundle:Tenant t
  135.                 WHERE
  136.                     t.name = :name
  137.             ")
  138.             ->setParameter('name'$name)
  139.             ->getScalarResult();
  140.         return (count($result) != 0);
  141.     }
  142.     /**
  143.      * @param string $slug
  144.      * @return Tenant[]
  145.      */
  146.     public function findTenantForView($slug)
  147.     {
  148.         return $this->findOneBy(array(
  149.             'slug' => $slug,
  150.         ));
  151.     }
  152. }