resource = $resource; $this->avatrFactory = $avatrFactory; $this->avatrCollectionFactory = $avatrCollectionFactory; $this->searchResultsFactory = $searchResultsFactory; $this->collectionProcessor = $collectionProcessor; } /** * @inheritDoc */ public function save(AvatrInterface $avatr) { try { $this->resource->save($avatr); } catch (\Exception $exception) { throw new CouldNotSaveException(__( 'Could not save the avatr: %1', $exception->getMessage() )); } return $avatr; } /** * @inheritDoc */ public function get($avatrId) { $avatr = $this->avatrFactory->create(); $this->resource->load($avatr, $avatrId); if (!$avatr->getId()) { throw new NoSuchEntityException(__('Avatr with id "%1" does not exist.', $avatrId)); } return $avatr; } /** * @inheritDoc */ public function getList( \Magento\Framework\Api\SearchCriteriaInterface $criteria ) { $collection = $this->avatrCollectionFactory->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(AvatrInterface $avatr) { try { $avatrModel = $this->avatrFactory->create(); $this->resource->load($avatrModel, $avatr->getAvatrId()); $this->resource->delete($avatrModel); } catch (\Exception $exception) { throw new CouldNotDeleteException(__( 'Could not delete the Avatr: %1', $exception->getMessage() )); } return true; } /** * @inheritDoc */ public function deleteById($avatrId) { return $this->delete($this->get($avatrId)); } /** * @param string $telephone * @param string $model * @return AvatrInterface */ public function getByTelephone(string $telephone, string $model): AvatrInterface { $collection = $this->avatrCollectionFactory->create(); $collection->addFieldToFilter('telephone', $telephone) ->addFieldToFilter('favorite_vehicle', $model) ->setItemObjectClass(Avatr::class); return $collection->getFirstItem(); } }