<?php
namespace Customize\Controller;
use Customize\Form\Type\Front\AgreementType;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Product;
use Eccube\Repository\ProductRepository;
use Eccube\Service\CartService;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class SeaBurialController extends AbstractController
{
/** @var PurchaseFlow */
protected $purchaseFlow;
/** @var ProductRepository */
protected $productRepository;
/** @var CartService */
protected $cartService;
public function __construct(
PurchaseFlow $purchaseFlow,
ProductRepository $productRepository,
CartService $cartService
) {
$this->purchaseFlow = $purchaseFlow;
$this->productRepository = $productRepository;
$this->cartService = $cartService;
}
/**
* 海洋散骨トップ画面.
*
* @Route("/sea_burial", name="sea_burial", methods={"GET", "POST"})
* @Template("sea_burial.twig")
*/
public function index(Request $request)
{
$builder = $this->formFactory->createBuilder(AgreementType::class);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var \Eccube\Entity\Product|null */
$Product = $this->productRepository->find(Product::PRODUCT_SEA_BURIAL_ID_ON_STG);
if (!$Product) {
throw new NotFoundHttpException();
}
$ProductClass = $Product->getProductClasses()->first();
// カートへ追加
$this->cartService->addProduct($ProductClass, 1, false, false, new \DateTime());
// 明細の正規化
$Carts = $this->cartService->getCarts();
foreach ($Carts as $Cart) {
$result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser()));
// 復旧不可のエラーが発生した場合は追加した明細を削除.
if ($result->hasError()) {
$this->cartService->removeProduct($ProductClass);
foreach ($result->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
}
foreach ($result->getWarning() as $warning) {
$errorMessages[] = $warning->getMessage();
}
}
$this->cartService->save();
return $this->redirectToRoute('cart');
}
return [
'form' => $form->createView(),
];
}
}