src/Controller/AppController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notifications;
  4. use App\Entity\Permission;
  5. use App\Entity\Product;
  6. use App\Entity\Stock;
  7. use App\Entity\Synchro;
  8. use App\Entity\User;
  9. use App\Repository\ProductRepository;
  10. use App\Repository\StockRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use phpDocumentor\Reflection\Types\Boolean;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use function PHPUnit\Framework\throwException;
  17. class AppController extends AbstractController
  18. {
  19.     private $em;
  20.     public function __construct(EntityManagerInterface $entityManager)
  21.     {
  22.         $this->em $entityManager;
  23.     }
  24.     /**
  25.      * @Route("/", name="home")
  26.      */
  27.     public function index(): Response
  28.     {
  29.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  30.         return $this->redirectToRoute('app_checkout', []);
  31.     }
  32.     public function renderWithParams(string $view, array $params): Response
  33.     {
  34.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  35.         if($this->getUser() != null){
  36.             /** @var User $user */
  37.             $user $this->em->getRepository(User::class)->findOneBy(['username' => $this->getUser()->getUserIdentifier()]);
  38.         }else{
  39.             /** @var User $user */
  40.             $user $this->em->getRepository(User::class)->find(5);
  41.         }
  42.         $notifications $this->getNotifications();
  43.         $synch $this->lastSynchro();
  44.         $shops $user->getUserShops()->toArray();
  45.         $currentShop $user->getCurrentshop() ? $user->getCurrentshop() : $shops[0];
  46.         $rended_params array_merge([
  47.             'user' => $user,
  48.             'permissions' => $this->getPermissions($user->getProfile()->getProfilePermissions()->toArray()),
  49.             'profile' => $user->getUserIdentifier(),
  50.             'userShops' => $shops,
  51.             'currentShop' => $currentShop,
  52.             'lastSynch' => $synch['lastSynch'],
  53.             'nextSynch' => $synch['nextSynch'],
  54.             'notifications' => $notifications
  55.         ], $params);
  56.         return $this->render($view$rended_params);
  57.     }
  58.     public function grantAccess(string $acessUser $user): Boolean
  59.     {
  60.         $permissions $user->getProfile()->getProfilePermissions();
  61.         foreach ($permissions as $permission) if($permission == $acess){
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66.     /**
  67.      * @param array $profilePermissions
  68.      * @return array
  69.      */
  70.     private function getPermissions(array $profilePermissions){
  71.         $permissions = [];
  72.         /** @var Permission $permission */
  73.         foreach ($profilePermissions as $permission){
  74.             $permissions[] = $permission->getLabel();
  75.         }
  76.         return $permissions;
  77.     }
  78.     /**
  79.      * @return array
  80.      */
  81.     private function getAlerts(){
  82.         $alerts = [];
  83.         $seuil = [];
  84.         $expiration = [];
  85.         $seuil $this->em->getRepository(Stock::class)->findAll();
  86.         $expiration $this->em->getRepository(Stock::class)->findAll();
  87.         $alerts array_merge($alerts$seuil);
  88.         $alerts array_merge($alerts$expiration);
  89.         return $alerts;
  90.     }
  91.     private function lastSynchro(){
  92.         /** @var Synchro $synch */
  93.         $synch $this->em->getRepository(Synchro::class)->getLastSynchro()[0];
  94.         $synch $this->em->getRepository(Synchro::class)->getLastSynchro()[0];
  95.         $timestamp strtotime($synch->getMoment());
  96.         /** @var \DateTime $lastSynchDate */
  97.         $nextSynchDate = (new \DateTime())->setTimestamp($timestamp) ;
  98.         $lastSynchDate = clone ($nextSynchDate);
  99.         $nextSynchDate->add(new \DateInterval('PT1H'));
  100.         return ['lastSynch' => $lastSynchDate->format('d/m/y H:i') , 'nextSynch' => $nextSynchDate->format('d/m/y H:i')];
  101.     }
  102.     private function getNotifications(): array
  103.     {
  104.         $notificationRepository $this->em->getRepository(Notifications::class);
  105.         $notifications['EXP'] = $notificationRepository->findBy(['type' => 'EXPIRATION']);
  106.         $notifications['LIMIT']  = $notificationRepository->findBy(['type' => 'LIMIT']);
  107.         return $notifications ;
  108.     }
  109. }