<?php
namespace App\Controller\Api;
use PhpAmqpLib\Channel\AbstractChannel;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/receiver", name="api_receiver_")
*/
class MetakockaReceiverController extends AbstractController
{
const LOGFILE = 'webhook_receiver';
/**
* @var AMQPStreamConnection
*/
protected $connection;
/**
* @var AbstractChannel|AMQPChannel
*/
protected $channel;
/**
* @var array
*/
private $connectionParams = [
'host' => '212.44.120.180',
'port' => '5672',
'user' => 'malizakladi',
'password' => 'h4pCEJEZZW3PxbZaw9otkfJM',
'vhost' => 'prod'
];
private $queueName = 'erp.product.queue';
/**
* @Route("/submit", name="submit", methods={"POST"})
*/
public function submit(Request $request): JsonResponse
{
// Decode the JSON request payload
$data = json_decode($request->getContent(), true);
if (!$data) {
return new JsonResponse(['error' => 'Invalid request data'], 400);
}
try {
$this->connection = new AMQPStreamConnection(
$this->connectionParams['host'],
$this->connectionParams['port'],
$this->connectionParams['user'],
$this->connectionParams['password'],
$this->connectionParams['vhost']
);
$this->channel = $this->connection->channel();
$this->channel->queue_declare($this->queueName, false, true, false, false);
$message = [];
if (isset($data['stock_list'])) {
foreach ($data['stock_list'] as $stock) {
$message[] = $stock['code'];
}
\Pimcore\Log\Simple::log(self::LOGFILE, json_encode($message));
$msg = new AMQPMessage(json_encode($message), ['message_id' => time()]);
$this->channel->basic_publish($msg, '', $this->queueName);
} else {
\Pimcore\Log\Simple::log(self::LOGFILE, json_encode($data));
}
} catch (\Exception $e) {
\Pimcore\Log\Simple::log(self::LOGFILE, $e->getMessage());
}
return new JsonResponse(["status"=>"ok"]);
}
}