<?php /** @noinspection ALL */
namespace Plugin\IkusMobileApi\Controller;
use Eccube\Controller\AbstractController;
use PHPUnit\Util\Json;
use Plugin\IkusMobileApi\Service\ProductService;
use Plugin\IkusMobileApi\Util\Paths;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ProductController extends AbstractController
{
private ProductService $productService;
public function __construct(ProductService $productService)
{
$this->productService = $productService;
}
/**
* Search API
* @Route("/api/products/search", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function search(Request $request)
{
$results = $this->productService->search($request->query->all());
return $this->json($this->formatList($request, $results));
}
/**
* Ranking API
* @Route("/api/products/ranking", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function ranking(Request $request): JsonResponse
{
$results = $this->productService->findByRanking();
return $this->json($this->formatList($request, $results));
}
/**
* PickUp API
* @Route("/api/products/pickUp", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function pickUp(Request $request): JsonResponse
{
$results = $this->productService->findByPickUp();
return $this->json($this->formatList($request, $results));
}
/**
* Checked API
* @Route("/api/products/checked", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function checked(Request $request): JsonResponse
{
$ids = $request->query->get('ids');
if (!is_array($ids)) {
return $this->json([]);
}
$results = $this->productService->findByIds($ids);
return $this->json($this->formatList($request, $results));
}
/**
* カテゴリー内の商品が持つ検索条件取得API
* @Route("/api/product/styleFilters", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function styleFilters(Request $request): JsonResponse
{
$category_id = $request->get('category_id');
$results = $this->productService->findStyleFilters($category_id);
return $this->json($results);
}
private function formatList(Request $request, $results)
{
foreach ($results as &$result) {
$result['min_price'] = intval($result['min_price']);
$result['max_price'] = intval($result['max_price']);
if ($result['image'] !== null) {
$result['image'] = $request->getSchemeAndHttpHost() . Paths::UPLOAD_SAVE_IMAGE . $result['image'];
}
}
unset($result);
return $results;
}
}