Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: agrintals
  5.  * Date: 16/2/18
  6.  * Time: 15:02 PM
  7.  */
  8.  
  9. namespace Dyninno\ReviewsAdminBundle\Service;
  10.  
  11. class CountService // maybe better just include in same REP
  12. {
  13.     private $em;
  14.  
  15.     public function __construct(EntityManagerInterface $em) // em
  16.     {
  17.         $this->em = $em;
  18.     }
  19.  
  20.     // Where Should be Added in RevRepository
  21.  
  22.     /*
  23.     Add New Review - if $params published == 1
  24.  
  25.     ***************************************************************************
  26.  
  27.     EditReview - if previous published was 0 and new is 1 OR if previou published was 1 and new is 0
  28.  
  29.     and IF review is published and there is made changes either in source or IN brands
  30.  
  31.     if there are made changes in source - call count public in sources
  32.  
  33.     if there are made changes in brand - call count public in brand
  34.  
  35.     **************************************************************************
  36.  
  37.     publishUnpublishReviews - call in all cases at the end...
  38.  
  39.     deleteReviews - call in all cases at the end...
  40.  
  41.     */
  42.  
  43.     public function countPublicInBrands(): void
  44.     {
  45.         // select all brands
  46.         // in foreach dynamic query to select count
  47.         // set this count to brand object
  48.         $allBrands = $this->em->getRepository('ReviewsAdminBundle:Brands')->findAll();
  49.  
  50.         foreach ($allBrands as $oneBrand) {
  51.             $queryBuilder = $this->em->createQueryBuilder();
  52.             $countPublished = $queryBuilder->select('count(r.id)') // r or r.id
  53.                         ->from('ReviewsAdminBundle:Reviews', 'r')
  54.                         ->innerJoin('r.brand', 'b')
  55.                         ->where('b.id = :brand')
  56.                         ->andWhere('r.published = 1')
  57.                         ->setParameter('brand', $oneBrand)
  58.                         ->getQuery()
  59.                         ->getResult(); // getSingleScalar or Scalar ?
  60.             $oneBrand->setCountPublished($countPublished);
  61.         }
  62.     }
  63.  
  64.     public function countPublicInGroups(): void
  65.     {
  66.         // select all groups
  67.         // in foreach dynamic query to select count
  68.         // set this count to group object
  69.         $allGroups = $this->em->getRepository('ReviewsAdminBundle:Groups')->findAll();
  70.  
  71.         foreach ($allGroups as $oneGroup) {
  72.             $queryBuilder = $this->em->createQueryBuilder();
  73.             $idList = $this->getIdList($oneGroup);
  74.             $exprSourceIds = $queryBuilder->expr()->in('r.source', $idList);
  75.             $countPublished = $queryBuilder->select('count(r.id)')
  76.                 ->from('ReviewsAdminBundle:Reviews', 'r')
  77.                 ->leftJoin('r.source', 's')
  78.                 ->where($exprSourceIds)
  79.                 ->andWhere('r.published = 1')
  80.                 ->getQuery()
  81.                 ->getResult();
  82.             $oneGroup->setCountPublished($countPublished); 
  83.         }
  84.     }
  85.  
  86.      private function getIdList(Groups $group): array
  87.     {
  88.         $ids = [];
  89.         $groupObject = $this->_em->getRepository('ReviewsAdminBundle:Groups')->findOneBy(['id' => $group]);
  90.        
  91.         foreach ($groupObject->getSourceList() as $source) {
  92.             $ids[] = $source->getId();
  93.         }
  94.  
  95.         return $ids;
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement