123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Ui\Model\ResourceModel;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- use Magento\Framework\Api\SearchCriteriaInterface;
- use Magento\Framework\Api\SortOrder;
- use Magento\Ui\Api\BookmarkRepositoryInterface;
- use Magento\Framework\Api\Search\FilterGroup;
- use Magento\Ui\Api\Data\BookmarkInterface;
- use Magento\Ui\Model\ResourceModel\Bookmark\Collection;
- use Magento\Framework\Exception\CouldNotDeleteException;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Class BookmarkRepository
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class BookmarkRepository implements BookmarkRepositoryInterface
- {
- /**
- * @var \Magento\Ui\Api\Data\BookmarkInterfaceFactory
- */
- protected $bookmarkFactory;
- /**
- * @var \Magento\Ui\Model\ResourceModel\Bookmark
- */
- protected $bookmarkResourceModel;
- /**
- * @var \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory
- */
- protected $searchResultsFactory;
- /**
- * @var CollectionProcessorInterface
- */
- private $collectionProcessor;
- /**
- * @param \Magento\Ui\Api\Data\BookmarkInterfaceFactory $bookmarkFactory
- * @param Bookmark $bookmarkResourceModel
- * @param \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory $searchResultsFactory
- * @param CollectionProcessorInterface | null $collectionProcessor
- */
- public function __construct(
- \Magento\Ui\Api\Data\BookmarkInterfaceFactory $bookmarkFactory,
- \Magento\Ui\Model\ResourceModel\Bookmark $bookmarkResourceModel,
- \Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory $searchResultsFactory,
- CollectionProcessorInterface $collectionProcessor = null
- ) {
- $this->bookmarkResourceModel = $bookmarkResourceModel;
- $this->bookmarkFactory = $bookmarkFactory;
- $this->searchResultsFactory = $searchResultsFactory;
- $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
- }
- /**
- * Save bookmark.
- *
- * @param BookmarkInterface $bookmark
- * @return BookmarkInterface
- * @throws CouldNotSaveException
- */
- public function save(BookmarkInterface $bookmark)
- {
- try {
- $this->bookmarkResourceModel->save($bookmark);
- } catch (\Exception $exception) {
- throw new CouldNotSaveException(__($exception->getMessage()));
- }
- return $bookmark;
- }
- /**
- * Retrieve bookmark.
- *
- * @param int $bookmarkId
- * @return BookmarkInterface
- * @throws NoSuchEntityException
- */
- public function getById($bookmarkId)
- {
- $bookmark = $this->bookmarkFactory->create();
- $this->bookmarkResourceModel->load($bookmark, $bookmarkId);
- if (!$bookmark->getId()) {
- throw new NoSuchEntityException(
- __('The bookmark with "%1" ID doesn\'t exist. Verify your information and try again.', $bookmarkId)
- );
- }
- return $bookmark;
- }
- /**
- * Retrieve bookmarks matching the specified criteria.
- *
- * @param SearchCriteriaInterface $searchCriteria
- * @return \Magento\Framework\Api\SearchResultsInterface
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getList(SearchCriteriaInterface $searchCriteria)
- {
- $searchResults = $this->searchResultsFactory->create();
- $searchResults->setSearchCriteria($searchCriteria);
- /** @var \Magento\Ui\Model\ResourceModel\Bookmark\Collection $collection */
- $collection = $this->bookmarkFactory->create()->getCollection();
- $this->collectionProcessor->process($searchCriteria, $collection);
- $searchResults->setTotalCount($collection->getSize());
- $bookmarks = [];
- /** @var BookmarkInterface $bookmark */
- foreach ($collection->getItems() as $bookmark) {
- $bookmarks[] = $this->getById($bookmark->getId());
- }
- $searchResults->setItems($bookmarks);
- return $searchResults;
- }
- /**
- * Delete bookmark.
- *
- * @param BookmarkInterface $bookmark
- * @return bool true on success
- * @throws CouldNotDeleteException
- */
- public function delete(BookmarkInterface $bookmark)
- {
- try {
- $this->bookmarkResourceModel->delete($bookmark);
- } catch (\Exception $exception) {
- throw new CouldNotDeleteException(__($exception->getMessage()));
- }
- return true;
- }
- /**
- * Delete bookmark by ID.
- *
- * @param int $bookmarkId
- * @return bool true on success
- * @throws NoSuchEntityException
- * @throws CouldNotDeleteException
- */
- public function deleteById($bookmarkId)
- {
- return $this->delete($this->getById($bookmarkId));
- }
- /**
- * Helper function that adds a FilterGroup to the collection.
- *
- * @param FilterGroup $filterGroup
- * @param Collection $collection
- * @return void
- * @deprecated 101.0.0
- * @throws \Magento\Framework\Exception\InputException
- */
- protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
- {
- foreach ($filterGroup->getFilters() as $filter) {
- $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
- $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
- }
- }
- /**
- * Retrieve collection processor
- *
- * @deprecated 101.0.0
- * @return CollectionProcessorInterface
- */
- private function getCollectionProcessor()
- {
- if (!$this->collectionProcessor) {
- $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
- CollectionProcessorInterface::class
- );
- }
- return $this->collectionProcessor;
- }
- }
|