src/Controller/FrontController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\RecipeCount;
  4. use App\Helpers\SerializerHelper;
  5. use App\Repository\RecipeCountRepository;
  6. use App\Repository\RecipeRepository;
  7. use Detect;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Serializer\SerializerInterface;
  13. class FrontController extends AbstractController
  14. {
  15.     public function __construct(
  16.         private EntityManagerInterface $em,
  17.         private RecipeRepository $recipeRepo,
  18.         private RecipeCountRepository $recipeCountRepo,
  19.         private SerializerInterface $serializer
  20.     ) {}
  21.         
  22.     //  #[Route('/', name: 'app_front_password')]
  23.     // public function getIndexPasswordForget()
  24.     // {
  25.     //     return $this->render('base.html.twig');
  26.     // }
  27.     #[Route('/views-counter/{slug}'name'views-counter')]
  28.     public function countView($slug)
  29.     {
  30.         try {
  31.             $recipe $this->recipeRepo->findOneBy(["slug"=>$slug]);
  32.             
  33.             $ip Detect::ip(); 
  34.             $deviceType Detect::deviceType(); 
  35.             // $browser = Detect::browser(); 
  36.     
  37.             $currentUserInfo =
  38.                 [
  39.                     "ID" => $ip,
  40.                     "USER" => $deviceType
  41.                 ];
  42.     
  43.             $checkRecipeCount $this->recipeCountRepo->findBy(
  44.                 [
  45.                     "identifiant" => $currentUserInfo['ID'], "user" =>  $currentUserInfo['USER']
  46.                 ]
  47.             );
  48.     
  49.             foreach ( $checkRecipeCount as $recipeToCheck ) {
  50.     
  51.                 if($recipeToCheck->getRecipe()->contains($recipe)){
  52.                     return $this->json([
  53.                         "status_code" => Response::HTTP_CONTINUE,
  54.                         "message" => "KO"
  55.                     ], 200);
  56.                 }
  57.     
  58.             }
  59.              
  60.             $userRf $currentUserInfo["ID"] . "-" $currentUserInfo["USER"];
  61.             $recipeCount = new RecipeCount();
  62.             $recipeCount
  63.                 ->setUser($currentUserInfo['USER'])
  64.                 ->setIdentifiant($currentUserInfo['ID'])
  65.                 ->addRecipe($recipe)
  66.                 ->setRf($userRf)
  67.                 ->setNumber(((int) $recipeCount->getNumber() + 1));
  68.     
  69.             $recipeCount->addRecipe($recipe);
  70.             $this->em->persist($recipeCount);
  71.             $this->em->flush();
  72.             $recipeCountJSON SerializerHelper::getJson($this->serializer$recipeCount"show_recipe_count");
  73.             return $this->json([
  74.                 "data" => $recipeCountJSON,
  75.                 "status_code" => Response::HTTP_ACCEPTED,
  76.                 "message" => "OK"
  77.             ], 200);
  78.         }catch(\Exception $e){
  79.             return $this->json([
  80.                 "status_code" => $e->getCode(),
  81.                 "message" => $e->getMessage()
  82.             ], 200);
  83.         }
  84.     }
  85.     #[Route('/api/views-counter/{slug}'name'views-counter-auth')]
  86.     public function countViewAuth($slug)
  87.     {
  88.         $user  $this->getUser();
  89.         $recipe $this->recipeRepo->findOneBy(["slug"=>$slug]);
  90.         $checkRecipeCount $this->recipeCountRepo->findBy(["user"=>$user->getEmail()]);
  91.         $ip Detect::ip(); 
  92.         $deviceType Detect::deviceType(); 
  93.         $currentUserInfo =
  94.             [
  95.                 "ID" => $ip,
  96.                 "USER" => $deviceType
  97.             ];
  98.         foreach ( $checkRecipeCount as $recipeToCheck ) {
  99.             if($recipeToCheck->getRecipe()->contains($recipe)){
  100.                 return $this->json([
  101.                     "status_code" => Response::HTTP_CONTINUE,
  102.                     "message" => "KO"
  103.                 ], 200);
  104.             }
  105.         }
  106.         // if (empty($check) || $check == null) {
  107.         $userRf $currentUserInfo["ID"] . "-" $currentUserInfo["USER"];
  108.         $recipeCount = new RecipeCount();
  109.         $recipeCount
  110.             ->setUser($user->getEmail())
  111.             ->setIdentifiant($currentUserInfo['ID'])
  112.             ->setRf($userRf)
  113.             ->addRecipe($recipe)
  114.             ->setNumber(((int) $recipeCount->getNumber() + 1))
  115.             ->addVisitor($user);
  116.         $this->em->persist($recipeCount);
  117.         $this->em->flush();
  118.         $recipeCountJSON SerializerHelper::getJson($this->serializer$recipeCount"show_recipe_count");
  119.         return $this->json([
  120.             "data" => $recipeCountJSON,
  121.             "status_code" => Response::HTTP_OK,
  122.             "message" => "OK"
  123.         ], 200);
  124.         // } else {
  125.         return $this->json([
  126.             "status_code" => Response::HTTP_CONTINUE,
  127.             "message" => "Not Found"
  128.         ], 100);
  129.         // }
  130.     }
  131.     #[Route('recipes-for-abj')]
  132.     public function getRecipeForAbj(){
  133.         $recipes $this->recipeRepo->findBy(["isSpotLightAbj"=> true]);
  134.         $recipesJSON SerializerHelper::getJson($this->serializer$recipes"show_recipe");
  135.         return $this->json([
  136.             "data"=>$recipesJSON,
  137.             "message"=> "OK"
  138.         ]);
  139.     }
  140.     // #[Route('/{page}', name: 'app_front_home_front', requirements:["page"=>"^(?!.*wdt).+"])]
  141.     // ([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
  142.     /**
  143.      * @Route("/{page}", requirements={"page": "([a-z0-9-].?!.*)+(:[0-9]+).+"})
  144.      * @Route("/{page}", requirements={"page": "([a-z0-9-].?!.*)+(:[0-9]+).+", "page1": "([a-z0-9-].?!.*)+(:[0-9]+).+"})
  145.      */
  146.     // #[Route('/{page}', name: 'app_front_home_front', requirements:["page"=>"^.*$"])]
  147.     // #[Route('/{page}/{page1}', name: 'app_front_home_front', requirements:["page"=>"^(?!panel).*$", "page1"=>"^(?!panel).*$"])]
  148.     // public function getFrontView(Request $req): Response
  149.     // {
  150.         // return $this->render('FrontOffice/index.html.twig', []);
  151.     // }
  152.     // #[Route('/{page}', name: 'app_front_home_front', requirements:["page"=>"^.*$"])]
  153.     // #[Route('/{page}', name: 'app_front_home_front', requirements:["page"=>"^(?!api).+"])]
  154.     // /**
  155.     //  * @Route("/{page}", requirements={"page": "([a-z0-9-].?!.*)+(:[0-9]+).+"})
  156.     // */
  157.     // #[Route('/', name: 'app_front_home_front')]
  158.     // public function getIndexPage(): Response
  159.     // {
  160.     //     return $this->render('FrontOffice/index.html.twig');
  161.     // }
  162. }