1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Controller\Block; |
15
|
|
|
|
16
|
|
|
use Eccube\Controller\AbstractController; |
17
|
|
|
use Eccube\Entity\Cart; |
18
|
|
|
use Eccube\Service\CartService; |
19
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseContext; |
20
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseFlow; |
21
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseFlowResult; |
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
|
25
|
|
|
class CartController extends AbstractController |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var CartService |
29
|
|
|
*/ |
30
|
|
|
protected $cartService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var PurchaseFlow |
34
|
|
|
*/ |
35
|
|
|
protected $purchaseFlow; |
36
|
|
|
|
37
|
114 |
|
public function __construct( |
38
|
|
|
CartService $cartService, |
39
|
|
|
PurchaseFlow $cartPurchaseFlow |
40
|
|
|
) { |
41
|
114 |
|
$this->cartService = $cartService; |
42
|
114 |
|
$this->purchaseFlow = $cartPurchaseFlow; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @Route("/block/cart", name="block_cart") |
47
|
|
|
* @Route("/block/cart_sp", name="block_cart_sp") |
48
|
|
|
*/ |
|
|
|
|
49
|
114 |
|
public function index(Request $request) |
50
|
|
|
{ |
51
|
114 |
|
$Carts = $this->cartService->getCarts(); |
52
|
|
|
|
53
|
|
|
// 二重に実行され, 注文画面でのエラーハンドリングができないので |
54
|
|
|
// ここではpurchaseFlowは実行しない |
55
|
|
|
|
56
|
114 |
|
$totalQuantity = array_reduce($Carts, function ($total, $Cart) { |
57
|
|
|
/* @var Cart $Cart */ |
58
|
71 |
|
$total += $Cart->getTotalQuantity(); |
59
|
|
|
|
60
|
71 |
|
return $total; |
61
|
114 |
|
}, 0); |
62
|
114 |
|
$totalPrice = array_reduce($Carts, function ($total, $Cart) { |
63
|
|
|
/* @var Cart $Cart */ |
64
|
71 |
|
$total += $Cart->getTotalPrice(); |
65
|
|
|
|
66
|
71 |
|
return $total; |
67
|
114 |
|
}, 0); |
68
|
|
|
|
69
|
114 |
|
$route = $request->attributes->get('_route'); |
70
|
|
|
|
71
|
114 |
|
if ($route == 'block_cart_sp') { |
72
|
113 |
|
return $this->render('Block/nav_sp.twig', [ |
73
|
113 |
|
'totalQuantity' => $totalQuantity, |
74
|
113 |
|
'totalPrice' => $totalPrice, |
75
|
113 |
|
'Carts' => $Carts, |
76
|
|
|
]); |
77
|
|
|
} else { |
78
|
114 |
|
return $this->render('Block/cart.twig', [ |
79
|
114 |
|
'totalQuantity' => $totalQuantity, |
80
|
114 |
|
'totalPrice' => $totalPrice, |
81
|
114 |
|
'Carts' => $Carts, |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function execPurchaseFlow($Carts) |
87
|
|
|
{ |
88
|
|
|
/** @var PurchaseFlowResult[] $flowResults */ |
89
|
|
|
$flowResults = array_map(function ($Cart) { |
|
|
|
|
90
|
|
|
$purchaseContext = new PurchaseContext($Cart, $this->getUser()); |
91
|
|
|
|
92
|
|
|
return $this->purchaseFlow->validate($Cart, $purchaseContext); |
93
|
|
|
}, $Carts); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|