src/Repository/CompanyRepository.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Company;
  4. use App\Services\MediaPathResolver;
  5. use App\Services\ToolsService;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. /**
  9.  * @method Company|null find($id, $lockMode = null, $lockVersion = null)
  10.  * @method Company|null findOneBy(array $criteria, array $orderBy = null)
  11.  * @method Company[]    findAll()
  12.  * @method Company[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  13.  */
  14. class CompanyRepository extends ServiceEntityRepository
  15. {
  16.     /**
  17.      * @var MediaPathResolver
  18.      */
  19.     private $mediaPathResolver;
  20.     /**
  21.      * @var ToolsService
  22.      */
  23.     private $tools;
  24.     public function __construct(ManagerRegistry $registryMediaPathResolver $mediaPathResolverToolsService $tools)
  25.     {
  26.         parent::__construct($registryCompany::class);
  27.         $this->mediaPathResolver $mediaPathResolver;
  28.         $this->tools $tools;
  29.     }
  30.     /**
  31.      * Generate sql query
  32.      */
  33.     private function getQuery(array $criterias, array $orderBy null$limit null$offset null$groups null)
  34.     {
  35.         $pathMedia $this->mediaPathResolver->getMediaPathPrefix();
  36.         $sql "SELECT
  37.             C.id, 
  38.             C.name,
  39.             C.address_line AS addressLine,
  40.             C.city,
  41.             C.zip_code AS zipCode,
  42.             C.country,
  43.             C.manager_first_name AS managerFirstName,
  44.             C.manager_last_name AS managerLastName,
  45.             C.manager_function AS managerFunction,
  46.             C.manager_phone AS managerPhone,
  47.             C.manager_email AS managerEmail,
  48.             C.siren,
  49.             C.status,
  50.             C.active,
  51.             C.subscription_start AS subscriptionStart,
  52.             C.subscription_end AS subscriptionEnd,
  53.             C.users_count AS usersCount,
  54.             C.messages,
  55.             C.max_users AS maxUsers,
  56.             C.plan,
  57.             C.slug,
  58.             C.ips,
  59.             C.domain,
  60.             C.hourly_restriction AS hourlyRestriction,
  61.             C.code,
  62.             C.customizable,
  63.             C.cms,
  64.             C.cms_img_id AS cmsImgId,
  65.             C.cms_title AS cmsTitle,
  66.             C.cms_text AS cmsText,
  67.             C.cms_cta_text AS cmsCtaText,
  68.             C.cms_cta_url AS cmsCtaUrl,
  69.             CONCAT('{$pathMedia}', LOGO.file_path) AS logoUrl 
  70.             FROM company AS C
  71.             
  72.             LEFT JOIN media_object AS LOGO ON LOGO.id = C.logo_id ";
  73.         // WHERE
  74.         if(!empty($criterias)) {
  75.             foreach($criterias as $key => $value) {
  76.                 $condition = (str_contains($sql"WHERE")) ? "AND " "WHERE ";
  77.                 $sql .= (intval($value)) ? "{$condition} C.{$this->tools->toSnakeCase($key)} = {$value} " "{$condition} C.{$this->tools->toSnakeCase($key)} LIKE '%{$value}%' ";
  78.             }
  79.         }
  80.         // ORDER BY
  81.         if(isset($orderBy) && is_array($orderBy)) {
  82.             $sql .= "ORDER BY ";
  83.             foreach($orderBy as $key => $value) {
  84.                 $condition = ($key !== array_key_first($orderBy)) ? ",""";
  85.                 $sql .= "{$condition} C.{$this->tools->toSnakeCase($key)} {$value} ";
  86.             }
  87.         }
  88.         // LIMIT
  89.         if(isset($limit)) {
  90.             $sql .= "LIMIT {$limit}";
  91.         }
  92.         return $sql;
  93.     }
  94.     public function getCompanyByCodeInNativeSql(array $criterias, array $orderBy null$limit null$offset null$groups null)
  95.     {
  96.         $sql $this->getQuery($criterias$orderBy$limit$offset$groups);
  97.         $sql "SELECT C.id, 
  98.             C.name, 
  99.             C.code, 
  100.             C.status,
  101.             C.active,
  102.             C.subscription_start AS subscriptionStart,
  103.             C.subscription_end AS subscriptionEnd,
  104.             C.users_count AS usersCount,
  105.             C.max_users AS maxUsers
  106.             FROM company AS C ";
  107.         // WHERE
  108.         if(!empty($criterias)) {
  109.             foreach($criterias as $key => $value) {
  110.                 $condition = (str_contains($sql"WHERE")) ? "AND " "WHERE ";
  111.                 $sql .= (intval($value)) ? "{$condition} C.{$this->tools->toSnakeCase($key)} = {$value} " "{$condition} C.{$this->tools->toSnakeCase($key)} LIKE '%{$value}%' ";
  112.             }
  113.         }
  114.         return $this->tools->getQueryResults($sqltrue);
  115.     }
  116.     public function getCompanyByIdInNativeSql(int $id$groups null)
  117.     {
  118.         $sql $this->getQuery(['id' => $id], nullnullnull$groups);
  119.         return $this->tools->getQueryResults($sqltrue);
  120.     }
  121.     
  122.     public function getCompaniesInNativeSql(array $criterias, array $orderBy null$limit null$offset null$groups null)
  123.     {
  124.         $sql $this->getQuery($criterias$orderBy$limit$offset$groups);
  125.         return $this->tools->getQueryResults($sqlfalse);
  126.     }
  127.     // /**
  128.     //  * @return Company[] Returns an array of Company objects
  129.     //  */
  130.     /*
  131.     public function findByExampleField($value)
  132.     {
  133.         return $this->createQueryBuilder('c')
  134.             ->andWhere('c.exampleField = :val')
  135.             ->setParameter('val', $value)
  136.             ->orderBy('c.id', 'ASC')
  137.             ->setMaxResults(10)
  138.             ->getQuery()
  139.             ->getResult()
  140.         ;
  141.     }
  142.     */
  143.     /*
  144.     public function findOneBySomeField($value): ?Company
  145.     {
  146.         return $this->createQueryBuilder('c')
  147.             ->andWhere('c.exampleField = :val')
  148.             ->setParameter('val', $value)
  149.             ->getQuery()
  150.             ->getOneOrNullResult()
  151.         ;
  152.     }
  153.     */
  154.     
  155. }