152 lines
4.8 KiB
PHP
152 lines
4.8 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\ExperimentalSchemeInterface;
|
|
use Stemcell\Catalog\Api\Data\ExperimentalSchemeInterfaceFactory;
|
|
use Stemcell\Catalog\Api\Data\ExperimentalSchemeSearchResultsInterfaceFactory;
|
|
use Stemcell\Catalog\Api\ExperimentalSchemeRepositoryInterface;
|
|
use Stemcell\Catalog\Model\ResourceModel\ExperimentalScheme as ResourceExperimentalScheme;
|
|
use Stemcell\Catalog\Model\ResourceModel\ExperimentalScheme\CollectionFactory as ExperimentalSchemeCollectionFactory;
|
|
|
|
class ExperimentalSchemeRepository implements ExperimentalSchemeRepositoryInterface
|
|
{
|
|
|
|
/**
|
|
* @var ResourceExperimentalScheme
|
|
*/
|
|
protected $resource;
|
|
|
|
/**
|
|
* @var ExperimentalSchemeInterfaceFactory
|
|
*/
|
|
protected $experimentalSchemeFactory;
|
|
|
|
/**
|
|
* @var ExperimentalSchemeCollectionFactory
|
|
*/
|
|
protected $experimentalSchemeCollectionFactory;
|
|
|
|
/**
|
|
* @var CollectionProcessorInterface
|
|
*/
|
|
protected $collectionProcessor;
|
|
|
|
/**
|
|
* @var ExperimentalScheme
|
|
*/
|
|
protected $searchResultsFactory;
|
|
|
|
|
|
/**
|
|
* @param ResourceExperimentalScheme $resource
|
|
* @param ExperimentalSchemeInterfaceFactory $experimentalSchemeFactory
|
|
* @param ExperimentalSchemeCollectionFactory $experimentalSchemeCollectionFactory
|
|
* @param ExperimentalSchemeSearchResultsInterfaceFactory $searchResultsFactory
|
|
* @param CollectionProcessorInterface $collectionProcessor
|
|
*/
|
|
public function __construct(
|
|
ResourceExperimentalScheme $resource,
|
|
ExperimentalSchemeInterfaceFactory $experimentalSchemeFactory,
|
|
ExperimentalSchemeCollectionFactory $experimentalSchemeCollectionFactory,
|
|
ExperimentalSchemeSearchResultsInterfaceFactory $searchResultsFactory,
|
|
CollectionProcessorInterface $collectionProcessor
|
|
) {
|
|
$this->resource = $resource;
|
|
$this->experimentalSchemeFactory = $experimentalSchemeFactory;
|
|
$this->experimentalSchemeCollectionFactory = $experimentalSchemeCollectionFactory;
|
|
$this->searchResultsFactory = $searchResultsFactory;
|
|
$this->collectionProcessor = $collectionProcessor;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function save(
|
|
ExperimentalSchemeInterface $experimentalScheme
|
|
) {
|
|
try {
|
|
$this->resource->save($experimentalScheme);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotSaveException(__(
|
|
'Could not save the experimentalScheme: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return $experimentalScheme;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function get($experimentalSchemeId)
|
|
{
|
|
$experimentalScheme = $this->experimentalSchemeFactory->create();
|
|
$this->resource->load($experimentalScheme, $experimentalSchemeId);
|
|
if (!$experimentalScheme->getId()) {
|
|
throw new NoSuchEntityException(__('ExperimentalScheme with id "%1" does not exist.', $experimentalSchemeId));
|
|
}
|
|
return $experimentalScheme;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getList(
|
|
\Magento\Framework\Api\SearchCriteriaInterface $criteria
|
|
) {
|
|
$collection = $this->experimentalSchemeCollectionFactory->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(
|
|
ExperimentalSchemeInterface $experimentalScheme
|
|
) {
|
|
try {
|
|
$experimentalSchemeModel = $this->experimentalSchemeFactory->create();
|
|
$this->resource->load($experimentalSchemeModel, $experimentalScheme->getExperimentalschemeId());
|
|
$this->resource->delete($experimentalSchemeModel);
|
|
} catch (\Exception $exception) {
|
|
throw new CouldNotDeleteException(__(
|
|
'Could not delete the ExperimentalScheme: %1',
|
|
$exception->getMessage()
|
|
));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function deleteById($experimentalSchemeId)
|
|
{
|
|
return $this->delete($this->get($experimentalSchemeId));
|
|
}
|
|
}
|
|
|