src/Controller/WB/ApiController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller\WB;
  3. use App\Entity\WBOrder;
  4. use App\Entity\WBOrderItem;
  5. use App\Entity\WildberriesClient;
  6. use App\Entity\WildberriesProduct;
  7. use App\Service\WB\PriceListXmlService;
  8. use App\Service\WB\Type\WildberriesSellerPostingStatusType;
  9. use App\Service\WB\WildberriesPostingService;
  10. use DateTime;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\HttpFoundation\HeaderUtils;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\StreamedResponse;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * Class ApiController.
  21.  */
  22. class ApiController
  23. {
  24.     /** @var EntityManagerInterface */
  25.     protected EntityManagerInterface $em;
  26.     /** @var LoggerInterface */
  27.     protected $logger;
  28.     protected PriceListXmlService $xmlService;
  29.     protected ContainerInterface $ci;
  30.     private WildberriesPostingService $postingService;
  31.     /**
  32.      * ApiController constructor.
  33.      *
  34.      * @param EntityManagerInterface $em
  35.      * @param LoggerInterface        $logger
  36.      */
  37.     public function __construct(
  38.         EntityManagerInterface $em,
  39.         LoggerInterface $logger,
  40.         PriceListXmlService $xmlService,
  41.         WildberriesPostingService $postingService,
  42.         ContainerInterface $ci
  43.     )
  44.     {
  45.         $this->em $em;
  46.         $this->logger $logger;
  47.         $this->xmlService $xmlService;
  48.         $this->postingService $postingService;
  49.         $this->ci $ci;
  50.     }
  51.     /**
  52.      * @Route(
  53.      *     "/wb/feed/{clientId}",
  54.      *     name="wb_export",
  55.      *     host="%host.monitoring%",
  56.      *     methods={"GET"}
  57.      * )
  58.      *
  59.      * @return Response
  60.      * @throws \Exception
  61.      */
  62.     public function wbApiExportAction(int $clientId): Response
  63.     {
  64.         $filePath $this->ci->getParameter('upload_dir').'/wb/feed-' $clientId '.xml';
  65.         $data '';
  66.         if (is_file($filePath)) {
  67.             $data file_get_contents($filePath);
  68.         }
  69.         $response = new StreamedResponse(function () use ($data) {
  70.             echo $data;
  71.         });
  72.         $disposition HeaderUtils::makeDispositionHeaderUtils::DISPOSITION_ATTACHMENT,
  73.             sprintf("feed-%s.xml"$clientId));
  74.         $response->headers->set('Content-Disposition'$disposition);
  75.         $response->headers->set('Content-Type''application/xml;charset=UTF-8');
  76.         return $response;
  77.     }
  78.     /**
  79.      * @Route(
  80.      *     "/wb/rid/{rid}",
  81.      *     name="wb_get_order_id",
  82.      *     host="%host.monitoring%",
  83.      *     methods={"GET"}
  84.      * )
  85.      *
  86.      * @return Response
  87.      * @throws \Exception
  88.      */
  89.     public function wbApiGetOrderIdByRid(string $rid)
  90.     {
  91.         /** @var WBOrderItem $orderItem */
  92.         $orderItem $this->em->getRepository(WBOrderItem::class)->findOneBy(['rid' => $rid]);
  93.         if (!$orderItem) {
  94.             return new JsonResponse(['error' => 'rid not found']);
  95.         }
  96.         return new JsonResponse(['orderId' => $orderItem->getOrder()->getOrderId()]);
  97.     }
  98.     /**
  99.      * @Route(
  100.      *     "/wb/order/{orderId}/receive",
  101.      *     name="wb_receive_order_id",
  102.      *     host="%host.monitoring%",
  103.      *     methods={"GET"}
  104.      * )
  105.      *
  106.      * @return Response
  107.      * @throws \Exception
  108.      */
  109.     public function wbApiReceiveOrderIdByRid(string $orderId)
  110.     {
  111.         /** @var WBOrder $orderItem */
  112.         $order $this->em->getRepository(WBOrder::class)->findOneBy(['omsId' => $orderId]);
  113.         if (!$order) {
  114.             return new JsonResponse(['error' => 'order not found']);
  115.         }
  116.         if ($this->postingService->apiReceiveWildberriesPosting($order->getClient(), $order->getOrderId())) {
  117.             $order->setStatus(WildberriesSellerPostingStatusType::DELIVER);
  118.             $this->em->flush();
  119.         }
  120.         return new JsonResponse(['success' => true]);
  121.     }
  122.     /**
  123.      * @Route(
  124.      *     "/wb/get/clients",
  125.      *     name="wb_get_clients",
  126.      *     host="%host.monitoring%",
  127.      *     methods={"GET"}
  128.      * )
  129.      *
  130.      * @return Response
  131.      * @throws \Exception
  132.      */
  133.     public function wbApiGetClientsAction()
  134.     {
  135.         $clients $this->em->getRepository(WildberriesClient::class)->findClients();
  136.         if (!$clients) {
  137.             return new JsonResponse(['error' => 'clients not found']);
  138.         }
  139.         return new JsonResponse($clients);
  140.     }
  141.     /**
  142.      * @Route(
  143.      *     "/wb/get/client/{clientId}/products",
  144.      *     name="wb_get_client_products",
  145.      *     host="%host.monitoring%",
  146.      *     methods={"GET"}
  147.      * )
  148.      *
  149.      * @return Response
  150.      * @throws \Exception
  151.      */
  152.     public function wbApiGetClientProductsAction(int $clientId)
  153.     {
  154.         $clients $this->em->getRepository(WildberriesProduct::class)->findProductsInfoByClient($clientId);
  155.         if (!$clients) {
  156.             return new JsonResponse(['error' => 'clients not found']);
  157.         }
  158.         return new JsonResponse($clients);
  159.     }
  160. }