src/Controller/DemandChartAction.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\Security\Core\Security;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use App\Entity\Demand;
  7. class DemandChartAction
  8. {
  9.     private $security;
  10.     public function __construct(Security $securityManagerRegistry $doctrine)
  11.     {
  12.         $this->security $security;
  13.         $this->doctrine $doctrine;
  14.     }
  15.     public function __invoke(): JsonResponse
  16.     {
  17.         $user $this->security->getUser();
  18.         $repository $this->doctrine->getRepository(Demand::class);
  19.         if ($this->security->isGranted('ROLE_INSURED')) {
  20.             $insured $user->getInsureds()->first()->getInsured();
  21.         }
  22.         
  23.         $query $repository->createQueryBuilder("d")
  24.             ->select("d.status, count(d.id) as total")
  25.             ->groupBy('d.status')
  26.         ;
  27.         if ($this->security->isGranted('ROLE_INSURED')) {
  28.             $query $query
  29.                 ->andWhere("d.insured = :insured")
  30.                 ->setParameter("insured"$insured)
  31.             ;
  32.         }
  33.         $statuses $query
  34.             ->getQuery()
  35.             ->getResult()
  36.         ;
  37.         if ($this->security->isGranted('ROLE_INSURED')) {
  38.             $count $repository->createQueryBuilder("d")
  39.                 ->select("count(d.id)")
  40.                 ->andWhere("d.insured = :insured")
  41.                 ->setParameter("insured"$insured)
  42.                 ->getQuery()
  43.                 ->getSingleScalarResult()
  44.             ;
  45.         } else {
  46.             $count $repository->count([]);
  47.         }
  48.         $labels = [];
  49.         $data = [];
  50.         foreach ($statuses as $status) {
  51.             $labels[] = $status["status"];
  52.             $data[] = $status["total"];
  53.         }
  54.         
  55.         return new JsonResponse([
  56.             "count" => $count,
  57.             "labels" => $labels,
  58.             "data" => $data
  59.         ]);
  60.     }
  61. }