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\SubItemProduct\Model;
|
|
|
|
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
|
|
use Magento\Framework\Exception\CouldNotDeleteException;
|
|
use Magento\Framework\Exception\CouldNotSaveException;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Stemcell\SubItemProduct\Api\Data\SubItemInterface;
|
|
use Stemcell\SubItemProduct\Api\Data\SubItemInterfaceFactory;
|
|
use Stemcell\SubItemProduct\Api\Data\SubItemSearchResultsInterfaceFactory;
|
|
use Stemcell\SubItemProduct\Api\SubItemRepositoryInterface;
|
|
use Stemcell\SubItemProduct\Model\ResourceModel\SubItem as ResourceSubItem;
|
|
use Stemcell\SubItemProduct\Model\ResourceModel\SubItem\CollectionFactory as SubItemCollectionFactory;
|
|
|
|
class SubItemRepository implements SubItemRepositoryInterface
|
|
{
|
|
|
|
/**
|
|
* @var SubItemInterfaceFactory
|
|
*/
|
|
protected $subItemFactory;
|
|
|
|
/**
|
|
* @var CollectionProcessorInterface
|
|
*/
|
|
protected $collectionProcessor;
|
|
|
|
/**
|
|
* @var SubItemCollectionFactory
|
|
*/
|
|
protected $subItemCollectionFactory;
|
|
|
|
/**
|
|
* @var ResourceSubItem
|
|
*/
|
|
protected $resource;
|
|
|
|
/**
|
|
* @var SubItem
|
|
*/
|
|
protected $searchResultsFactory;
|
|
|
|
|
|
/**
|
|
* @param ResourceSubItem $resource
|
|
* @param SubItemInterfaceFactory $subItemFactory
|
|
* @param SubItemCollectionFactory $subItemCollectionFactory
|
|
* @param SubItemSearchResultsInterfaceFactory $searchResultsFactory
|
|
* @param CollectionProcessorInterface $collectionProcessor
|
|
*/
|
|
public function __construct(
|
|
ResourceSubItem $resource,
|
|
SubItemInterfaceFactory $subItemFactory,
|
|
SubItemCollectionFactory $subItemCollectionFactory,
|
|
SubItemSearchResultsInterfaceFactory $searchResultsFactory,
|
|
CollectionProcessorInterface $collectionProcessor
|
|
) {
|
|
$this->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));
|
|
}
|
|
}
|
|
|