BookmarkRepository.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Model\ResourceModel;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\Api\SearchCriteriaInterface;
  9. use Magento\Framework\Api\SortOrder;
  10. use Magento\Ui\Api\BookmarkRepositoryInterface;
  11. use Magento\Framework\Api\Search\FilterGroup;
  12. use Magento\Ui\Api\Data\BookmarkInterface;
  13. use Magento\Ui\Model\ResourceModel\Bookmark\Collection;
  14. use Magento\Framework\Exception\CouldNotDeleteException;
  15. use Magento\Framework\Exception\CouldNotSaveException;
  16. use Magento\Framework\Exception\NoSuchEntityException;
  17. /**
  18. * Class BookmarkRepository
  19. *
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class BookmarkRepository implements BookmarkRepositoryInterface
  23. {
  24. /**
  25. * @var \Magento\Ui\Api\Data\BookmarkInterfaceFactory
  26. */
  27. protected $bookmarkFactory;
  28. /**
  29. * @var \Magento\Ui\Model\ResourceModel\Bookmark
  30. */
  31. protected $bookmarkResourceModel;
  32. /**
  33. * @var \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory
  34. */
  35. protected $searchResultsFactory;
  36. /**
  37. * @var CollectionProcessorInterface
  38. */
  39. private $collectionProcessor;
  40. /**
  41. * @param \Magento\Ui\Api\Data\BookmarkInterfaceFactory $bookmarkFactory
  42. * @param Bookmark $bookmarkResourceModel
  43. * @param \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory $searchResultsFactory
  44. * @param CollectionProcessorInterface | null $collectionProcessor
  45. */
  46. public function __construct(
  47. \Magento\Ui\Api\Data\BookmarkInterfaceFactory $bookmarkFactory,
  48. \Magento\Ui\Model\ResourceModel\Bookmark $bookmarkResourceModel,
  49. \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory $searchResultsFactory,
  50. CollectionProcessorInterface $collectionProcessor = null
  51. ) {
  52. $this->bookmarkResourceModel = $bookmarkResourceModel;
  53. $this->bookmarkFactory = $bookmarkFactory;
  54. $this->searchResultsFactory = $searchResultsFactory;
  55. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  56. }
  57. /**
  58. * Save bookmark.
  59. *
  60. * @param BookmarkInterface $bookmark
  61. * @return BookmarkInterface
  62. * @throws CouldNotSaveException
  63. */
  64. public function save(BookmarkInterface $bookmark)
  65. {
  66. try {
  67. $this->bookmarkResourceModel->save($bookmark);
  68. } catch (\Exception $exception) {
  69. throw new CouldNotSaveException(__($exception->getMessage()));
  70. }
  71. return $bookmark;
  72. }
  73. /**
  74. * Retrieve bookmark.
  75. *
  76. * @param int $bookmarkId
  77. * @return BookmarkInterface
  78. * @throws NoSuchEntityException
  79. */
  80. public function getById($bookmarkId)
  81. {
  82. $bookmark = $this->bookmarkFactory->create();
  83. $this->bookmarkResourceModel->load($bookmark, $bookmarkId);
  84. if (!$bookmark->getId()) {
  85. throw new NoSuchEntityException(
  86. __('The bookmark with "%1" ID doesn\'t exist. Verify your information and try again.', $bookmarkId)
  87. );
  88. }
  89. return $bookmark;
  90. }
  91. /**
  92. * Retrieve bookmarks matching the specified criteria.
  93. *
  94. * @param SearchCriteriaInterface $searchCriteria
  95. * @return \Magento\Framework\Api\SearchResultsInterface
  96. * @throws \Magento\Framework\Exception\LocalizedException
  97. */
  98. public function getList(SearchCriteriaInterface $searchCriteria)
  99. {
  100. $searchResults = $this->searchResultsFactory->create();
  101. $searchResults->setSearchCriteria($searchCriteria);
  102. /** @var \Magento\Ui\Model\ResourceModel\Bookmark\Collection $collection */
  103. $collection = $this->bookmarkFactory->create()->getCollection();
  104. $this->collectionProcessor->process($searchCriteria, $collection);
  105. $searchResults->setTotalCount($collection->getSize());
  106. $bookmarks = [];
  107. /** @var BookmarkInterface $bookmark */
  108. foreach ($collection->getItems() as $bookmark) {
  109. $bookmarks[] = $this->getById($bookmark->getId());
  110. }
  111. $searchResults->setItems($bookmarks);
  112. return $searchResults;
  113. }
  114. /**
  115. * Delete bookmark.
  116. *
  117. * @param BookmarkInterface $bookmark
  118. * @return bool true on success
  119. * @throws CouldNotDeleteException
  120. */
  121. public function delete(BookmarkInterface $bookmark)
  122. {
  123. try {
  124. $this->bookmarkResourceModel->delete($bookmark);
  125. } catch (\Exception $exception) {
  126. throw new CouldNotDeleteException(__($exception->getMessage()));
  127. }
  128. return true;
  129. }
  130. /**
  131. * Delete bookmark by ID.
  132. *
  133. * @param int $bookmarkId
  134. * @return bool true on success
  135. * @throws NoSuchEntityException
  136. * @throws CouldNotDeleteException
  137. */
  138. public function deleteById($bookmarkId)
  139. {
  140. return $this->delete($this->getById($bookmarkId));
  141. }
  142. /**
  143. * Helper function that adds a FilterGroup to the collection.
  144. *
  145. * @param FilterGroup $filterGroup
  146. * @param Collection $collection
  147. * @return void
  148. * @deprecated 101.0.0
  149. * @throws \Magento\Framework\Exception\InputException
  150. */
  151. protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
  152. {
  153. foreach ($filterGroup->getFilters() as $filter) {
  154. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  155. $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
  156. }
  157. }
  158. /**
  159. * Retrieve collection processor
  160. *
  161. * @deprecated 101.0.0
  162. * @return CollectionProcessorInterface
  163. */
  164. private function getCollectionProcessor()
  165. {
  166. if (!$this->collectionProcessor) {
  167. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  168. CollectionProcessorInterface::class
  169. );
  170. }
  171. return $this->collectionProcessor;
  172. }
  173. }