src/Controller/HomeController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Datetime;
  7. use Psr\Log\LoggerInterface;
  8. use App\Entity\Notification;
  9. use App\Entity\ProposedArticle;
  10. use App\Repository\ArticleRepository;
  11. use App\Repository\ProposedArticleRepository;
  12. use App\Repository\NotificationRepository;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use App\Entity\Article;
  15. use App\Repository\UserPackageRepository;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. class HomeController extends AbstractController
  19. {
  20.     private $logger;
  21.     private $httpClient;
  22.     public function __construct(HttpClientInterface $httpClientLoggerInterface $logger)
  23.     {
  24.         $this->logger $logger;
  25.         $this->httpClient $httpClient;
  26.     }
  27.     #[Route('/home'name'app_home')]
  28.     public function index(UserPackageRepository $userPackageRepositoryProposedArticleRepository $proposedArticleRepositoryArticleRepository $articleRepositoryNotificationRepository $notificationRepositoryRequest $request): Response
  29.     {
  30.         // Je récupères les articles du client
  31.             $packages $userPackageRepository->findByUser($this->getUser());
  32.             $notifications $notificationRepository->findBy(['user' => $this->getUser()], ['dateCreated' => 'DESC']);
  33.             $proposedArticles $proposedArticleRepository->findBy(['user' => $this->getUser()], ['dateCreated' => 'DESC']);
  34.             $articles $articleRepository->findBy(['user' => $this->getUser(), 'deleted' => false], ['orderDate' => 'DESC']);
  35.             $articlesDelivered $articleRepository->findBy(['user' => $this->getUser(), 'deleted' => false'status' => 'Livré'], ['orderDate' => 'DESC']);
  36.             $articlesOnline $articleRepository->findBy(['user' => $this->getUser(), 'deleted' => false'status' => 'En ligne'], ['orderDate' => 'DESC']);
  37.             //dump($articles);
  38.         $seoPackages null;
  39.         foreach($packages as $userPackage) {
  40.             $articleType $userPackage->getPackage()->getArticleType()->getId();
  41.             //dump($articleType);
  42.             if($articleType === && $userPackage->getSolde() > 0) {
  43.                 $seoPackages[] = $userPackage;
  44.             }
  45.         }
  46.          
  47.         if ($this->isGranted('ROLE_ADMIN')) {
  48.             $articles $articleRepository->findBy(['deleted' => false], ['orderDate' => 'DESC']);
  49.             /*$unreads = $articleRepository->findBy(['user' => $this->getUser(), 'readed' => false, 'deleted' => false]);
  50.             $numberUnRead = null;
  51.             //dump($unreads);
  52.             foreach($unreads as $unread) {
  53.                 if($unread->getContent() !== null) {
  54.                     $numberUnRead++;
  55.                 }
  56.             }*/
  57.         }
  58.         return $this->render('home/index.html.twig', [
  59.             'articles' => $articles,
  60.             'numberArticle' => count($articles),
  61.             'numberArticleDelivered' => count($articlesDelivered),
  62.             'numberArticleOnline' => count($articlesOnline),
  63.             'notifications' => $notifications,
  64.             'titles' => $proposedArticles,
  65.             'seoPackages' => $seoPackages,
  66.         ]);
  67.     }
  68.     #[Route('/'name'app_day')]
  69.     public function error(): Response
  70.     {
  71.         return $this->redirectToRoute('app_home');
  72.     }
  73.     // La commande des exemples de titres
  74.         #[Route('/ask/titles'name'ask_title')]
  75.         public function orderTitles(Request $requestEntityManagerInterface $entityManager): Response
  76.         {
  77.             $user $this->getUser();
  78.             if ($request->get('keyWord') != null) {
  79.                 $keyWord $request->get('keyWord');
  80.                 $keyIdeas $request->get('keyIdeas');
  81.                 try {
  82.                     $orderDate = new Datetime('now');
  83.                     $jsonData = [
  84.                         'authentication_key' => $_ENV['AUTHENTICATION_KEY'],
  85.                         'idcustomer' => $this->getUser()->getId(),
  86.                         'customerkey' => $this->getUser()->getCustomerKey(),
  87.                         'keyword' => $keyWord,
  88.                         'keyideas' => $keyIdeas,
  89.                     ];
  90.                     //dump($jsonData);
  91.                     try {
  92.                         // J'envoie les données de demande d'article au backoffice
  93.                             $response $this->httpClient->request('POST''https://www.groupe-saven.fr/api.cpe/gettitles.php', [
  94.                                 'json' => $jsonData
  95.                             ]);
  96.                             //dump($response);
  97.                         $statusCode $response->getStatusCode();
  98.                         $content $response->getContent(false);
  99.                         $data json_decode($contenttrue);
  100.                         //dump($statusCode);
  101.                         //dump($data);
  102.                         // Je gère les erreurs de transmission avec le back
  103.                             if ($statusCode !== 200 && $statusCode !== 201) {
  104.                                     $this->addFlash('danger''Problème de transmission de votre commande');
  105.                                 // Lancer une exception avec le contenu de la réponse si le code de statut n'est pas 200
  106.                                 throw new \Exception("Erreur HTTP $statusCode: " $content);
  107.                             }
  108.                             if ($data['titles']) {
  109.                                 try {
  110.                                     //dump($data['titles']);
  111.                                     // Je cree la notification pour l'historique d'activité
  112.                                         $notification = new Notification();
  113.                                         $creationDate = new Datetime('now');
  114.                                         $notification->setDateCreated($creationDate);
  115.                                         $notification->setTitle('Demande de propositions de titres');
  116.                                         $notification->setUser($user);
  117.                                         $notification->setDetail($keyWord);
  118.                                         $notification2 = new Notification();
  119.                                         $creationDate2 = new Datetime('now');
  120.                                         $notification2->setDateCreated($creationDate2);
  121.                                         $notification2->setTitle('Livraisons des propositions de titres');
  122.                                         $notification2->setUser($user);
  123.                                         $notification2->setDetail($keyWord);
  124.                                         $entityManager->persist($notification2);
  125.                                         $entityManager->persist($notification);
  126.                                         $entityManager->flush();
  127.                                         foreach ($data['titles'] as $title) {
  128.                                             $proposedArticle = new ProposedArticle();
  129.                                             $creationDate = new Datetime('now');
  130.                                             $proposedArticle->setDateCreated($creationDate);
  131.                                             $proposedArticle->setTitle($title);
  132.                                             $proposedArticle->setUser($user);
  133.                                             $entityManager->persist($proposedArticle);
  134.                                             $entityManager->flush();
  135.                                         }
  136.                                         $this->addFlash('success''Vos titres vous ont été livrés');
  137.                                 } catch (Exception $e) {
  138.                                     $this->logger->error('Erreur : '$e->getMessage());
  139.                                 }
  140.                             }
  141.                         $this->addFlash('success''Votre commande a été passée (Vous la recevrez dans environ 10 minutes)');
  142.                         return $this->redirectToRoute('app_home');
  143.                     } catch (Exception $e) {
  144.                         //dump($response);
  145.                         $this->logger->error("danger" $e->getMessage());
  146.                     }
  147.                 } catch (\Exception $e) {
  148.                     // Vérifiez si l'exception implémente HttpExceptionInterface
  149.                         if ($e instanceof HttpExceptionInterface) {
  150.                             // Cas d'erreur côté client, comme une exception `BadRequestHttpException`
  151.                                 $statusCode $e->getStatusCode();
  152.                                 $message $e->getMessage();
  153.                         } else {
  154.                             // Cas d'erreur côté serveur
  155.                                 $statusCode Response::HTTP_INTERNAL_SERVER_ERROR;
  156.                                 $message 'Une erreur interne du serveur s\'est produite.';
  157.                         }
  158.                     // Gestion des erreurs
  159.                             // Log l'erreur
  160.                                 $this->logger->error(sprintf('Erreur %d : %s'$statusCode$e->getMessage()));
  161.                             $this->addFlash('danger''Erreur : '$message);
  162.                     return $this->redirectToRoute('app_home');
  163.                 }
  164.             }
  165.             return $this->redirectToRoute('app_home');
  166.         }
  167.     //
  168. }