150 lines
4.1 KiB
PHP
150 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace Stemcell\Catalog\Model;
|
|
|
|
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
|
|
use Magento\Framework\Exception\CouldNotDeleteException;
|
|
use Magento\Framework\Exception\CouldNotSaveException;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Stemcell\Catalog\Api\Data\TissueInterface;
|
|
use Stemcell\Catalog\Api\Data\TissueInterfaceFactory;
|
|
use Stemcell\Catalog\Api\Data\TissueSearchResultsInterfaceFactory;
|
|
use Stemcell\Catalog\Api\TissueRepositoryInterface;
|
|
use Stemcell\Catalog\Model\ResourceModel\Tissue as ResourceTissue;
|
|
use Stemcell\Catalog\Model\ResourceModel\Tissue\CollectionFactory as TissueCollectionFactory;
|
|
|
|
class TissueRepository implements TissueRepositoryInterface
|
|
{
|
|
|
|
/**
|
|
* @var TissueInterfaceFactory
|
|
*/
|
|
protected $tissueFactory;
|
|
|
|
/**
|
|
* @var Tissue
|
|
*/
|
|
protected $searchResultsFactory;
|
|
|
|
/**
|
|
* @var CollectionProcessorInterface
|
|
*/
|
|
protected $collectionProcessor;
|
|
|
|
/**
|
|
* @var ResourceTissue
|
|
*/
|
|
protected $resource;
|
|
|
|
/**
|
|
* @var TissueCollectionFactory
|
|
*/
|
|
protected $tissueCollectionFactory;
|
|
|
|
|
|
/**
|
|
* @param ResourceTissue $resource
|
|
* @param TissueInterfaceFactory $tissueFactory
|
|
* @param TissueCollectionFactory $tissueCollectionFactory
|
|
* @param TissueSearchResultsInterfaceFactory $searchResultsFactory
|
|
* @param CollectionProcessorInterface $collectionProcessor
|
|
*/
|
|
public function __construct(
|
|
ResourceTissue $resource,
|
|
TissueInterfaceFactory $tissueFactory,
|
|
TissueCollectionFactory $tissueCollectionFactory,
|
|
TissueSearchResultsInterfaceFactory $searchResultsFactory,
|
|
CollectionProcessorInterface $collectionProcessor
|
|
) {
|
|
$this->resource = $resource;
|
|
$this->tissueFactory = $tissueFactory;
|
|
$this->tissueCollectionFactory = $tissueCollectionFactory;
|
|
$this->searchResultsFactory = $searchResultsFactory;
|
|
$this->collectionProcessor = $collectionProcessor;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function save(TissueInterface $tissue)
|
|
{
|
|
try {
|
|
$this->resource->save($tissue);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotSaveException(__(
|
|
'Could not save the tissue: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return $tissue;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function get($tissueId)
|
|
{
|
|
$tissue = $this->tissueFactory->create();
|
|
$this->resource->load($tissue, $tissueId);
|
|
if (!$tissue->getId()) {
|
|
throw new NoSuchEntityException(__('Tissue with id "%1" does not exist.', $tissueId));
|
|
}
|
|
return $tissue;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getList(
|
|
\Magento\Framework\Api\SearchCriteriaInterface $criteria
|
|
) {
|
|
$collection = $this->tissueCollectionFactory->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(TissueInterface $tissue)
|
|
{
|
|
try {
|
|
$tissueModel = $this->tissueFactory->create();
|
|
$this->resource->load($tissueModel, $tissue->getTissueId());
|
|
$this->resource->delete($tissueModel);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotDeleteException(__(
|
|
'Could not delete the Tissue: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function deleteById($tissueId)
|
|
{
|
|
return $this->delete($this->get($tissueId));
|
|
}
|
|
}
|
|
|