<?php
namespace App\Controller;
use App\Entity\Widget;
use App\Helpers\TiberiumHelper;
use App\Utils\Notification\OrderRemainderNotification;
use App\Utils\Tiberium;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class EntrypointController extends AbstractController
{
/**
* @param Widget $widget
* @param Tiberium $tiberium
* @return Response
* @throws Exception
*/
#[Route('/widget/{id}/products', name: "products", methods: ['GET'])]
public function products(Widget $widget, Tiberium $tiberium): Response
{
$tiberium->setDsn($widget->getOrderServiceDsn());
$products = explode(',', $widget->getProducts());
$productsJson = [];
foreach($products as $row){
$productData = null;
try {
$productData = $tiberium->getProduct($row);
} catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
continue;
}
$product = TiberiumHelper::getProductFromTiberiumData($productData);
if (is_null($product)){
continue;
}
$productsJson[$product['Id']] = [
'Name' => $product['Name'],
'InStock' => $product['InStock'],
'Price' => $product['Price'],
'Params' => $product['Params'],
];
}
return $this->json($productsJson);
}
/**
* @param Request $request
* @param Widget $widget
* @param OrderRemainderNotification $notification
* @return JsonResponse
*/
#[Route("/order/notification/widget/{id}", name: "create_order_notification", methods: ['POST'])]
public function createOrderNotification(Request $request, Widget $widget, OrderRemainderNotification $notification): JsonResponse
{
$notification->send(
$widget,
$request->toArray()['client'],
$request->toArray()['email'],
$request->toArray()['nominal'],
$request->toArray()['quantity'],
$request->toArray()['remainder']
);
return new JsonResponse(['status' => 'ok']);
}
/**
* @Route("/", name="entrypoint")
*/
public function index(): Response
{
return $this->redirect('backend/orders/statistics');
}
/**
* @Route("/backend", name="backend_index", priority="0")
* @Security("is_granted('ROLE_CLIENT') or is_granted('ROLE_MERCHANT') or is_granted('ROLE_MANAGER') or is_granted('ROLE_SUPERADMINISTRATOR')")
*/
public function backend(): Response
{
return $this->redirect('backend/orders/statistics');
}
}