src/Controller/DashboardController.php line 45

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Job\Job;
  4. use App\Entity\Search\Search;
  5. use App\Entity\Search\Status;
  6. use App\Entity\Stat;
  7. use App\Entity\User;
  8. use App\Repository\Job\JobRepository;
  9. use App\Repository\SearchRepository;
  10. use DateTimeImmutable;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Attribute\IsGranted;
  16. class DashboardController extends AbstractController
  17. {
  18.     private const CHART_COLORS = [
  19.         '#3B7DDD',
  20.         '#7a828a',
  21.         '#1cbb8c',
  22.         '#17a2b8',
  23.         '#fcb92c',
  24.         '#dc3545',
  25.         '#3538DC ',
  26.         '#2a830a ',
  27.         '#ec6c10',
  28.         '#2b2b2b',
  29.     ];
  30.     public function __construct(
  31.         private readonly EntityManagerInterface $em,
  32.         private readonly JobRepository          $jobRepo,
  33.         private readonly SearchRepository       $searchRepo,
  34.     )
  35.     {
  36.     }
  37.     #[Route('/dashboard'name'app_dashboard')]
  38.     #[Route('/'name'app_dashboard_base')]
  39.     #[IsGranted('ROLE_USER')]
  40.     public function index(): Response
  41.     {
  42.         $searches $this->em->getRepository(Search::class)->createQueryBuilder('s')
  43.             ->andWhere('s.status = :status')
  44.             ->setParameter('status'Status::LIVE)
  45.             ->addOrderBy('s.createdAt''DESC')
  46.             ->setMaxResults(10)
  47.             ->getQuery()
  48.             ->getResult();
  49.         if ($this->isGranted('ROLE_ADMIN')) {
  50.             $cityStats $this->jobRepo->getCityStats();
  51.             $counts $this->getCounts([Search::class, User::class, Job::class]);
  52.             $mostSearchWords $this->searchRepo->getMostSearchWords(5);
  53.             $lastSearches $this->searchRepo->getLastSearches(5);
  54.             $searchCountsByUser $this->searchRepo->getSearchesByUser();
  55.             $topUsers array_slice($searchCountsByUser010);
  56.             $flopUsers array_slice(array_reverse($searchCountsByUser), 010);
  57.             foreach ($topUsers as $key => &$u) {
  58.                 $u['color'] = self::CHART_COLORS[$key];
  59.             }
  60.             unset($u);
  61.             foreach ($flopUsers as $key => &$u) {
  62.                 $u['color'] = self::CHART_COLORS[$key];
  63.             }
  64.             unset($u);
  65.             $lastJobs $this->jobRepo->getLastJobs(5);
  66.         }
  67.         return $this->render('dashboard/dashboard.html.twig', [
  68.             'pagename' => 'Dashboard',
  69.             'searches' => $searches,
  70.             'city_stats' => $cityStats ?? [],
  71.             'counts' => $counts ?? [],
  72.             'last_searches' => $lastSearches ?? [],
  73.             'last_jobs' => $lastJobs ?? [],
  74.             'most_searchwords' => $mostSearchWords ?? [],
  75.             'top_users' => $topUsers ?? [],
  76.             'flop_users' => $flopUsers ?? [],
  77.             'error' => '',
  78.         ]);
  79.     }
  80.     private function getCounts(array $classes): array
  81.     {
  82.         $counts = [];
  83.         $stats $this->em->getRepository(Stat::class)->findAll();
  84.         $today = (new DateTimeImmutable())->setTime(00);
  85.         foreach ($stats as $stat) {
  86.             $stats[$stat->getName()] = $stat;
  87.         }
  88.         foreach ($classes as $class) {
  89.             $tmp explode('\\'$class);
  90.             $key strtolower(array_pop($tmp));
  91.             $stat $stats[$key] ?? (new Stat())->setName($key);
  92.             $count $this->em->getRepository($class)
  93.                 ->createQueryBuilder('a')
  94.                 ->select('COUNT(a.id) as count')
  95.                 ->getQuery()
  96.                 ->getArrayResult();
  97.             $prog '+0,00';
  98.             $val array_pop($count)['count'];
  99.             if (null !== $stat->getValue() && (int)$stat->getValue() !== (int)$val) {
  100.                 if ($stat->getValue() < $val) {
  101.                     $prog '+' . (number_format((($val $stat->getValue()) / $stat->getValue()) * 1002','));
  102.                 } else {
  103.                     $prog '-' . (number_format((($stat->getValue() - $val) / $val) * 1002','));
  104.                 }
  105.             }
  106.             $counts[$key] = [
  107.                 'value' => $val,
  108.                 'prog' => $prog '%'
  109.             ];
  110.             if ('Mon' === $today->format('D') && (!isset($stats[$key]) || $stat->getLastUpdate() !== $today)) {
  111.                 $stat
  112.                     ->setValue($val)
  113.                     ->setLastUpdate($today);
  114.                 $this->em->persist($stat);
  115.                 $this->em->flush();
  116.             }
  117.         }
  118.         return $counts;
  119.     }
  120. }