|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sylius\SyliusShopApiPlugin\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\AdjustmentInterface; |
|
8
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
9
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
|
10
|
|
|
use Sylius\SyliusShopApiPlugin\View\AdjustmentView; |
|
11
|
|
|
use Sylius\SyliusShopApiPlugin\View\CartSummaryView; |
|
12
|
|
|
|
|
13
|
|
|
final class CartViewFactory implements CartViewFactoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var CartItemViewFactoryInterface */ |
|
16
|
|
|
private $cartItemFactory; |
|
17
|
|
|
|
|
18
|
|
|
/** @var AddressViewFactoryInterface */ |
|
19
|
|
|
private $addressViewFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** @var TotalViewFactoryInterface */ |
|
22
|
|
|
private $totalViewFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ShipmentViewFactoryInterface */ |
|
25
|
|
|
private $shipmentViewFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** @var PaymentViewFactoryInterface */ |
|
28
|
|
|
private $paymentViewFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** @var AdjustmentViewFactoryInterface */ |
|
31
|
|
|
private $adjustmentViewFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $cartSummaryViewClass; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
CartItemViewFactoryInterface $cartItemFactory, |
|
38
|
|
|
AddressViewFactoryInterface $addressViewFactory, |
|
39
|
|
|
TotalViewFactoryInterface $totalViewFactory, |
|
40
|
|
|
ShipmentViewFactoryInterface $shipmentViewFactory, |
|
41
|
|
|
PaymentViewFactoryInterface $paymentViewFactory, |
|
42
|
|
|
AdjustmentViewFactoryInterface $adjustmentViewFactory, |
|
43
|
|
|
string $cartSummaryViewClass |
|
44
|
|
|
) { |
|
45
|
|
|
$this->cartItemFactory = $cartItemFactory; |
|
46
|
|
|
$this->addressViewFactory = $addressViewFactory; |
|
47
|
|
|
$this->totalViewFactory = $totalViewFactory; |
|
48
|
|
|
$this->shipmentViewFactory = $shipmentViewFactory; |
|
49
|
|
|
$this->paymentViewFactory = $paymentViewFactory; |
|
50
|
|
|
$this->adjustmentViewFactory = $adjustmentViewFactory; |
|
51
|
|
|
$this->cartSummaryViewClass = $cartSummaryViewClass; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function create(OrderInterface $cart, string $localeCode): CartSummaryView |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var CartSummaryView $cartView */ |
|
60
|
|
|
$cartView = new $this->cartSummaryViewClass(); |
|
61
|
|
|
$cartView->channel = $cart->getChannel()->getCode(); |
|
62
|
|
|
$cartView->currency = $cart->getCurrencyCode(); |
|
63
|
|
|
$cartView->locale = $localeCode; |
|
64
|
|
|
$cartView->checkoutState = $cart->getCheckoutState(); |
|
65
|
|
|
$cartView->tokenValue = $cart->getTokenValue(); |
|
66
|
|
|
$cartView->totals = $this->totalViewFactory->create($cart); |
|
67
|
|
|
|
|
68
|
|
|
/** @var OrderItemInterface $item */ |
|
69
|
|
|
foreach ($cart->getItems() as $item) { |
|
70
|
|
|
$cartView->items[] = $this->cartItemFactory->create($item, $cart->getChannel(), $localeCode); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
foreach ($cart->getShipments() as $shipment) { |
|
74
|
|
|
$cartView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
foreach ($cart->getPayments() as $payment) { |
|
78
|
|
|
$cartView->payments[] = $this->paymentViewFactory->create($payment, $localeCode); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @var AdjustmentView[] $cartDiscounts */ |
|
82
|
|
|
$cartDiscounts = []; |
|
83
|
|
|
/** @var AdjustmentInterface $adjustment */ |
|
84
|
|
|
foreach ($cart->getAdjustmentsRecursively(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) { |
|
85
|
|
|
$originCode = $adjustment->getOriginCode(); |
|
86
|
|
|
$additionalAmount = isset($cartDiscounts[$originCode]) ? $cartDiscounts[$originCode]->amount->current : 0; |
|
87
|
|
|
|
|
88
|
|
|
$cartDiscounts[$originCode] = $this->adjustmentViewFactory->create($adjustment, $additionalAmount, $cart->getCurrencyCode()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$cartView->cartDiscounts = $cartDiscounts; |
|
92
|
|
|
|
|
93
|
|
|
if (null !== $cart->getShippingAddress()) { |
|
94
|
|
|
$cartView->shippingAddress = $this->addressViewFactory->create($cart->getShippingAddress()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (null !== $cart->getBillingAddress()) { |
|
98
|
|
|
$cartView->billingAddress = $this->addressViewFactory->create($cart->getBillingAddress()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $cartView; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|