app/Customize/Controller/SeaBurialController.php line 44

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Customize\Form\Type\Front\AgreementType;
  4. use Eccube\Controller\AbstractController;
  5. use Eccube\Entity\Product;
  6. use Eccube\Repository\ProductRepository;
  7. use Eccube\Service\CartService;
  8. use Eccube\Service\PurchaseFlow\PurchaseContext;
  9. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class SeaBurialController extends AbstractController
  15. {
  16.     /** @var PurchaseFlow */
  17.     protected $purchaseFlow;
  18.     /** @var ProductRepository */
  19.     protected $productRepository;
  20.     /** @var CartService */
  21.     protected $cartService;
  22.     public function __construct(
  23.         PurchaseFlow $purchaseFlow,
  24.         ProductRepository $productRepository,
  25.         CartService $cartService
  26.     ) {
  27.         $this->purchaseFlow $purchaseFlow;
  28.         $this->productRepository $productRepository;
  29.         $this->cartService $cartService;
  30.     }
  31.     /**
  32.      * 海洋散骨トップ画面.
  33.      *
  34.      * @Route("/sea_burial", name="sea_burial", methods={"GET", "POST"})
  35.      * @Template("sea_burial.twig")
  36.      */
  37.     public function index(Request $request)
  38.     {
  39.         $builder $this->formFactory->createBuilder(AgreementType::class);
  40.         $form $builder->getForm();
  41.         $form->handleRequest($request);
  42.         if ($form->isSubmitted() && $form->isValid()) {
  43.             /** @var \Eccube\Entity\Product|null */
  44.             $Product $this->productRepository->find(Product::PRODUCT_SEA_BURIAL_ID_ON_STG);
  45.             if (!$Product) {
  46.                 throw new NotFoundHttpException();
  47.             }
  48.             $ProductClass $Product->getProductClasses()->first();
  49.             // カートへ追加
  50.             $this->cartService->addProduct($ProductClass1falsefalse, new \DateTime());
  51.             // 明細の正規化
  52.             $Carts $this->cartService->getCarts();
  53.             foreach ($Carts as $Cart) {
  54.                 $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  55.                 // 復旧不可のエラーが発生した場合は追加した明細を削除.
  56.                 if ($result->hasError()) {
  57.                     $this->cartService->removeProduct($ProductClass);
  58.                     foreach ($result->getErrors() as $error) {
  59.                         $errorMessages[] = $error->getMessage();
  60.                     }
  61.                 }
  62.                 foreach ($result->getWarning() as $warning) {
  63.                     $errorMessages[] = $warning->getMessage();
  64.                 }
  65.             }
  66.             $this->cartService->save();
  67.             return $this->redirectToRoute('cart');
  68.         }
  69.         return [
  70.             'form' => $form->createView(),
  71.         ];
  72.     }
  73. }