Files
ltsecurityinc/app/code/Lts/Inventory/Model/SourceCustomerRepository.php
T

191 lines
6.7 KiB
PHP

<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Lts\Inventory\Model;
use Lts\Inventory\Api\Data\SourceCustomerInterface;
use Lts\Inventory\Api\Data\SourceCustomerInterfaceFactory;
use Lts\Inventory\Api\Data\SourceCustomerSearchResultsInterfaceFactory;
use Lts\Inventory\Api\SourceCustomerRepositoryInterface;
use Lts\Inventory\Model\ResourceModel\SourceCustomer as ResourceInventorySourceCustomer;
use Lts\Inventory\Model\ResourceModel\SourceCustomer\CollectionFactory as SourceCustomerCollectionFactory;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
class SourceCustomerRepository implements SourceCustomerRepositoryInterface
{
/**
* @var SourceCustomerInterfaceFactory
*/
protected $sourceCustomerFactory;
/**
* @var CollectionProcessorInterface
*/
protected $collectionProcessor;
/**
* @var SourceCustomerCollectionFactory
*/
protected $sourceCustomerCollectionFactory;
/**
* @var ResourceInventorySourceCustomer
*/
protected $resource;
/**
* @var SourceCustomer
*/
protected $searchResultsFactory;
private \Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory;
private SourceRepositoryInterface $sourceRepository;
/**
* @param ResourceInventorySourceCustomer $resource
* @param SourceCustomerInterfaceFactory $sourceCustomerFactory
* @param sourceCustomerCollectionFactory $sourceCustomerCollectionFactory
* @param SourceCustomerSearchResultsInterfaceFactory $searchResultsFactory
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
ResourceInventorySourceCustomer $resource,
SourceCustomerInterfaceFactory $sourceCustomerFactory,
SourceCustomerCollectionFactory $sourceCustomerCollectionFactory,
SourceCustomerSearchResultsInterfaceFactory $searchResultsFactory,
CollectionProcessorInterface $collectionProcessor,
\Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
SourceRepositoryInterface $sourceRepository,
) {
$this->resource = $resource;
$this->sourceCustomerFactory = $sourceCustomerFactory;
$this->sourceCustomerCollectionFactory = $sourceCustomerCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->collectionProcessor = $collectionProcessor;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->sourceRepository = $sourceRepository;
}
/**
* @inheritDoc
*/
public function save(
SourceCustomerInterface $sourceCustomer
) {
try {
$this->sourceRepository->get($sourceCustomer->getSourceCode());
if (!$sourceCustomer->getEntityId() && $sourceCustomer->getCustomerId()) {
$collection = $this->sourceCustomerCollectionFactory->create()
->setItemObjectClass(SourceCustomer::class)
->addFilter(SourceCustomerInterface::CUSTOMER_ID, $sourceCustomer->getCustomerId());
$temp = $collection->getFirstItem();
if ($temp->getEntityId() && !$sourceCustomer->getEntityId()) {
$sourceCustomer->setEntityId($temp->getEntityId());
}
}
$this->resource->save($sourceCustomer);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__(
'Could not save the inventorySourceCustomer: %1',
$exception->getMessage()
));
}
return $sourceCustomer;
}
public function getFromCustomerId(int $customerId): ?SourceCustomerInterface
{
$inventorySourceCustomer = $this->sourceCustomerFactory->create();
$this->resource->load($inventorySourceCustomer, $customerId, 'customer_id');
if (!$inventorySourceCustomer->getId()) {
throw new NoSuchEntityException(__('SourceCustomer with customer id "%1" does not exist.', $customerId));
}
return $inventorySourceCustomer;
}
/**
* @inheritDoc
*/
public function get($id)
{
$inventorySourceCustomer = $this->sourceCustomerFactory->create();
$this->resource->load($inventorySourceCustomer, $id);
if (!$inventorySourceCustomer->getId()) {
throw new NoSuchEntityException(__('SourceCustomer with id "%1" does not exist.', $id));
}
return $inventorySourceCustomer;
}
/**
* @inheritDoc
*/
public function getList(
string $customerId
) {
$criteria = $this->searchCriteriaBuilderFactory->create()
->addFilter(SourceInterface::ENABLED, true)
->addFilter('is_pickup_location_active', 1)
->create();
$searchResults = $this->sourceRepository->getList($criteria);
$items = [];
$index = 0;
/* @var $model SourceInterface */
$sourceCustomer = $this->sourceCustomerFactory->create();
$this->resource->load($sourceCustomer, $customerId, 'customer_id');
foreach ($searchResults->getItems() as $model) {
if ($sourceCustomer->getSourceCode() == $model->getSourceCode()) {
$items[0] = $sourceCustomer->setSource($model);
} else {
$index += 1;
$items[$index] = $this->sourceCustomerFactory->create()->setSource($model);
}
}
ksort($items);
$searchResults->setItems(array_values($items));
$searchResults->setTotalCount($searchResults->getTotalCount());
return $searchResults;
}
/**
* @inheritDoc
*/
public function delete(
SourceCustomerInterface $inventorySourceCustomer
) {
try {
$inventorySourceCustomerModel = $this->sourceCustomerFactory->create();
$this->resource->load($inventorySourceCustomerModel, $inventorySourceCustomer->getInventorysourcecustomerId());
$this->resource->delete($inventorySourceCustomerModel);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(__(
'Could not delete the SourceCustomer: %1',
$exception->getMessage()
));
}
return true;
}
/**
* @inheritDoc
*/
public function deleteById(int $entityId)
{
return $this->delete($this->get($entityId));
}
}