|
10 | 10 | use Magento\Customer\Api\CustomerRepositoryInterface;
|
11 | 11 | use Magento\Customer\Api\Data\CustomerInterface;
|
12 | 12 | use Magento\Customer\Model\Config\Share;
|
| 13 | +use Magento\Framework\App\ObjectManager; |
13 | 14 | use Magento\Framework\App\RequestInterface;
|
14 | 15 | use Magento\Framework\Data\OptionSourceInterface;
|
15 | 16 | use Magento\Framework\Escaper;
|
| 17 | +use Magento\Framework\Exception\LocalizedException; |
| 18 | +use Magento\Sales\Api\CreditmemoRepositoryInterface; |
| 19 | +use Magento\Sales\Api\InvoiceRepositoryInterface; |
| 20 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 21 | +use Magento\Sales\Api\ShipmentRepositoryInterface; |
16 | 22 | use Magento\Store\Model\Group;
|
17 | 23 | use Magento\Store\Model\System\Store as SystemStore;
|
18 | 24 | use Magento\Store\Model\Website;
|
19 | 25 |
|
20 | 26 | /**
|
21 | 27 | * Store group options for Login As Customer confirmation pop-up.
|
| 28 | + * |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
22 | 30 | */
|
23 | 31 | class Options implements OptionSourceInterface
|
24 | 32 | {
|
@@ -52,37 +60,75 @@ class Options implements OptionSourceInterface
|
52 | 60 | */
|
53 | 61 | private $options;
|
54 | 62 |
|
| 63 | + /** |
| 64 | + * @var OrderRepositoryInterface |
| 65 | + */ |
| 66 | + private $orderRepository; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var InvoiceRepositoryInterface |
| 70 | + */ |
| 71 | + private $invoiceRepository; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var ShipmentRepositoryInterface |
| 75 | + */ |
| 76 | + private $shipmentRepository; |
| 77 | + |
| 78 | + /** |
| 79 | + * @var CreditmemoRepositoryInterface |
| 80 | + */ |
| 81 | + private $creditmemoRepository; |
| 82 | + |
55 | 83 | /**
|
56 | 84 | * @param CustomerRepositoryInterface $customerRepository
|
57 | 85 | * @param Escaper $escaper
|
58 | 86 | * @param RequestInterface $request
|
59 | 87 | * @param Share $share
|
60 | 88 | * @param SystemStore $systemStore
|
| 89 | + * @param OrderRepositoryInterface|null $orderRepository |
| 90 | + * @param InvoiceRepositoryInterface|null $invoiceRepository |
| 91 | + * @param ShipmentRepositoryInterface|null $shipmentRepository |
| 92 | + * @param CreditmemoRepositoryInterface|null $creditmemoRepository |
61 | 93 | */
|
62 | 94 | public function __construct(
|
63 | 95 | CustomerRepositoryInterface $customerRepository,
|
64 | 96 | Escaper $escaper,
|
65 | 97 | RequestInterface $request,
|
66 | 98 | Share $share,
|
67 |
| - SystemStore $systemStore |
| 99 | + SystemStore $systemStore, |
| 100 | + ?OrderRepositoryInterface $orderRepository = null, |
| 101 | + ?InvoiceRepositoryInterface $invoiceRepository = null, |
| 102 | + ?ShipmentRepositoryInterface $shipmentRepository = null, |
| 103 | + ?CreditmemoRepositoryInterface $creditmemoRepository = null |
68 | 104 | ) {
|
69 | 105 | $this->customerRepository = $customerRepository;
|
70 | 106 | $this->escaper = $escaper;
|
71 | 107 | $this->request = $request;
|
72 | 108 | $this->share = $share;
|
73 | 109 | $this->systemStore = $systemStore;
|
| 110 | + $this->orderRepository = $orderRepository |
| 111 | + ?? ObjectManager::getInstance()->get(OrderRepositoryInterface::class); |
| 112 | + $this->invoiceRepository = $invoiceRepository |
| 113 | + ?? ObjectManager::getInstance()->get(InvoiceRepositoryInterface::class); |
| 114 | + $this->shipmentRepository = $shipmentRepository |
| 115 | + ?? ObjectManager::getInstance()->get(ShipmentRepositoryInterface::class); |
| 116 | + $this->creditmemoRepository = $creditmemoRepository |
| 117 | + ?? ObjectManager::getInstance()->get(CreditmemoRepositoryInterface::class); |
74 | 118 | }
|
75 | 119 |
|
76 | 120 | /**
|
77 | 121 | * @inheritdoc
|
| 122 | + * |
| 123 | + * @throws LocalizedException |
78 | 124 | */
|
79 | 125 | public function toOptionArray(): array
|
80 | 126 | {
|
81 | 127 | if ($this->options !== null) {
|
82 | 128 | return $this->options;
|
83 | 129 | }
|
84 | 130 |
|
85 |
| - $customerId = (int)$this->request->getParam('id'); |
| 131 | + $customerId = $this->getCustomerId(); |
86 | 132 | $this->options = $this->generateCurrentOptions($customerId);
|
87 | 133 |
|
88 | 134 | return $this->options;
|
@@ -167,4 +213,50 @@ private function fillStoreGroupOptions(Website $website, CustomerInterface $cust
|
167 | 213 |
|
168 | 214 | return $groups;
|
169 | 215 | }
|
| 216 | + |
| 217 | + /** |
| 218 | + * Get Customer id from request param. |
| 219 | + * |
| 220 | + * @return int |
| 221 | + * @throws LocalizedException |
| 222 | + */ |
| 223 | + private function getCustomerId(): int |
| 224 | + { |
| 225 | + $customerId = $this->request->getParam('id'); |
| 226 | + if ($customerId) { |
| 227 | + return (int)$customerId; |
| 228 | + } |
| 229 | + try { |
| 230 | + $orderId = $this->getOrderId(); |
| 231 | + } catch (LocalizedException $exception) { |
| 232 | + throw new LocalizedException(__('Unable to get Customer ID.')); |
| 233 | + } |
| 234 | + |
| 235 | + return (int)$this->orderRepository->get($orderId)->getCustomerId(); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Get Order id from request param |
| 240 | + * |
| 241 | + * @return int |
| 242 | + * @throws LocalizedException |
| 243 | + */ |
| 244 | + private function getOrderId(): int |
| 245 | + { |
| 246 | + $orderId = $this->request->getParam('order_id'); |
| 247 | + if ($orderId) { |
| 248 | + return (int)$orderId; |
| 249 | + } |
| 250 | + $shipmentId = $this->request->getParam('shipment_id'); |
| 251 | + $creditmemoId = $this->request->getParam('creditmemo_id'); |
| 252 | + $invoiceId = $this->request->getParam('invoice_id'); |
| 253 | + if ($invoiceId) { |
| 254 | + return (int)$this->invoiceRepository->get($invoiceId)->getOrderId(); |
| 255 | + } elseif ($shipmentId) { |
| 256 | + return (int)$this->shipmentRepository->get($shipmentId)->getOrderId(); |
| 257 | + } elseif ($creditmemoId) { |
| 258 | + return (int)$this->creditmemoRepository->get($creditmemoId)->getOrderId(); |
| 259 | + } |
| 260 | + throw new LocalizedException(__('Unable to get Order ID.')); |
| 261 | + } |
170 | 262 | }
|
0 commit comments