src/Controller/Api/MetakockaReceiverController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use PhpAmqpLib\Channel\AbstractChannel;
  4. use PhpAmqpLib\Channel\AMQPChannel;
  5. use PhpAmqpLib\Connection\AMQPStreamConnection;
  6. use PhpAmqpLib\Message\AMQPMessage;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * @Route("/api/receiver", name="api_receiver_")
  13.  */
  14. class MetakockaReceiverController extends AbstractController
  15. {
  16.     const LOGFILE 'webhook_receiver';
  17.     /**
  18.      * @var AMQPStreamConnection
  19.      */
  20.     protected $connection;
  21.     /**
  22.      * @var AbstractChannel|AMQPChannel
  23.      */
  24.     protected $channel;
  25.     /**
  26.      * @var array
  27.      */
  28.     private $connectionParams = [
  29.         'host' => '212.44.120.180',
  30.         'port' => '5672',
  31.         'user' => 'malizakladi',
  32.         'password' => 'h4pCEJEZZW3PxbZaw9otkfJM',
  33.         'vhost' => 'prod'
  34.     ];
  35.     private $queueName 'erp.product.queue';
  36.     /**
  37.      * @Route("/submit", name="submit", methods={"POST"})
  38.      */
  39.     public function submit(Request $request): JsonResponse
  40.     {
  41.         // Decode the JSON request payload
  42.         $data json_decode($request->getContent(), true);
  43.         if (!$data) {
  44.             return new JsonResponse(['error' => 'Invalid request data'], 400);
  45.         }
  46.         try {
  47.             $this->connection = new AMQPStreamConnection(
  48.                 $this->connectionParams['host'],
  49.                 $this->connectionParams['port'],
  50.                 $this->connectionParams['user'],
  51.                 $this->connectionParams['password'],
  52.                 $this->connectionParams['vhost']
  53.             );
  54.             $this->channel $this->connection->channel();
  55.             $this->channel->queue_declare($this->queueNamefalsetruefalsefalse);
  56.             $message = [];
  57.             if (isset($data['stock_list'])) {
  58.                 foreach ($data['stock_list'] as $stock) {
  59.                     $message[] = $stock['code'];
  60.         }
  61.         \Pimcore\Log\Simple::log(self::LOGFILEjson_encode($message));
  62.                 $msg = new AMQPMessage(json_encode($message), ['message_id' => time()]);
  63.                 $this->channel->basic_publish($msg''$this->queueName);
  64.         } else {
  65.             \Pimcore\Log\Simple::log(self::LOGFILEjson_encode($data));
  66.         }
  67.         } catch (\Exception $e) {
  68.             \Pimcore\Log\Simple::log(self::LOGFILE$e->getMessage());
  69.     }
  70.         return new JsonResponse(["status"=>"ok"]);
  71.     }
  72. }