-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathShipment.php
150 lines (132 loc) · 4.21 KB
/
Shipment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php declare(strict_types = 1);
/**
* Yireo EmailTester for Magento
*
* @author Yireo (https://www.yireo.com/)
* @copyright Copyright 2017 Yireo (https://www.yireo.com/)
* @license Open Source License (OSL v3)
*/
namespace Yireo\EmailTester2\Model\Mailer\Variable;
use Exception;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\PhraseFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
use Magento\Sales\Api\Data\ShipmentItemInterfaceFactory;
use Magento\Sales\Api\ShipmentRepositoryInterface;
use Yireo\EmailTester2\Model\Mailer\VariablesInterface;
class Shipment implements VariablesInterface
{
/**
* @var OrderInterface
*/
private $order;
/**
* @var ShipmentRepositoryInterface
*/
private $shipmentRepository;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var PhraseFactory
*/
private $phraseFactory;
/**
* @var ShipmentItemCreationInterfaceFactory
*/
private $shipmentItemFactory;
/**
* Shipment constructor.
*
* @param ShipmentRepositoryInterface $shipmentRepository
* @param ShipmentItemInterfaceFactory $shipmentItemFactory
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param PhraseFactory $phraseFactory
*/
public function __construct(
ShipmentRepositoryInterface $shipmentRepository,
ShipmentItemInterfaceFactory $shipmentItemFactory,
SearchCriteriaBuilder $searchCriteriaBuilder,
PhraseFactory $phraseFactory
) {
$this->shipmentRepository = $shipmentRepository;
$this->shipmentItemFactory = $shipmentItemFactory;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->phraseFactory = $phraseFactory;
}
/**
* @return array
* @throws NoSuchEntityException
*/
public function getVariables(): array
{
$shipment = $this->getShipment();
return [
'shipment' => $shipment,
'shipment_id' => $shipment->getEntityId(),
];
}
/**
* @return ShipmentInterface
* @throws NoSuchEntityException
*/
private function getShipment(): ShipmentInterface
{
if (empty($this->order)) {
$phrase = $this->phraseFactory->create(['text' => 'Could not find any order entity']);
throw new NoSuchEntityException($phrase);
}
try {
$this->searchCriteriaBuilder->addFilter('order_id', $this->order->getEntityId());
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchCriteria->setCurrentPage(1);
$searchCriteria->setPageSize(1);
$shipments = $this->shipmentRepository->getList($searchCriteria)->getItems();
if ($shipments) {
return $shipments[0];
}
return $this->createDummyShipment();
} catch (Exception $e) {
return $this->createDummyShipment();
}
}
/**
* @return ShipmentInterface
*/
private function createDummyShipment(): ShipmentInterface
{
$shipment = $this->shipmentRepository->create();
$shipmentItems = $this->getShipmentItems($this->order);
$shipment->setItems($shipmentItems);
$shipment->setOrderId($this->order->getEntityId());
return $shipment;
}
/**
* @param OrderInterface $order
* @return array
*/
private function getShipmentItems(OrderInterface $order): array
{
$shipmentItems = [];
$orderItems = $order->getItems();
foreach ($orderItems as $orderItem) {
$item = $this->shipmentItemFactory->create();
$item->setData($orderItem->getData());
$item->setOrderItemId($orderItem->getId());
$item->setQty($orderItem->getQtyOrdered());
$shipmentItems[] = $item;
}
return $shipmentItems;
}
/**
* @param $order OrderInterface
*/
public function setOrder(OrderInterface $order)
{
$this->order = $order;
}
}