Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BookmarkRepository.php
Go to the documentation of this file.
1 <?php
8 
19 
26 {
30  protected $bookmarkFactory;
31 
36 
41 
45  private $collectionProcessor;
46 
53  public function __construct(
54  \Magento\Ui\Api\Data\BookmarkInterfaceFactory $bookmarkFactory,
56  \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory $searchResultsFactory,
57  CollectionProcessorInterface $collectionProcessor = null
58  ) {
59 
60  $this->bookmarkResourceModel = $bookmarkResourceModel;
61  $this->bookmarkFactory = $bookmarkFactory;
62  $this->searchResultsFactory = $searchResultsFactory;
63  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
64  }
65 
73  public function save(BookmarkInterface $bookmark)
74  {
75  try {
76  $this->bookmarkResourceModel->save($bookmark);
77  } catch (\Exception $exception) {
78  throw new CouldNotSaveException(__($exception->getMessage()));
79  }
80  return $bookmark;
81  }
82 
90  public function getById($bookmarkId)
91  {
92  $bookmark = $this->bookmarkFactory->create();
93  $this->bookmarkResourceModel->load($bookmark, $bookmarkId);
94  if (!$bookmark->getId()) {
95  throw new NoSuchEntityException(
96  __('The bookmark with "%1" ID doesn\'t exist. Verify your information and try again.', $bookmarkId)
97  );
98  }
99  return $bookmark;
100  }
101 
110  {
111  $searchResults = $this->searchResultsFactory->create();
112  $searchResults->setSearchCriteria($searchCriteria);
113 
115  $collection = $this->bookmarkFactory->create()->getCollection();
116  $this->collectionProcessor->process($searchCriteria, $collection);
117  $searchResults->setTotalCount($collection->getSize());
118 
119  $bookmarks = [];
121  foreach ($collection->getItems() as $bookmark) {
122  $bookmarks[] = $this->getById($bookmark->getId());
123  }
124  $searchResults->setItems($bookmarks);
125 
126  return $searchResults;
127  }
128 
136  public function delete(BookmarkInterface $bookmark)
137  {
138  try {
139  $this->bookmarkResourceModel->delete($bookmark);
140  } catch (\Exception $exception) {
141  throw new CouldNotDeleteException(__($exception->getMessage()));
142  }
143  return true;
144  }
145 
154  public function deleteById($bookmarkId)
155  {
156  return $this->delete($this->getById($bookmarkId));
157  }
158 
169  {
170  foreach ($filterGroup->getFilters() as $filter) {
171  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
172  $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
173  }
174  }
175 
182  private function getCollectionProcessor()
183  {
184  if (!$this->collectionProcessor) {
185  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
186  CollectionProcessorInterface::class
187  );
188  }
189  return $this->collectionProcessor;
190  }
191 }
$bookmarks
Definition: bookmarks.php:10
__()
Definition: __.php:13
$searchCriteria
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
__construct(\Magento\Ui\Api\Data\BookmarkInterfaceFactory $bookmarkFactory, \Magento\Ui\Model\ResourceModel\Bookmark $bookmarkResourceModel, \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory $searchResultsFactory, CollectionProcessorInterface $collectionProcessor=null)
addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)