Files
2026-09-15 10:26:34 +08:00

181 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Stemcell\Customer\Model;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Api\Data\LabInterface;
use Stemcell\Customer\Api\Data\LabInterfaceFactory;
use Stemcell\Customer\Api\Data\LabSearchResultsInterfaceFactory;
use Stemcell\Customer\Api\LabRepositoryInterface;
use Stemcell\Customer\Model\ResourceModel\Lab as ResourceLab;
use Stemcell\Customer\Model\ResourceModel\Lab\CollectionFactory as LabCollectionFactory;
class LabRepository implements LabRepositoryInterface
{
/**
* @var ResourceLab
*/
protected $resource;
/**
* @var LabInterfaceFactory
*/
protected $labFactory;
/**
* @var LabCollectionFactory
*/
protected $labCollectionFactory;
/**
* @var Lab
*/
protected $searchResultsFactory;
/**
* @var CollectionProcessorInterface
*/
protected $collectionProcessor;
protected \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder;
/**
* @param ResourceLab $resource
* @param LabInterfaceFactory $labFactory
* @param LabCollectionFactory $labCollectionFactory
* @param LabSearchResultsInterfaceFactory $searchResultsFactory
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
ResourceLab $resource,
LabInterfaceFactory $labFactory,
LabCollectionFactory $labCollectionFactory,
LabSearchResultsInterfaceFactory $searchResultsFactory,
CollectionProcessorInterface $collectionProcessor,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->resource = $resource;
$this->labFactory = $labFactory;
$this->labCollectionFactory = $labCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->collectionProcessor = $collectionProcessor;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* @inheritDoc
*/
public function save(LabInterface $lab)
{
try {
$this->resource->save($lab);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__(
'Could not save the lab: %1',
$exception->getMessage()
));
}
return $lab;
}
/**
* @inheritDoc
*/
public function get($labId)
{
$lab = $this->labFactory->create();
$this->resource->load($lab, $labId);
if (!$lab->getId()) {
throw new NoSuchEntityException(__('Lab with id "%1" does not exist.', $labId));
}
return $lab;
}
/**
* @inheritDoc
*/
public function getList(
\Magento\Framework\Api\SearchCriteriaInterface $criteria
) {
$collection = $this->labCollectionFactory->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(LabInterface $lab)
{
try {
$labModel = $this->labFactory->create();
$this->resource->load($labModel, $lab->getId());
$this->resource->delete($labModel);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(__(
'Could not delete the Lab: %1',
$exception->getMessage()
));
}
return true;
}
/**
* @inheritDoc
*/
public function deleteById($labId)
{
return $this->delete($this->get($labId));
}
/**
* @inheirtDoc
*/
public function getByDealerId(int $customerId): ResourceLab\Collection
{
$collection = $this->labCollectionFactory->create();
$collection->addFieldToFilter('main_table.customer_id', $customerId)
->getSelect()
->joinLeft('customer_entity', 'customer_entity.entity_id=main_table.customer_id', ['customer_name'=>'firstname'])
->joinLeft('salesperson', 'salesperson.salesperson_id=main_table.salesperson_id', ['salesperson_name'=>'salesperson.name'])
->query();
return $collection;
}
/**
* @inheritDoc
*/
public function getByLabCode($labCode)
{
$lab = $this->labFactory->create();
$this->resource->load($lab, $labCode, 'lab_code');
if (!$lab->getId()) {
throw new NoSuchEntityException(__('Lab with id "%1" does not exist.', $labCode));
}
return $lab;
}
}