150 lines
4.2 KiB
PHP
150 lines
4.2 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\SpeciesInterface;
|
|
use Stemcell\Catalog\Api\Data\SpeciesInterfaceFactory;
|
|
use Stemcell\Catalog\Api\Data\SpeciesSearchResultsInterfaceFactory;
|
|
use Stemcell\Catalog\Api\SpeciesRepositoryInterface;
|
|
use Stemcell\Catalog\Model\ResourceModel\Species as ResourceSpecies;
|
|
use Stemcell\Catalog\Model\ResourceModel\Species\CollectionFactory as SpeciesCollectionFactory;
|
|
|
|
class SpeciesRepository implements SpeciesRepositoryInterface
|
|
{
|
|
|
|
/**
|
|
* @var SpeciesInterfaceFactory
|
|
*/
|
|
protected $speciesFactory;
|
|
|
|
/**
|
|
* @var CollectionProcessorInterface
|
|
*/
|
|
protected $collectionProcessor;
|
|
|
|
/**
|
|
* @var ResourceSpecies
|
|
*/
|
|
protected $resource;
|
|
|
|
/**
|
|
* @var SpeciesCollectionFactory
|
|
*/
|
|
protected $speciesCollectionFactory;
|
|
|
|
/**
|
|
* @var Species
|
|
*/
|
|
protected $searchResultsFactory;
|
|
|
|
|
|
/**
|
|
* @param ResourceSpecies $resource
|
|
* @param SpeciesInterfaceFactory $speciesFactory
|
|
* @param SpeciesCollectionFactory $speciesCollectionFactory
|
|
* @param SpeciesSearchResultsInterfaceFactory $searchResultsFactory
|
|
* @param CollectionProcessorInterface $collectionProcessor
|
|
*/
|
|
public function __construct(
|
|
ResourceSpecies $resource,
|
|
SpeciesInterfaceFactory $speciesFactory,
|
|
SpeciesCollectionFactory $speciesCollectionFactory,
|
|
SpeciesSearchResultsInterfaceFactory $searchResultsFactory,
|
|
CollectionProcessorInterface $collectionProcessor
|
|
) {
|
|
$this->resource = $resource;
|
|
$this->speciesFactory = $speciesFactory;
|
|
$this->speciesCollectionFactory = $speciesCollectionFactory;
|
|
$this->searchResultsFactory = $searchResultsFactory;
|
|
$this->collectionProcessor = $collectionProcessor;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function save(SpeciesInterface $species)
|
|
{
|
|
try {
|
|
$this->resource->save($species);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotSaveException(__(
|
|
'Could not save the species: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return $species;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function get($speciesId)
|
|
{
|
|
$species = $this->speciesFactory->create();
|
|
$this->resource->load($species, $speciesId);
|
|
if (!$species->getId()) {
|
|
throw new NoSuchEntityException(__('Species with id "%1" does not exist.', $speciesId));
|
|
}
|
|
return $species;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getList(
|
|
\Magento\Framework\Api\SearchCriteriaInterface $criteria
|
|
) {
|
|
$collection = $this->speciesCollectionFactory->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(SpeciesInterface $species)
|
|
{
|
|
try {
|
|
$speciesModel = $this->speciesFactory->create();
|
|
$this->resource->load($speciesModel, $species->getSpeciesId());
|
|
$this->resource->delete($speciesModel);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotDeleteException(__(
|
|
'Could not delete the Species: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function deleteById($speciesId)
|
|
{
|
|
return $this->delete($this->get($speciesId));
|
|
}
|
|
}
|
|
|