LocationRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Location;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Psr\Log\LoggerInterface;
  8. use Temando\Shipping\Model\LocationInterface;
  9. use Temando\Shipping\Model\ResourceModel\Repository\LocationRepositoryInterface;
  10. use Temando\Shipping\Rest\Adapter\LocationApiInterface;
  11. use Temando\Shipping\Rest\EntityMapper\LocationResponseMapper;
  12. use Temando\Shipping\Rest\Exception\AdapterException;
  13. use Temando\Shipping\Rest\Request\ItemRequestInterfaceFactory;
  14. use Temando\Shipping\Rest\Request\ListRequestInterfaceFactory;
  15. use Temando\Shipping\Rest\Response\DataObject\Location;
  16. use Temando\Shipping\Webservice\Pagination\PaginationFactory;
  17. /**
  18. * Temando Location Repository
  19. *
  20. * @package Temando\Shipping\Model
  21. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  22. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  23. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link https://www.temando.com/
  25. */
  26. class LocationRepository implements LocationRepositoryInterface
  27. {
  28. /**
  29. * @var LocationApiInterface
  30. */
  31. private $apiAdapter;
  32. /**
  33. * @var PaginationFactory
  34. */
  35. private $paginationFactory;
  36. /**
  37. * @var ListRequestInterfaceFactory
  38. */
  39. private $listRequestFactory;
  40. /**
  41. * @var ItemRequestInterfaceFactory
  42. */
  43. private $itemRequestFactory;
  44. /**
  45. * @var LocationResponseMapper
  46. */
  47. private $locationMapper;
  48. /**
  49. * @var LoggerInterface
  50. */
  51. private $logger;
  52. /**
  53. * LocationRepository constructor.
  54. * @param LocationApiInterface $apiAdapter
  55. * @param PaginationFactory $paginationFactory
  56. * @param ListRequestInterfaceFactory $listRequestFactory
  57. * @param ItemRequestInterfaceFactory $itemRequestFactory
  58. * @param LocationResponseMapper $locationMapper
  59. * @param LoggerInterface $logger
  60. */
  61. public function __construct(
  62. LocationApiInterface $apiAdapter,
  63. PaginationFactory $paginationFactory,
  64. ListRequestInterfaceFactory $listRequestFactory,
  65. ItemRequestInterfaceFactory $itemRequestFactory,
  66. LocationResponseMapper $locationMapper,
  67. LoggerInterface $logger
  68. ) {
  69. $this->apiAdapter = $apiAdapter;
  70. $this->paginationFactory = $paginationFactory;
  71. $this->listRequestFactory = $listRequestFactory;
  72. $this->itemRequestFactory = $itemRequestFactory;
  73. $this->locationMapper = $locationMapper;
  74. $this->logger = $logger;
  75. }
  76. /**
  77. * @param int|null $offset
  78. * @param int|null $limit
  79. *
  80. * @return LocationInterface[]
  81. */
  82. public function getList($offset = null, $limit = null)
  83. {
  84. try {
  85. $pagination = $this->paginationFactory->create([
  86. 'offset' => $offset,
  87. 'limit' => $limit,
  88. ]);
  89. $request = $this->listRequestFactory->create([
  90. 'pagination' => $pagination,
  91. ]);
  92. $apiLocations = $this->apiAdapter->getLocations($request);
  93. $locations = array_map(function (Location $apiLocation) {
  94. return $this->locationMapper->map($apiLocation);
  95. }, $apiLocations);
  96. } catch (AdapterException $e) {
  97. $this->logger->critical($e->getMessage(), ['exception' => $e]);
  98. $locations = [];
  99. }
  100. return $locations;
  101. }
  102. /**
  103. * @param string $locationId
  104. *
  105. * @return void
  106. * @throws CouldNotDeleteException
  107. */
  108. public function delete($locationId)
  109. {
  110. try {
  111. $request = $this->itemRequestFactory->create(['entityId' => $locationId]);
  112. $this->apiAdapter->deleteLocation($request);
  113. } catch (AdapterException $e) {
  114. $this->logger->critical($e->getMessage(), ['exception' => $e]);
  115. throw new CouldNotDeleteException(__('Unable to delete location.'), $e);
  116. }
  117. }
  118. }