src/Controller/UpdateStockController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Classes\Constant;
  4. use App\Services\BusinessCentral\Exceptions\NotFoundException;
  5. use App\Services\Routes\ImportStocks;
  6. use App\Services\Shopware6\Shopware6Client;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\DependencyInjection\Attribute\Autowire;
  9. use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Throwable;
  14. use Vin\ShopwareSdk\Data\Criteria;
  15. use Vin\ShopwareSdk\Data\Entity\Product\ProductDefinition;
  16. use Vin\ShopwareSdk\Factory\RepositoryFactory;
  17. class UpdateStockController extends AbstractController
  18. {
  19.     public function __construct(
  20.         #[Autowire("%webhookSecret%")]
  21.         protected string $webhookSecret,
  22.         protected Shopware6Client $shopware6Client,
  23.         protected ImportStocks $importStocksRoute
  24.     ) {
  25.     }
  26.     /**
  27.      * @throws Throwable
  28.      */
  29.     #[Route('/webhook/update-stock'name'app_webhook_update_stock')]
  30.     public function index(Request $request): Response
  31.     {
  32.         // Validation
  33.         $shopwareProductId $request->get("id");
  34.         $preview $request->get("preview"false);
  35.         if (!$shopwareProductId) {
  36.             return $this->json([
  37.                 "success" => false,
  38.                 "error" => "Product id not supplied"
  39.             ], 500);
  40.         }
  41.         // Update stock
  42.         try {
  43.             $productRepository RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
  44.             
  45.             $shopwareProduct $productRepository->get(
  46.                 $shopwareProductId,
  47.                 new Criteria(),
  48.                 $this->shopware6Client->context
  49.             );
  50.             if (!$shopwareProduct) {
  51.                 $criteria = new Criteria();
  52.                 $criteria->addFilter(new EqualsFilter("parentId"$shopwareProductId));
  53.                 $shopwareProduct $productRepository->search($criteria$this->shopware6Client->context)->first();
  54.                 if (!$shopwareProduct) {
  55.                     throw new NotFoundException("Product not found");
  56.                 }
  57.             }
  58.             $productNumber $shopwareProduct->productNumber;
  59.             
  60.             $payload $this->importStocksRoute->execute($productNumberfalse$previewfalse);
  61.             $statusCode 200;
  62.             $response = [
  63.                 "success" => true,
  64.                 "message" => "Updated",
  65.                 "data" => $payload
  66.             ];
  67.         } catch (Throwable $exception) {
  68.             $statusCode 500;
  69.             $response = [
  70.                 "success" => false,
  71.                 "error" => $exception->getMessage()
  72.             ];
  73.         }
  74.         return $this->json($response$statusCode);
  75.     }
  76. }