160 lines
4.4 KiB
PHP
160 lines
4.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Stemcell\Customer\Model;
|
|
|
|
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
|
|
use Magento\Framework\Exception\CouldNotDeleteException;
|
|
use Magento\Framework\Exception\CouldNotSaveException;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Stemcell\Customer\Api\AvatrRepositoryInterface;
|
|
use Stemcell\Customer\Api\Data\AvatrInterface;
|
|
use Stemcell\Customer\Api\Data\AvatrInterfaceFactory;
|
|
use Stemcell\Customer\Api\Data\AvatrSearchResultsInterfaceFactory;
|
|
use Stemcell\Customer\Model\ResourceModel\Avatr as ResourceAvatr;
|
|
use Stemcell\Customer\Model\ResourceModel\Avatr\CollectionFactory as AvatrCollectionFactory;
|
|
|
|
class AvatrRepository implements AvatrRepositoryInterface
|
|
{
|
|
|
|
/**
|
|
* @var CollectionProcessorInterface
|
|
*/
|
|
protected $collectionProcessor;
|
|
|
|
/**
|
|
* @var AvatrInterfaceFactory
|
|
*/
|
|
protected $avatrFactory;
|
|
|
|
/**
|
|
* @var ResourceAvatr
|
|
*/
|
|
protected $resource;
|
|
|
|
/**
|
|
* @var AvatrCollectionFactory
|
|
*/
|
|
protected $avatrCollectionFactory;
|
|
|
|
/**
|
|
* @var Avatr
|
|
*/
|
|
protected $searchResultsFactory;
|
|
|
|
|
|
/**
|
|
* @param ResourceAvatr $resource
|
|
* @param AvatrInterfaceFactory $avatrFactory
|
|
* @param AvatrCollectionFactory $avatrCollectionFactory
|
|
* @param AvatrSearchResultsInterfaceFactory $searchResultsFactory
|
|
* @param CollectionProcessorInterface $collectionProcessor
|
|
*/
|
|
public function __construct(
|
|
ResourceAvatr $resource,
|
|
AvatrInterfaceFactory $avatrFactory,
|
|
AvatrCollectionFactory $avatrCollectionFactory,
|
|
AvatrSearchResultsInterfaceFactory $searchResultsFactory,
|
|
CollectionProcessorInterface $collectionProcessor
|
|
) {
|
|
$this->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();
|
|
}
|
|
}
|
|
|