src/Controller/DashboardController.php line 45
<?phpnamespace App\Controller;use App\Entity\Job\Job;use App\Entity\Search\Search;use App\Entity\Search\Status;use App\Entity\Stat;use App\Entity\User;use App\Repository\Job\JobRepository;use App\Repository\SearchRepository;use DateTimeImmutable;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Security\Http\Attribute\IsGranted;class DashboardController extends AbstractController{private const CHART_COLORS = ['#3B7DDD','#7a828a','#1cbb8c','#17a2b8','#fcb92c','#dc3545','#3538DC ','#2a830a ','#ec6c10','#2b2b2b',];public function __construct(private readonly EntityManagerInterface $em,private readonly JobRepository $jobRepo,private readonly SearchRepository $searchRepo,){}#[Route('/dashboard', name: 'app_dashboard')]#[Route('/', name: 'app_dashboard_base')]#[IsGranted('ROLE_USER')]public function index(): Response{$searches = $this->em->getRepository(Search::class)->createQueryBuilder('s')->andWhere('s.status = :status')->setParameter('status', Status::LIVE)->addOrderBy('s.createdAt', 'DESC')->setMaxResults(10)->getQuery()->getResult();if ($this->isGranted('ROLE_ADMIN')) {$cityStats = $this->jobRepo->getCityStats();$counts = $this->getCounts([Search::class, User::class, Job::class]);$mostSearchWords = $this->searchRepo->getMostSearchWords(5);$lastSearches = $this->searchRepo->getLastSearches(5);$searchCountsByUser = $this->searchRepo->getSearchesByUser();$topUsers = array_slice($searchCountsByUser, 0, 10);$flopUsers = array_slice(array_reverse($searchCountsByUser), 0, 10);foreach ($topUsers as $key => &$u) {$u['color'] = self::CHART_COLORS[$key];}unset($u);foreach ($flopUsers as $key => &$u) {$u['color'] = self::CHART_COLORS[$key];}unset($u);$lastJobs = $this->jobRepo->getLastJobs(5);}return $this->render('dashboard/dashboard.html.twig', ['pagename' => 'Dashboard','searches' => $searches,'city_stats' => $cityStats ?? [],'counts' => $counts ?? [],'last_searches' => $lastSearches ?? [],'last_jobs' => $lastJobs ?? [],'most_searchwords' => $mostSearchWords ?? [],'top_users' => $topUsers ?? [],'flop_users' => $flopUsers ?? [],'error' => '',]);}private function getCounts(array $classes): array{$counts = [];$stats = $this->em->getRepository(Stat::class)->findAll();$today = (new DateTimeImmutable())->setTime(0, 0);foreach ($stats as $stat) {$stats[$stat->getName()] = $stat;}foreach ($classes as $class) {$tmp = explode('\\', $class);$key = strtolower(array_pop($tmp));$stat = $stats[$key] ?? (new Stat())->setName($key);$count = $this->em->getRepository($class)->createQueryBuilder('a')->select('COUNT(a.id) as count')->getQuery()->getArrayResult();$prog = '+0,00';$val = array_pop($count)['count'];if (null !== $stat->getValue() && (int)$stat->getValue() !== (int)$val) {if ($stat->getValue() < $val) {$prog = '+' . (number_format((($val - $stat->getValue()) / $stat->getValue()) * 100, 2, ','));} else {$prog = '-' . (number_format((($stat->getValue() - $val) / $val) * 100, 2, ','));}}$counts[$key] = ['value' => $val,'prog' => $prog . '%'];if ('Mon' === $today->format('D') && (!isset($stats[$key]) || $stat->getLastUpdate() !== $today)) {$stat->setValue($val)->setLastUpdate($today);$this->em->persist($stat);$this->em->flush();}}return $counts;}}