OrderCollectionPointRepository.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\OrderCollectionPointInterface;
  9. use Temando\Shipping\Api\Data\Delivery\OrderCollectionPointInterfaceFactory;
  10. use Temando\Shipping\Model\Delivery\OrderCollectionPoint;
  11. use Temando\Shipping\Model\ResourceModel\Delivery\OrderCollectionPoint as CollectionPointResource;
  12. use Temando\Shipping\Model\ResourceModel\Repository\OrderCollectionPointRepositoryInterface;
  13. /**
  14. * Temando Order Collection Point 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 https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link https://www.temando.com/
  21. */
  22. class OrderCollectionPointRepository implements OrderCollectionPointRepositoryInterface
  23. {
  24. /**
  25. * @var CollectionPointResource
  26. */
  27. private $resource;
  28. /**
  29. * @var OrderCollectionPointInterfaceFactory
  30. */
  31. private $collectionPointFactory;
  32. /**
  33. * CollectionPointRepository constructor.
  34. * @param CollectionPointResource $resource
  35. * @param OrderCollectionPointInterfaceFactory $collectionPointFactory
  36. */
  37. public function __construct(
  38. CollectionPointResource $resource,
  39. OrderCollectionPointInterfaceFactory $collectionPointFactory
  40. ) {
  41. $this->resource = $resource;
  42. $this->collectionPointFactory = $collectionPointFactory;
  43. }
  44. /**
  45. * Load collection point by shipping address id.
  46. *
  47. * @param int $addressId
  48. * @return OrderCollectionPointInterface
  49. * @throws NoSuchEntityException
  50. */
  51. public function get($addressId)
  52. {
  53. /** @var OrderCollectionPoint $collectionPoint */
  54. $collectionPoint = $this->collectionPointFactory->create();
  55. try {
  56. $this->resource->load($collectionPoint, $addressId);
  57. } catch (\Exception $exception) {
  58. throw new NoSuchEntityException(__('Collection point with id "%1" does not exist.', $addressId));
  59. }
  60. if (!$collectionPoint->getRecipientAddressId()) {
  61. throw new NoSuchEntityException(__('Collection point with id "%1" does not exist.', $addressId));
  62. }
  63. return $collectionPoint;
  64. }
  65. /**
  66. * Save collection point.
  67. *
  68. * @param OrderCollectionPointInterface $collectionPoint
  69. * @return OrderCollectionPointInterface
  70. * @throws CouldNotSaveException
  71. */
  72. public function save(OrderCollectionPointInterface $collectionPoint)
  73. {
  74. try {
  75. /** @var OrderCollectionPoint $collectionPoint */
  76. $this->resource->save($collectionPoint);
  77. } catch (\Exception $exception) {
  78. throw new CouldNotSaveException(__('Unable to save collection point.'), $exception);
  79. }
  80. return $collectionPoint;
  81. }
  82. }