src/Controller/DashboardController.php line 16
<?phpnamespace App\Controller;use App\Entity\Entreprise;use App\Repository\EntrepriseRepository;use App\Repository\SubscriptionRepository;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;class DashboardController extends AbstractController{#[Route('/admin', name: 'app_dashboard')]public function index(SubscriptionRepository $subscriptionRepository,EntrepriseRepository $entrepriseRepository,EntityManagerInterface $entityManager): Response {$currentUser = $this->getUser();if (!$currentUser) {return $this->redirectToRoute('app_login');}$this->syncExpiredSubscriptions($subscriptionRepository, $entityManager);$entreprises = $entrepriseRepository->findBy([], ['id' => 'DESC'], 10);$activeEntreprises = count(array_filter($entreprises,static fn (Entreprise $entreprise): bool => $entreprise->isStatus() === true));$allEntrepriseCount = count($entreprises);$subscriptionCount = $subscriptionRepository->countPaidSubscriptionsForAdmin($currentUser);return $this->render('dashboard/index.html.twig', ['entreprises' => $entreprises,'activeEntreprise' => $activeEntreprises,'allEntrepriseCount' => $allEntrepriseCount,'subscriptionCount' => $subscriptionCount]);}private function syncExpiredSubscriptions(SubscriptionRepository $subscriptionRepository,EntityManagerInterface $entityManager): void{$now = new \DateTimeImmutable();$expiredSubscriptions = $subscriptionRepository->findExpiredActiveSubscriptions($now);if (!$expiredSubscriptions) {return;}$entreprisesToCheck = [];foreach ($expiredSubscriptions as $subscription) {$subscription->setStatus(false);$entreprise = $subscription->getEntreprise();if ($entreprise) {$key = $entreprise->getId() ?? spl_object_id($entreprise);$entreprisesToCheck[$key] = $entreprise;}}foreach ($entreprisesToCheck as $entreprise) {if (!$subscriptionRepository->hasActiveSubscriptionForEntreprise($entreprise, $now)) {$entreprise->setStatus(false);}}$entityManager->flush();}}