src/Controller/BaseController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use App\Entity\Magasin;
  12. use App\Entity\Utilisateur;
  13. abstract class BaseController extends AbstractController
  14. {
  15.     protected Connection $connection;
  16.     protected EntityManagerInterface $manager;
  17.     protected SessionInterface $session;
  18.     protected Request $request;
  19.     protected ?Magasin $magasin;
  20.     protected ?Utilisateur $utilisateur;
  21.     public function __construct(
  22.         RequestStack $request,
  23.         Connection $connection,
  24.         ManagerRegistry $doctrine,
  25.         TokenStorageInterface $tokenStorage
  26.     ) {
  27.         $this->connection $connection;
  28.         $this->manager $doctrine->getManager();
  29.         $this->request $request->getCurrentRequest();
  30.         $this->session $this->request->getSession();
  31.         // Initialize user
  32.         $token $tokenStorage->getToken();
  33.         $this->utilisateur $token $token->getUser() : null;
  34.         
  35.         // Ensure user is not just a string (anonymous)
  36.         if ($this->utilisateur instanceof Utilisateur === false) {
  37.             $this->utilisateur null;
  38.         }
  39.         if(!$this->session->get('magasin') && $this->utilisateur && $this->utilisateur->getDefaultMagasin())
  40.         {
  41.             $this->session->set('magasin'$this->utilisateur->getDefaultMagasin()->getId());
  42.         }
  43.         // Consider lazy-loading the magasin when actually needed
  44.         $magasinId $this->session->get('magasin');
  45.         $this->magasin $magasinId $this->manager->getRepository(Magasin::class)->find($magasinId) : null;
  46.     }
  47. }