CustomerLinkRepository.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Login\Model;
  17. use Magento\Framework\Api\FilterBuilder;
  18. use Magento\Framework\Api\Search\FilterGroup;
  19. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  20. use Magento\Framework\Api\SearchCriteriaBuilder;
  21. use Amazon\Login\Api\CustomerLinkRepositoryInterface;
  22. use Amazon\Login\Api\Data;
  23. use Amazon\Login\Api\Data\CustomerLinkInterface;
  24. use Amazon\Login\Api\Data\CustomerLinkSearchResultsInterfaceFactory;
  25. use Amazon\Login\Model\ResourceModel\CustomerLink as CustomerLinkResourceModel;
  26. use Amazon\Login\Model\ResourceModel\CustomerLink\CollectionFactory;
  27. /**
  28. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  29. */
  30. class CustomerLinkRepository implements CustomerLinkRepositoryInterface
  31. {
  32. /**
  33. * @var CustomerLinkResourceModel
  34. */
  35. private $resourceModel;
  36. /**
  37. * @var CustomerLinkFactory
  38. */
  39. private $customerLinkFactory;
  40. /**
  41. * @var PaymentTokenSearchResultsInterfaceFactory
  42. */
  43. private $searchResultsFactory;
  44. /**
  45. * @var \Magento\Framework\Api\FilterBuilder
  46. */
  47. private $filterBuilder;
  48. /**
  49. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  50. */
  51. private $searchCriteriaBuilder;
  52. /**
  53. * @var CollectionFactory
  54. */
  55. private $collectionFactory;
  56. /**
  57. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  58. */
  59. private $collectionProcessor;
  60. /**
  61. * @param CustomerLinkResourceModel $customerLinkFactory
  62. * @param CustomerLinkFactory $resourceModel
  63. * @param FilterBuilder $filterBuilder
  64. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  65. * @param PaymentTokenSearchResultsInterfaceFactory $searchResultsFactory
  66. * @param CollectionFactory $collectionFactory
  67. * @param CollectionProcessorInterface | null $collectionProcessor
  68. */
  69. public function __construct(
  70. CustomerLinkResourceModel $resourceModel,
  71. CustomerLinkFactory $customerLinkFactory,
  72. FilterBuilder $filterBuilder,
  73. SearchCriteriaBuilder $searchCriteriaBuilder,
  74. CustomerLinkSearchResultsInterfaceFactory $searchResultsFactory,
  75. CollectionFactory $collectionFactory,
  76. CollectionProcessorInterface $collectionProcessor
  77. ) {
  78. $this->resourceModel = $resourceModel;
  79. $this->customerLinkFactory = $customerLinkFactory;
  80. $this->filterBuilder = $filterBuilder;
  81. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  82. $this->searchResultsFactory = $searchResultsFactory;
  83. $this->collectionFactory = $collectionFactory;
  84. $this->collectionProcessor = $collectionProcessor;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function get($customerId)
  90. {
  91. $customerLink = $this->customerLinkFactory->create();
  92. $this->resourceModel->load($customerLink, $customerId, 'customer_id');
  93. return $customerLink;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getById($entityId)
  99. {
  100. $customerLink = $this->customerLinkFactory->create();
  101. $this->resourceModel->load($customerLink, $entityId);
  102. return $customerLink;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  108. {
  109. $collection = $this->collectionFactory->create();
  110. $this->collectionProcessor->process($searchCriteria, $collection);
  111. $searchResults = $this->searchResultsFactory->create();
  112. $searchResults->setSearchCriteria($searchCriteria);
  113. $searchResults->setItems($collection->getItems());
  114. return $searchResults;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function delete(CustomerLinkInterface $customerLink)
  120. {
  121. try {
  122. $this->resourceModel->delete($customerLink);
  123. } catch (\Exception $exception) {
  124. throw new \Magento\Framework\Exception\CouldNotDeleteException(__($exception->getMessage()));
  125. }
  126. return true;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function deleteById($entityId)
  132. {
  133. return $this->delete($this->getById($entityId));
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function save(CustomerLinkInterface $customerLink)
  139. {
  140. try {
  141. $this->resourceModel->save($customerLink);
  142. } catch (\Exception $exception) {
  143. throw new \Magento\Framework\Exception\CouldNotSaveException(
  144. __('Could not save Amazon customer link: %1', $exception->getMessage()),
  145. $exception
  146. );
  147. }
  148. return $customerLink;
  149. }
  150. }