1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\SyliusLagersystemPlugin\Factory\Order; |
6
|
|
|
|
7
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\Address\AddressViewFactoryInterface; |
8
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\Customer\CustomerViewFactoryInterface; |
9
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\PaymentViewFactoryInterface; |
10
|
|
|
use Setono\SyliusLagersystemPlugin\Factory\ShipmentViewFactoryInterface; |
11
|
|
|
use Setono\SyliusLagersystemPlugin\View\Order\OrderView; |
12
|
|
|
use Sylius\Component\Core\Model\AdjustmentInterface; |
13
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
14
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
15
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
16
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
17
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
18
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
class OrderViewFactory implements OrderViewFactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** @var CustomerViewFactoryInterface */ |
23
|
|
|
protected $customerViewFactory; |
24
|
|
|
|
25
|
|
|
/** @var AddressViewFactoryInterface */ |
26
|
|
|
protected $addressViewFactory; |
27
|
|
|
|
28
|
|
|
/** @var ShipmentViewFactoryInterface */ |
29
|
|
|
protected $shipmentViewFactory; |
30
|
|
|
|
31
|
|
|
/** @var PaymentViewFactoryInterface */ |
32
|
|
|
protected $paymentViewFactory; |
33
|
|
|
|
34
|
|
|
/** @var AdjustmentViewFactoryInterface */ |
35
|
|
|
protected $adjustmentViewFactory; |
36
|
|
|
|
37
|
|
|
/** @var ItemViewFactoryInterface */ |
38
|
|
|
protected $itemFactory; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $orderViewClass; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
CustomerViewFactoryInterface $customerViewFactory, |
45
|
|
|
AddressViewFactoryInterface $addressViewFactory, |
46
|
|
|
ShipmentViewFactoryInterface $shipmentViewFactory, |
47
|
|
|
PaymentViewFactoryInterface $paymentViewFactory, |
48
|
|
|
AdjustmentViewFactoryInterface $adjustmentViewFactory, |
49
|
|
|
ItemViewFactoryInterface $itemFactory, |
50
|
|
|
string $orderViewClass |
51
|
|
|
) { |
52
|
|
|
$this->customerViewFactory = $customerViewFactory; |
53
|
|
|
$this->addressViewFactory = $addressViewFactory; |
54
|
|
|
$this->shipmentViewFactory = $shipmentViewFactory; |
55
|
|
|
$this->paymentViewFactory = $paymentViewFactory; |
56
|
|
|
$this->adjustmentViewFactory = $adjustmentViewFactory; |
57
|
|
|
$this->itemFactory = $itemFactory; |
58
|
|
|
$this->orderViewClass = $orderViewClass; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function create(OrderInterface $order): OrderView |
62
|
|
|
{ |
63
|
|
|
$channel = $order->getChannel(); |
64
|
|
|
Assert::notNull($channel); |
65
|
|
|
|
66
|
|
|
$locale = $channel->getDefaultLocale(); |
|
|
|
|
67
|
|
|
Assert::notNull($locale); |
68
|
|
|
|
69
|
|
|
$localeCode = $locale->getCode(); |
70
|
|
|
Assert::notNull($localeCode); |
71
|
|
|
|
72
|
|
|
$checkoutCompletedAt = $order->getCheckoutCompletedAt(); |
73
|
|
|
Assert::notNull($checkoutCompletedAt); |
74
|
|
|
|
75
|
|
|
/** @var CustomerInterface|null $customer */ |
76
|
|
|
$customer = $order->getCustomer(); |
77
|
|
|
Assert::notNull($customer); |
78
|
|
|
|
79
|
|
|
/** @var OrderView $orderView */ |
80
|
|
|
$orderView = new $this->orderViewClass(); |
81
|
|
|
$orderView->id = $order->getId(); |
82
|
|
|
$orderView->number = $order->getNumber(); |
83
|
|
|
$orderView->channel = $channel->getCode(); |
84
|
|
|
$orderView->currencyCode = $order->getCurrencyCode(); |
85
|
|
|
$orderView->localeCode = $localeCode; |
86
|
|
|
$orderView->state = $order->getState(); |
87
|
|
|
$orderView->checkoutState = $order->getCheckoutState(); |
88
|
|
|
$orderView->checkoutCompletedAt = $checkoutCompletedAt->format('c'); |
89
|
|
|
$orderView->shippingState = $order->getShippingState(); |
90
|
|
|
$orderView->paymentState = $order->getPaymentState(); |
91
|
|
|
|
92
|
|
|
/** @var OrderItemInterface $item */ |
93
|
|
|
foreach ($order->getItems() as $item) { |
94
|
|
|
$orderView->items[] = $this->itemFactory->create( |
95
|
|
|
$item, |
96
|
|
|
$localeCode |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @var ShipmentInterface $shipment */ |
101
|
|
|
foreach ($order->getShipments() as $shipment) { |
102
|
|
|
$orderView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @var PaymentInterface $payment */ |
106
|
|
|
foreach ($order->getPayments() as $payment) { |
107
|
|
|
$orderView->payments[] = $this->paymentViewFactory->create($payment, $localeCode); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (null !== $order->getShippingAddress()) { |
111
|
|
|
$orderView->shippingAddress = $this->addressViewFactory->create($order->getShippingAddress()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (null !== $order->getBillingAddress()) { |
115
|
|
|
$orderView->billingAddress = $this->addressViewFactory->create($order->getBillingAddress()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$adjustments = []; |
119
|
|
|
/** @var AdjustmentInterface $adjustment */ |
120
|
|
|
foreach ($order->getAdjustmentsRecursively() as $adjustment) { |
121
|
|
|
$originCode = $adjustment->getOriginCode(); |
122
|
|
|
$additionalAmount = isset($adjustments[$originCode]) ? $adjustments[$originCode]->amount : 0; |
123
|
|
|
|
124
|
|
|
$adjustments[$originCode] = $this->adjustmentViewFactory->create( |
125
|
|
|
$adjustment, |
126
|
|
|
$additionalAmount |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
$orderView->adjustments = $adjustments; |
130
|
|
|
$orderView->adjustmentsTotal = $order->getAdjustmentsTotalRecursively(); |
131
|
|
|
$orderView->total = $order->getTotal(); |
132
|
|
|
$orderView->customer = $this->customerViewFactory->create($customer); |
133
|
|
|
|
134
|
|
|
return $orderView; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|