PickupRepository.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Pickup;
  6. use Magento\Framework\Api\SearchCriteriaInterface;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Psr\Log\LoggerInterface;
  11. use Temando\Shipping\Model\PickupInterface;
  12. use Temando\Shipping\Model\ResourceModel\Order\OrderReference;
  13. use Temando\Shipping\Model\ResourceModel\Repository\PickupRepositoryInterface;
  14. use Temando\Shipping\Rest\Adapter\FulfillmentApiInterface;
  15. use Temando\Shipping\Rest\EntityMapper\FulfillmentResponseMapper;
  16. use Temando\Shipping\Rest\Exception\AdapterException;
  17. use Temando\Shipping\Rest\Filter\FilterConverter;
  18. use Temando\Shipping\Rest\Pagination\PaginationFactory;
  19. use Temando\Shipping\Rest\Request\FulfillmentRequestInterfaceFactory;
  20. use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
  21. use Temando\Shipping\Rest\Request\ListRequestInterfaceFactory;
  22. use Temando\Shipping\Rest\Request\Type\FulfillmentRequestTypeInterfaceFactory;
  23. use Temando\Shipping\Rest\Response\DataObject\Fulfillment;
  24. /**
  25. * Temando Pickup Fulfillment Repository
  26. *
  27. * @package Temando\Shipping\Model
  28. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  29. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  30. * @link https://www.temando.com/
  31. */
  32. class PickupRepository implements PickupRepositoryInterface
  33. {
  34. /**
  35. * @var FulfillmentApiInterface
  36. */
  37. private $apiAdapter;
  38. /**
  39. * @var FilterConverter
  40. */
  41. private $filterConverter;
  42. /**
  43. * @var PaginationFactory
  44. */
  45. private $paginationFactory;
  46. /**
  47. * @var OrderReference
  48. */
  49. private $orderReferenceResource;
  50. /**
  51. * @var ListRequestInterfaceFactory
  52. */
  53. private $listRequestFactory;
  54. /**
  55. * @var ItemRequestInterfaceFactory
  56. */
  57. private $itemRequestFactory;
  58. /**
  59. * @var FulfillmentRequestInterfaceFactory
  60. */
  61. private $fulfillmentRequestFactory;
  62. /**
  63. * @var FulfillmentRequestTypeInterfaceFactory
  64. */
  65. private $requestTypeFactory;
  66. /**
  67. * @var FulfillmentResponseMapper
  68. */
  69. private $fulfillmentMapper;
  70. /**
  71. * @var LoggerInterface
  72. */
  73. private $logger;
  74. /**
  75. * PickupRepository constructor.
  76. * @param FulfillmentApiInterface $apiAdapter
  77. * @param FilterConverter $filterConverter
  78. * @param PaginationFactory $paginationFactory
  79. * @param OrderReference $orderReferenceResource
  80. * @param ListRequestInterfaceFactory $listRequestFactory
  81. * @param ItemRequestInterfaceFactory $itemRequestFactory
  82. * @param FulfillmentRequestInterfaceFactory $fulfillmentRequestFactory
  83. * @param FulfillmentRequestTypeInterfaceFactory $requestTypeFactory
  84. * @param FulfillmentResponseMapper $fulfillmentMapper
  85. * @param LoggerInterface $logger
  86. */
  87. public function __construct(
  88. FulfillmentApiInterface $apiAdapter,
  89. FilterConverter $filterConverter,
  90. PaginationFactory $paginationFactory,
  91. OrderReference $orderReferenceResource,
  92. ListRequestInterfaceFactory $listRequestFactory,
  93. ItemRequestInterfaceFactory $itemRequestFactory,
  94. FulfillmentRequestInterfaceFactory $fulfillmentRequestFactory,
  95. FulfillmentRequestTypeInterfaceFactory $requestTypeFactory,
  96. FulfillmentResponseMapper $fulfillmentMapper,
  97. LoggerInterface $logger
  98. ) {
  99. $this->apiAdapter = $apiAdapter;
  100. $this->filterConverter = $filterConverter;
  101. $this->paginationFactory = $paginationFactory;
  102. $this->orderReferenceResource = $orderReferenceResource;
  103. $this->listRequestFactory = $listRequestFactory;
  104. $this->itemRequestFactory = $itemRequestFactory;
  105. $this->fulfillmentRequestFactory = $fulfillmentRequestFactory;
  106. $this->requestTypeFactory = $requestTypeFactory;
  107. $this->fulfillmentMapper = $fulfillmentMapper;
  108. $this->logger = $logger;
  109. }
  110. /**
  111. * Filter callbacks allow to change values prior to sending them to the platform.
  112. *
  113. * @return \Closure[]
  114. */
  115. private function getFilterCallbacks()
  116. {
  117. // convert local order id to platform order id
  118. $orderFilterCallback = function ($salesOrderId) {
  119. return $this->orderReferenceResource->getExtOrderIdByOrderId($salesOrderId);
  120. };
  121. return [
  122. PickupInterface::ORDER_ID => $orderFilterCallback,
  123. ];
  124. }
  125. /**
  126. * Load pickup fulfillment by entity id.
  127. *
  128. * @param string $pickupId
  129. * @return PickupInterface
  130. * @throws NoSuchEntityException
  131. * @throws LocalizedException
  132. */
  133. public function getById($pickupId)
  134. {
  135. if (!$pickupId) {
  136. throw new LocalizedException(__('An error occurred while loading data.'));
  137. }
  138. try {
  139. $request = $this->itemRequestFactory->create(['entityId' => $pickupId]);
  140. $apiFulfillment = $this->apiAdapter->getFulfillment($request);
  141. $pickup = $this->fulfillmentMapper->mapPickup($apiFulfillment);
  142. } catch (AdapterException $e) {
  143. if ($e->getCode() === 404) {
  144. throw NoSuchEntityException::singleField('fulfillmentId', $pickupId);
  145. }
  146. throw new LocalizedException(__('An error occurred while loading data.'), $e);
  147. }
  148. return $pickup;
  149. }
  150. /**
  151. * Load pickup fulfillments.
  152. *
  153. * @param SearchCriteriaInterface $criteria
  154. * @return PickupInterface[]
  155. */
  156. public function getList(SearchCriteriaInterface $criteria)
  157. {
  158. try {
  159. $pagination = $this->paginationFactory->create([
  160. 'offset' => 0,
  161. 'limit' => 1000,
  162. ]);
  163. $filter = $this->filterConverter->convert(
  164. $criteria->getFilterGroups(),
  165. $this->getFilterCallbacks()
  166. );
  167. $request = $this->listRequestFactory->create([
  168. 'pagination' => $pagination,
  169. 'filter' => $filter,
  170. ]);
  171. $apiFulfillments = $this->apiAdapter->getFulfillments($request);
  172. } catch (LocalizedException $e) {
  173. $this->logger->error($e->getLogMessage(), ['exception' => $e]);
  174. $apiFulfillments = [];
  175. } catch (AdapterException $e) {
  176. $this->logger->critical($e->getMessage(), ['exception' => $e]);
  177. $apiFulfillments = [];
  178. }
  179. $pickups = array_map(function (Fulfillment $apiFulfillment) {
  180. return $this->fulfillmentMapper->mapPickup($apiFulfillment);
  181. }, $apiFulfillments);
  182. return $pickups;
  183. }
  184. /**
  185. * Save pickup fulfillment.
  186. *
  187. * @param PickupInterface $pickup
  188. * @return PickupInterface
  189. * @throws CouldNotSaveException
  190. */
  191. public function save(PickupInterface $pickup)
  192. {
  193. // build fulfillment request type
  194. $fulfillmentType = $this->requestTypeFactory->create([
  195. 'id' => $pickup->getPickupId(),
  196. 'type' => 'fulfillment-pickup',
  197. 'reference' => $pickup->getOrderReference(),
  198. 'state' => $pickup->getState(),
  199. 'orderId' => $pickup->getOrderId(),
  200. 'pickupLocationId' => $pickup->getLocationId(),
  201. 'items' => $pickup->getItems() ?: [],
  202. ]);
  203. $fulfillmentRequest = $this->fulfillmentRequestFactory->create([
  204. 'fulfillment' => $fulfillmentType,
  205. ]);
  206. try {
  207. if ($pickup->getPickupId()) {
  208. $apiFulfillment = $this->apiAdapter->updateFulfillment($fulfillmentRequest);
  209. } else {
  210. $apiFulfillment = $this->apiAdapter->createFulfillment($fulfillmentRequest);
  211. }
  212. $fulfillment = $this->fulfillmentMapper->mapPickup($apiFulfillment);
  213. } catch (AdapterException $e) {
  214. throw new CouldNotSaveException(__('Unable to save pickup.'), $e);
  215. }
  216. return $fulfillment;
  217. }
  218. }