AddressRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Checkout;
  6. use Magento\Framework\Exception\CouldNotDeleteException;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Temando\Shipping\Api\Data\Checkout\AddressInterface;
  10. use Temando\Shipping\Api\Data\Checkout\AddressInterfaceFactory;
  11. use Temando\Shipping\Model\ResourceModel\Checkout\Address as AddressResource;
  12. use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
  13. /**
  14. * Temando Shipment Repository
  15. *
  16. * @package Temando\Shipping\Model
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link http://www.temando.com/
  21. */
  22. class AddressRepository implements AddressRepositoryInterface
  23. {
  24. /**
  25. * @var AddressResource
  26. */
  27. private $resource;
  28. /**
  29. * @var AddressInterfaceFactory
  30. */
  31. private $addressFactory;
  32. /**
  33. * AddressRepository constructor.
  34. * @param Address $resource
  35. * @param AddressInterfaceFactory $addressFactory
  36. */
  37. public function __construct(
  38. Address $resource,
  39. AddressInterfaceFactory $addressFactory
  40. ) {
  41. $this->resource = $resource;
  42. $this->addressFactory = $addressFactory;
  43. }
  44. /**
  45. * @param int $addressId
  46. * @return AddressInterface
  47. * @throws NoSuchEntityException
  48. */
  49. public function getById($addressId)
  50. {
  51. /** @var \Temando\Shipping\Model\Checkout\Address $address */
  52. $address = $this->addressFactory->create();
  53. $this->resource->load($address, $addressId);
  54. if (!$address->getId()) {
  55. throw new NoSuchEntityException(__('Address with id "%1" does not exist.', $addressId));
  56. }
  57. return $address;
  58. }
  59. /**
  60. * @param int $quoteAddressId
  61. * @return AddressInterface
  62. * @throws NoSuchEntityException
  63. */
  64. public function getByQuoteAddressId($quoteAddressId)
  65. {
  66. $addressId = $this->resource->getIdByQuoteAddressId($quoteAddressId);
  67. return $this->getById($addressId);
  68. }
  69. /**
  70. * @param AddressInterface $address
  71. * @return AddressInterface
  72. * @throws CouldNotSaveException
  73. */
  74. public function save(AddressInterface $address)
  75. {
  76. try {
  77. /** @var \Temando\Shipping\Model\Checkout\Address $address */
  78. $this->resource->save($address);
  79. } catch (\Exception $exception) {
  80. throw new CouldNotSaveException(__('Unable to save shipping services.'), $exception);
  81. }
  82. return $address;
  83. }
  84. /**
  85. * @param string $addressId
  86. * @return bool true on success
  87. * @throws CouldNotDeleteException
  88. */
  89. public function deleteByShippingAddressId($addressId)
  90. {
  91. $addressId = $this->resource->getIdByQuoteAddressId($addressId);
  92. /** @var \Temando\Shipping\Model\Checkout\Address $address */
  93. $address = $this->addressFactory->create(['data' => [
  94. AddressInterface::ENTITY_ID => $addressId
  95. ]]);
  96. return $this->delete($address);
  97. }
  98. /**
  99. * @param AddressInterface $address
  100. * @return bool true on success
  101. * @throws CouldNotDeleteException
  102. */
  103. public function delete(AddressInterface $address)
  104. {
  105. try {
  106. /** @var \Temando\Shipping\Model\Checkout\Address $address */
  107. $this->resource->delete($address);
  108. } catch (\Exception $exception) {
  109. $msg = __('Shipping services for address "%1" could not be deleted.', $address->getEntityId());
  110. throw new CouldNotDeleteException($msg);
  111. }
  112. return true;
  113. }
  114. }