PickupLocationSearchRequestRepository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\CouldNotDeleteException;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchRequestInterface;
  10. use Temando\Shipping\Api\Data\Delivery\PickupLocationSearchRequestInterfaceFactory;
  11. use Temando\Shipping\Model\Delivery\PickupLocationSearchRequest;
  12. use Temando\Shipping\Model\ResourceModel\Delivery\PickupLocationSearchRequest as SearchRequestResource;
  13. use Temando\Shipping\Model\ResourceModel\Repository\PickupLocationSearchRepositoryInterface;
  14. /**
  15. * Temando Pickup Location Search Request Repository
  16. *
  17. * @package Temando\Shipping\Model
  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 PickupLocationSearchRequestRepository implements PickupLocationSearchRepositoryInterface
  23. {
  24. /**
  25. * @var SearchRequestResource
  26. */
  27. private $resource;
  28. /**
  29. * @var PickupLocationSearchRequestInterfaceFactory
  30. */
  31. private $searchRequestFactory;
  32. /**
  33. * SearchRequestRepository constructor.
  34. * @param SearchRequestResource $resource
  35. * @param PickupLocationSearchRequestInterfaceFactory $searchRequestFactory
  36. */
  37. public function __construct(
  38. SearchRequestResource $resource,
  39. PickupLocationSearchRequestInterfaceFactory $searchRequestFactory
  40. ) {
  41. $this->resource = $resource;
  42. $this->searchRequestFactory = $searchRequestFactory;
  43. }
  44. /**
  45. * @param PickupLocationSearchRequestInterface $searchRequest
  46. * @return PickupLocationSearchRequestInterface
  47. * @throws CouldNotSaveException
  48. */
  49. public function save($searchRequest)
  50. {
  51. try {
  52. /** @var PickupLocationSearchRequest $searchRequest */
  53. $this->resource->save($searchRequest);
  54. } catch (\Exception $exception) {
  55. throw new CouldNotSaveException(__('Unable to save search parameters.'), $exception);
  56. }
  57. return $searchRequest;
  58. }
  59. /**
  60. * @param int $quoteAddressId
  61. * @return PickupLocationSearchRequestInterface
  62. * @throws NoSuchEntityException
  63. */
  64. public function get($quoteAddressId)
  65. {
  66. /** @var PickupLocationSearchRequest $searchRequest */
  67. $searchRequest = $this->searchRequestFactory->create();
  68. $this->resource->load($searchRequest, $quoteAddressId);
  69. if (!$searchRequest->getShippingAddressId()) {
  70. throw new NoSuchEntityException(__('Search request for address id "%1" does not exist.', $quoteAddressId));
  71. }
  72. return $searchRequest;
  73. }
  74. /**
  75. * @param int $quoteAddressId
  76. * @return bool
  77. * @throws CouldNotDeleteException
  78. */
  79. public function delete($quoteAddressId)
  80. {
  81. $searchRequest = $this->searchRequestFactory->create(['data' => [
  82. PickupLocationSearchRequestInterface::SHIPPING_ADDRESS_ID => $quoteAddressId,
  83. ]]);
  84. try {
  85. /** @var $searchRequest PickupLocationSearchRequest */
  86. $this->resource->delete($searchRequest);
  87. } catch (\Exception $exception) {
  88. $msg = __('Search request for address id "%1" could not be deleted.', $quoteAddressId);
  89. throw new CouldNotDeleteException($msg);
  90. }
  91. return $searchRequest->isDeleted();
  92. }
  93. }