src/Controller/Frontend/MarketPlaceController.php line 106

  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Services\CategoryService;
  4. use App\Services\Mooc\MoocService;
  5. use App\Services\Product\FakeData;
  6. use App\Services\Product\ProductService;
  7. use App\Services\ReviewService;
  8. use App\Services\TokenVerifier;
  9. use App\Services\User\AuthUserService;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. class MarketPlaceController extends AbstractController
  17. {
  18.     private ProductService $productService;
  19.     private CategoryService $categoryService;
  20.     private PaginatorInterface $paginator;
  21.     private ReviewService $reviewService;
  22.     private AuthUserService $authUserService;
  23.     /**
  24.      * @param ProductService $productService
  25.      * @param CategoryService $categoryService
  26.      * @param PaginatorInterface $paginator
  27.      * @param ReviewService $reviewService
  28.      * @param AuthUserService $authUserService
  29.      */
  30.     public function __construct(ProductService $productServiceCategoryService $categoryServicePaginatorInterface $paginator,
  31.                                 ReviewService $reviewServiceAuthUserService $authUserService)
  32.     {
  33.         $this->productService $productService;
  34.         $this->categoryService $categoryService;
  35.         $this->paginator $paginator;
  36.         $this->reviewService $reviewService;
  37.         $this->authUserService $authUserService;
  38.     }
  39.     #[Route('/market-place'name'app_market_search')]
  40.     public function index(Request $requeststring $marketplaceEnabled): Response
  41.     {
  42.         if (!$marketplaceEnabled) {
  43.             throw $this->createNotFoundException();
  44.         }
  45.         try {
  46.             $client_id null;
  47.             $client $this->authUserService->getAllAboutMe();
  48.             if ($client !== null) {
  49.                 $client_id $client->id;
  50.             }
  51.         } catch (AuthenticationException $exception) {
  52.             $client_id null;
  53.         }
  54.         $products $this->paginator->paginate(
  55.             $this->productService->getProducts(nulltruefalse$client_id),
  56.             $request->query->getInt('page'1),
  57.             limit9
  58.         );
  59.         $categories json_decode($this->categoryService->getCategories()->getContent(), true);
  60.         return $this->render('frontend/market/index.html.twig', [
  61.             'products' => $products,
  62.             'categories' => $categories,
  63.         ]);   
  64.     }
  65.     #[Route('market/products/{id}'name'app_market_detail_products')]
  66.     public function details($idstring $marketplaceEnabled): Response
  67.     {
  68.         if (!$marketplaceEnabled) {
  69.             throw $this->createNotFoundException();
  70.         }
  71.         $my_product $this->productService->getProduct($id);
  72.         $my_product json_decode($my_product->getContent(), true);
  73.         $products $this->productService->getProductsByCategory($my_product['id']);
  74.         $products json_decode($products->getContent(), true);
  75.         $reviews $this->reviewService->getReview($my_product['id']);
  76.         $productsArray=[];
  77.         $i 0;
  78.         foreach ($products as $product){
  79.             if ($i 3){
  80.                 $productsArray[] = $product;
  81.             }
  82.             $i $i 1;
  83.         }
  84.         
  85.         return $this->render("frontend/market/detail-product.html.twig",[
  86.             'product' => $my_product,
  87.             'products' => $productsArray,
  88.             'reviews' => $reviews
  89.         ]);
  90.     }
  91.     #[Route('/market-place/category/{id}'name'app_market_search_by_category')]
  92.     public function productByCategory(Request $request$idstring $marketplaceEnabled): Response
  93.     {
  94.         if (!$marketplaceEnabled) {
  95.             throw $this->createNotFoundException();
  96.         }
  97.         $products $this->productService->getProductsByCategory((int) $id);
  98.         $products json_decode($products->getContent(), true);
  99.         $products $this->paginator->paginate(
  100.             $products,
  101.             $request->query->getInt('page'1),
  102.             limit9
  103.         );
  104.         $categories json_decode($this->categoryService->getCategories()->getContent(), true);
  105.         return $this->render('frontend/market/product-by-category.html.twig', [
  106.             'products' => $products,
  107.             'categories' => $categories
  108.         ]);
  109.     }
  110. }