src/Controller/EntrypointController.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Widget;
  4. use App\Helpers\TiberiumHelper;
  5. use App\Utils\Notification\OrderRemainderNotification;
  6. use App\Utils\Tiberium;
  7. use Exception;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  18. class EntrypointController extends AbstractController
  19. {
  20. /**
  21. * @param Widget $widget
  22. * @param Tiberium $tiberium
  23. * @return Response
  24. * @throws Exception
  25. */
  26. #[Route('/widget/{id}/products', name: "products", methods: ['GET'])]
  27. public function products(Widget $widget, Tiberium $tiberium): Response
  28. {
  29. $tiberium->setDsn($widget->getOrderServiceDsn());
  30. $products = explode(',', $widget->getProducts());
  31. $productsJson = [];
  32. foreach($products as $row){
  33. $productData = null;
  34. try {
  35. $productData = $tiberium->getProduct($row);
  36. } catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
  37. continue;
  38. }
  39. $product = TiberiumHelper::getProductFromTiberiumData($productData);
  40. if (is_null($product)){
  41. continue;
  42. }
  43. $productsJson[$product['Id']] = [
  44. 'Name' => $product['Name'],
  45. 'InStock' => $product['InStock'],
  46. 'Price' => $product['Price'],
  47. 'Params' => $product['Params'],
  48. ];
  49. }
  50. return $this->json($productsJson);
  51. }
  52. /**
  53. * @param Request $request
  54. * @param Widget $widget
  55. * @param OrderRemainderNotification $notification
  56. * @return JsonResponse
  57. */
  58. #[Route("/order/notification/widget/{id}", name: "create_order_notification", methods: ['POST'])]
  59. public function createOrderNotification(Request $request, Widget $widget, OrderRemainderNotification $notification): JsonResponse
  60. {
  61. $notification->send(
  62. $widget,
  63. $request->toArray()['client'],
  64. $request->toArray()['email'],
  65. $request->toArray()['nominal'],
  66. $request->toArray()['quantity'],
  67. $request->toArray()['remainder']
  68. );
  69. return new JsonResponse(['status' => 'ok']);
  70. }
  71. /**
  72. * @Route("/", name="entrypoint")
  73. */
  74. public function index(): Response
  75. {
  76. return $this->redirect('backend/orders/statistics');
  77. }
  78. /**
  79. * @Route("/backend", name="backend_index", priority="0")
  80. * @Security("is_granted('ROLE_CLIENT') or is_granted('ROLE_MERCHANT') or is_granted('ROLE_MANAGER') or is_granted('ROLE_SUPERADMINISTRATOR')")
  81. */
  82. public function backend(): Response
  83. {
  84. return $this->redirect('backend/orders/statistics');
  85. }
  86. }