resource = $resource; $this->subItemFactory = $subItemFactory; $this->subItemCollectionFactory = $subItemCollectionFactory; $this->searchResultsFactory = $searchResultsFactory; $this->collectionProcessor = $collectionProcessor; } /** * @inheritDoc */ public function save(SubItemInterface $subItem) { try { $this->resource->save($subItem); } catch (\Exception $exception) { throw new CouldNotSaveException(__( 'Could not save the subItem: %1', $exception->getMessage() )); } return $subItem; } /** * @inheritDoc */ public function get($subItemId) { $subItem = $this->subItemFactory->create(); $this->resource->load($subItem, $subItemId); if (!$subItem->getId()) { throw new NoSuchEntityException(__('SubItem with id "%1" does not exist.', $subItemId)); } return $subItem; } /** * @inheritDoc */ public function getList( \Magento\Framework\Api\SearchCriteriaInterface $criteria ) { $collection = $this->subItemCollectionFactory->create(); $this->collectionProcessor->process($criteria, $collection); $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($criteria); $items = []; foreach ($collection as $model) { $items[] = $model; } $searchResults->setItems($items); $searchResults->setTotalCount($collection->getSize()); return $searchResults; } /** * @inheritDoc */ public function delete(SubItemInterface $subItem) { try { $subItemModel = $this->subItemFactory->create(); $this->resource->load($subItemModel, $subItem->getSubitemId()); $this->resource->delete($subItemModel); } catch (\Exception $exception) { throw new CouldNotDeleteException(__( 'Could not delete the SubItem: %1', $exception->getMessage() )); } return true; } /** * @inheritDoc */ public function deleteById($subItemId) { return $this->delete($this->get($subItemId)); } }