src/Component/Wishlist/Controller/Action/CustomWishlistController.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Component\Wishlist\Controller\Action;
  4. use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
  5. use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
  6. use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
  7. use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Sylius\Component\Order\Context\CartContextInterface;
  10. use Sylius\Component\Order\Context\CartNotFoundException;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Twig\Environment;
  17. final class CustomWishlistController
  18. {
  19.     public function __construct(
  20.         private CartContextInterface $cartContext,
  21.         private FormFactoryInterface $formFactory,
  22.         private Environment $twigEnvironment,
  23.         private WishlistCommandProcessorInterface $wishlistCommandProcessor,
  24.         private WishlistsResolverInterface $wishlistsResolver,
  25.         private UrlGeneratorInterface $generator,
  26.         private EntityManagerInterface $entityManager
  27.     ) {
  28.     }
  29.     public function __invoke(Request $request): Response
  30.     {
  31.         $wishlists $this->wishlistsResolver->resolveAndCreate();
  32.         /** @var WishlistInterface|null $wishlist */
  33.         $wishlist array_shift($wishlists);
  34.         if (null === $wishlist) {
  35.             $homepageUrl $this->generator->generate('sylius_shop_homepage');
  36.             return new RedirectResponse($homepageUrl);
  37.         }
  38.         $wishlist->setName('List');
  39.         $this->entityManager->persist($wishlist);
  40.         $this->entityManager->flush();
  41.         try {
  42.             $cart $this->cartContext->getCart();
  43.         } catch (CartNotFoundException $exception) {
  44.             $cart null;
  45.         }
  46.         $commandsArray $this->wishlistCommandProcessor->createWishlistItemsCollection($wishlist->getWishlistProducts());
  47.         $form $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
  48.             'cart' => $cart,
  49.         ]);
  50.         return new Response(
  51.             $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
  52.                 'wishlist' => $wishlist,
  53.                 'form' => $form->createView(),
  54.             ])
  55.         );
  56.     }
  57. }