<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\Franchise\Franchise;
use App\Entity\Shop\Menu;
use App\Entity\Shop\Shop;
use App\Entity\Traits\CommandLockTrait;
use App\Repository\Franchise\FranchiseRepository;
use App\Repository\Shop\MenuRepository;
use App\Repository\Shop\ShopRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @IsGranted("ROLE_ADMIN")
*/
class CloneMenuController extends AbstractController
{
use CommandLockTrait;
public function __construct(
private $foxordersLocksDirectory,
private ShopRepository $shopRepository,
private FranchiseRepository $franchiseRepository,
private MenuRepository $menuRepository,
private TranslatorInterface $translator,
) {
}
public function index(): Response
{
$franchises = $this->franchiseRepository->getAll();
return $this->renderForm('Admin/CloneMenu/index.html.twig', compact('franchises'));
}
public function shops(Franchise $franchise): JsonResponse
{
return new JsonResponse($this->shopRepository->getShopsByFranchise($franchise));
}
public function menus(Shop $shop): JsonResponse
{
return new JsonResponse($this->menuRepository->getMenus($shop));
}
public function clone(Shop $shopSrc, Shop $shopDest, Menu $menu, KernelInterface $kernel): JsonResponse
{
if ($shopSrc !== $menu->getShop()) {
return new JsonResponse($this->translator->trans('app.admin.pages.menu_clones.message.shop_menu_discordant'), Response::HTTP_BAD_REQUEST);
}
$srcLocales = $shopSrc->getFranchise()->getLocales()->toArray();
$destLocales = $shopDest->getFranchise()->getLocales()->toArray();
if ([] !== array_diff($srcLocales, $destLocales)) {
return new JsonResponse('INVALID_LOCALES', Response::HTTP_BAD_REQUEST);
}
exec("nohup php {$kernel->getProjectDir()}/bin/console clone-menu {$menu->getId()} {$shopDest->getId()} {$shopDest->getFranchise()->getId()} > /dev/null 2>&1 &");
return new JsonResponse();
}
public function locked(): JsonResponse
{
return new JsonResponse(['isLocked' => $this->isLocked($this->foxordersLocksDirectory)]);
}
}