OrderPickupLocationRepository.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Delivery;
  6. use Magento\Framework\Exception\CouldNotSaveException;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Temando\Shipping\Api\Data\Delivery\OrderPickupLocationInterface;
  9. use Temando\Shipping\Api\Data\Delivery\OrderPickupLocationInterfaceFactory;
  10. use Temando\Shipping\Model\Delivery\OrderPickupLocation;
  11. use Temando\Shipping\Model\ResourceModel\Delivery\OrderPickupLocation as PickupLocationResource;
  12. use Temando\Shipping\Model\ResourceModel\Repository\OrderPickupLocationRepositoryInterface;
  13. /**
  14. * Temando Order Pickup Location Repository
  15. *
  16. * @package Temando\Shipping\Model
  17. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  18. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link https://www.temando.com/
  20. */
  21. class OrderPickupLocationRepository implements OrderPickupLocationRepositoryInterface
  22. {
  23. /**
  24. * @var PickupLocationResource
  25. */
  26. private $resource;
  27. /**
  28. * @var OrderPickupLocationInterfaceFactory
  29. */
  30. private $pickupLocationFactory;
  31. /**
  32. * OrderPickupLocationRepository constructor.
  33. * @param PickupLocationResource $resource
  34. * @param OrderPickupLocationInterfaceFactory $pickupLocationFactory
  35. */
  36. public function __construct(
  37. PickupLocationResource $resource,
  38. OrderPickupLocationInterfaceFactory $pickupLocationFactory
  39. ) {
  40. $this->resource = $resource;
  41. $this->pickupLocationFactory = $pickupLocationFactory;
  42. }
  43. /**
  44. * Load pickup location by shipping address id.
  45. *
  46. * @param int $addressId
  47. * @return OrderPickupLocationInterface
  48. * @throws NoSuchEntityException
  49. */
  50. public function get($addressId)
  51. {
  52. /** @var OrderPickupLocation $pickupLocation */
  53. $pickupLocation = $this->pickupLocationFactory->create();
  54. try {
  55. $this->resource->load($pickupLocation, $addressId);
  56. } catch (\Exception $exception) {
  57. throw new NoSuchEntityException(__('Pickup location with id "%1" does not exist.', $addressId));
  58. }
  59. if (!$pickupLocation->getRecipientAddressId()) {
  60. throw new NoSuchEntityException(__('Pickup location with id "%1" does not exist.', $addressId));
  61. }
  62. return $pickupLocation;
  63. }
  64. /**
  65. * Save pickup location.
  66. *
  67. * @param OrderPickupLocationInterface $pickupLocation
  68. * @return OrderPickupLocationInterface
  69. * @throws CouldNotSaveException
  70. */
  71. public function save(OrderPickupLocationInterface $pickupLocation)
  72. {
  73. try {
  74. /** @var OrderPickupLocation $pickupLocation */
  75. $this->resource->save($pickupLocation);
  76. } catch (\Exception $exception) {
  77. throw new CouldNotSaveException(__('Unable to save pickup location.'), $exception);
  78. }
  79. return $pickupLocation;
  80. }
  81. }