src/Controller/Admin/CloneMenuController.php line 71

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Admin;
  4. use App\Entity\Franchise\Franchise;
  5. use App\Entity\Shop\Menu;
  6. use App\Entity\Shop\Shop;
  7. use App\Entity\Traits\CommandLockTrait;
  8. use App\Repository\Franchise\FranchiseRepository;
  9. use App\Repository\Shop\MenuRepository;
  10. use App\Repository\Shop\ShopRepository;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\KernelInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * @IsGranted("ROLE_ADMIN")
  19.  */
  20. class CloneMenuController extends AbstractController
  21. {
  22.     use CommandLockTrait;
  23.     public function __construct(
  24.         private $foxordersLocksDirectory,
  25.         private ShopRepository $shopRepository,
  26.         private FranchiseRepository $franchiseRepository,
  27.         private MenuRepository $menuRepository,
  28.         private TranslatorInterface $translator,
  29.     ) {
  30.     }
  31.     public function index(): Response
  32.     {
  33.         $franchises $this->franchiseRepository->getAll();
  34.         return $this->renderForm('Admin/CloneMenu/index.html.twig'compact('franchises'));
  35.     }
  36.     public function shops(Franchise $franchise): JsonResponse
  37.     {
  38.         return new JsonResponse($this->shopRepository->getShopsByFranchise($franchise));
  39.     }
  40.     public function menus(Shop $shop): JsonResponse
  41.     {
  42.         return new JsonResponse($this->menuRepository->getMenus($shop));
  43.     }
  44.     public function clone(Shop $shopSrcShop $shopDestMenu $menuKernelInterface $kernel): JsonResponse
  45.     {
  46.         if ($shopSrc !== $menu->getShop()) {
  47.             return new JsonResponse($this->translator->trans('app.admin.pages.menu_clones.message.shop_menu_discordant'), Response::HTTP_BAD_REQUEST);
  48.         }
  49.         $srcLocales $shopSrc->getFranchise()->getLocales()->toArray();
  50.         $destLocales $shopDest->getFranchise()->getLocales()->toArray();
  51.         if ([] !== array_diff($srcLocales$destLocales)) {
  52.             return new JsonResponse('INVALID_LOCALES'Response::HTTP_BAD_REQUEST);
  53.         }
  54.         exec("nohup php {$kernel->getProjectDir()}/bin/console clone-menu {$menu->getId()} {$shopDest->getId()} {$shopDest->getFranchise()->getId()} > /dev/null 2>&1 &");
  55.         return new JsonResponse();
  56.     }
  57.     public function locked(): JsonResponse
  58.     {
  59.         return new JsonResponse(['isLocked' => $this->isLocked($this->foxordersLocksDirectory)]);
  60.     }
  61. }