app/Plugin/IkusMobileApi/Controller/CustomerController.php line 40

Open in your IDE?
  1. <?php
  2. namespace Plugin\IkusMobileApi\Controller;
  3. use Doctrine\ORM\NonUniqueResultException;
  4. use Eccube\Controller\AbstractController;
  5. use Exception;
  6. use Plugin\IkusMobileApi\Service\CustomerAddressService;
  7. use Plugin\IkusMobileApi\Service\CustomerFavoriteProductService;
  8. use Plugin\IkusMobileApi\Service\CustomerService;
  9. use Plugin\IkusMobileApi\Util\Paths;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class CustomerController extends AbstractController
  14. {
  15.     private CustomerService $customerService;
  16.     private CustomerFavoriteProductService $customerFavoriteProductService;
  17.     private CustomerAddressService $customerAddressService;
  18.     public function __construct(
  19.         CustomerService $customerService,
  20.         CustomerFavoriteProductService $customerFavoriteProductService,
  21.         CustomerAddressService $customerAddressService
  22.     )
  23.     {
  24.         $this->customerService $customerService;
  25.         $this->customerFavoriteProductService $customerFavoriteProductService;
  26.         $this->customerAddressService $customerAddressService;
  27.     }
  28.     /**
  29.      * Customer API
  30.      * @Route("/api/customer", methods={"GET"})
  31.      * @param Request $request
  32.      * @return JsonResponse
  33.      * @throws Exception
  34.      */
  35.     public function customer(Request $request): JsonResponse
  36.     {
  37.         $id $request->get('id');
  38.         $this->customerService->verifyApiKey($request$id);
  39.         $result $this->customerService->findById($id);
  40.         return $this->json($result);
  41.     }
  42.     /**
  43.      * 会員情報更新 API
  44.      * @Route("/api/customer", methods={"PUT"})
  45.      * @param Request $request
  46.      * @return JsonResponse
  47.      * @throws Exception
  48.      */
  49.     public function updateCustomer(Request $request): JsonResponse
  50.     {
  51.         $data json_decode($request->getContent(), true);
  52.         $this->customerService->verifyApiKey($request$data['id']);
  53.         $res $this->customerService->update($data);
  54.         return $this->json($res);
  55.     }
  56.     /**
  57.      * お気に入り API
  58.      * @Route("/api/customer/favorites", methods={"GET"})
  59.      * @param Request $request
  60.      * @return JsonResponse
  61.      * @throws Exception
  62.      */
  63.     public function favorites(Request $request): JsonResponse
  64.     {
  65.         $customer_id $request->get('customer_id');
  66.         $this->customerService->verifyApiKey($request$customer_id);
  67.         $results $this->customerFavoriteProductService->findByCustomerId($customer_id);
  68.         foreach ($results as &$result) {
  69.             $result['min_price'] = intval($result['min_price'] * 1.1);
  70.             $result['max_price'] = intval($result['max_price'] * 1.1);
  71.             if ($result['image'] !== null) {
  72.                 $result['image'] = $request->getSchemeAndHttpHost() . Paths::UPLOAD_SAVE_IMAGE $result['image'];
  73.             }
  74.         }
  75.         unset($result);
  76.         return $this->json($results);
  77.     }
  78.     /**
  79.      * お届け先一覧 API
  80.      * @Route("/api/customer/addresses", methods={"GET"})
  81.      * @param Request $request
  82.      * @return JsonResponse
  83.      * @throws Exception
  84.      */
  85.     public function addresses(Request $request): JsonResponse
  86.     {
  87.         $customer_id $request->get('customer_id');
  88.         $this->customerService->verifyApiKey($request$customer_id);
  89.         $results $this->customerAddressService->findByCustomerId($customer_id);
  90.         return $this->json($results);
  91.     }
  92.     /**
  93.      * お届け先取得 API
  94.      * @Route("/api/customer/address", methods={"GET"})
  95.      * @param Request $request
  96.      * @return JsonResponse
  97.      * @throws NonUniqueResultException
  98.      * @throws Exception
  99.      */
  100.     public function address(Request $request): JsonResponse
  101.     {
  102.         $id $request->get('id');
  103.         $customer_id $request->get('customer_id');
  104.         $this->customerService->verifyApiKey($request$customer_id);
  105.         $res $this->customerAddressService->findById($id);
  106.         return $this->json($res);
  107.     }
  108.     /**
  109.      * お届け先登録 API
  110.      * @Route("/api/customer/address", methods={"POST"})
  111.      * @param Request $request
  112.      * @return JsonResponse
  113.      * @throws Exception
  114.      */
  115.     public function addAddress(Request $request): JsonResponse
  116.     {
  117.         $data json_decode($request->getContent(), true);
  118.         $this->customerService->verifyApiKey($request$data['customer_id']);
  119.         $res $this->customerAddressService->create($data);
  120.         return $this->json($res);
  121.     }
  122.     /**
  123.      * お届け先更新 API
  124.      * @Route("/api/customer/address", methods={"PUT"})
  125.      * @param Request $request
  126.      * @return JsonResponse
  127.      * @throws Exception
  128.      */
  129.     public function updateAddress(Request $request): JsonResponse
  130.     {
  131.         $data json_decode($request->getContent(), true);
  132.         $this->customerService->verifyApiKey($request$data['customer_id']);
  133.         $res $this->customerAddressService->update($data);
  134.         return $this->json($res);
  135.     }
  136.     /**
  137.      * お届け先削除 API
  138.      * @Route("/api/customer/address", methods={"DELETE"})
  139.      * @param Request $request
  140.      * @return JsonResponse
  141.      * @throws Exception
  142.      */
  143.     public function deleteAddress(Request $request): JsonResponse
  144.     {
  145.         $id $request->get('id');
  146.         $customer_id $request->get('customer_id');
  147.         $this->customerService->verifyApiKey($request$customer_id);
  148.         $this->customerAddressService->delete($id);
  149.         return $this->json([
  150.             'result' => true,
  151.         ]);
  152.     }
  153. }