src/Controller/DashboardController.php line 16

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Entreprise;
  4. use App\Repository\EntrepriseRepository;
  5. use App\Repository\SubscriptionRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class DashboardController extends AbstractController
  11. {
  12.     #[Route('/admin'name'app_dashboard')]
  13.     public function index(
  14.         SubscriptionRepository $subscriptionRepository,
  15.         EntrepriseRepository $entrepriseRepository,
  16.         EntityManagerInterface $entityManager
  17.     ): Response {
  18.         $currentUser $this->getUser();
  19.         if (!$currentUser) {
  20.             return $this->redirectToRoute('app_login');
  21.         }
  22.         $this->syncExpiredSubscriptions($subscriptionRepository$entityManager);
  23.         $entreprises $entrepriseRepository->findBy([], ['id' => 'DESC'], 10);
  24.         $activeEntreprises count(array_filter(
  25.             $entreprises,
  26.             static fn (Entreprise $entreprise): bool => $entreprise->isStatus() === true
  27.         ));
  28.         $allEntrepriseCount count($entreprises);
  29.         $subscriptionCount $subscriptionRepository->countPaidSubscriptionsForAdmin($currentUser);
  30.         return $this->render('dashboard/index.html.twig', [
  31.             'entreprises' => $entreprises,
  32.             'activeEntreprise' => $activeEntreprises,
  33.             'allEntrepriseCount' => $allEntrepriseCount,
  34.             'subscriptionCount' => $subscriptionCount
  35.         ]);
  36.     }
  37.     private function syncExpiredSubscriptions(
  38.         SubscriptionRepository $subscriptionRepository,
  39.         EntityManagerInterface $entityManager
  40.     ): void
  41.     {
  42.         $now = new \DateTimeImmutable();
  43.         $expiredSubscriptions $subscriptionRepository->findExpiredActiveSubscriptions($now);
  44.         if (!$expiredSubscriptions) {
  45.             return;
  46.         }
  47.         $entreprisesToCheck = [];
  48.         foreach ($expiredSubscriptions as $subscription) {
  49.             $subscription->setStatus(false);
  50.             $entreprise $subscription->getEntreprise();
  51.             if ($entreprise) {
  52.                 $key $entreprise->getId() ?? spl_object_id($entreprise);
  53.                 $entreprisesToCheck[$key] = $entreprise;
  54.             }
  55.         }
  56.         foreach ($entreprisesToCheck as $entreprise) {
  57.             if (!$subscriptionRepository->hasActiveSubscriptionForEntreprise($entreprise$now)) {
  58.                 $entreprise->setStatus(false);
  59.             }
  60.         }
  61.         $entityManager->flush();
  62.     }
  63. }