Files
ltsecurityinc/app/code/JR/NetSuite/Model/LocationRepository.php
T

152 lines
4.2 KiB
PHP
Executable File

<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace JR\NetSuite\Model;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use JR\NetSuite\Api\Data\LocationInterface;
use JR\NetSuite\Api\Data\LocationInterfaceFactory;
use JR\NetSuite\Api\Data\LocationSearchResultsInterfaceFactory;
use JR\NetSuite\Api\LocationRepositoryInterface;
use JR\NetSuite\Model\ResourceModel\Location as ResourceLocation;
use JR\NetSuite\Model\ResourceModel\Location\CollectionFactory as LocationCollectionFactory;
class LocationRepository implements LocationRepositoryInterface
{
/**
* @var LocationInterfaceFactory
*/
protected $locationFactory;
/**
* @var CollectionProcessorInterface
*/
protected $collectionProcessor;
/**
* @var Location
*/
protected $searchResultsFactory;
/**
* @var ResourceLocation
*/
protected $resource;
/**
* @var LocationCollectionFactory
*/
protected $locationCollectionFactory;
/**
* @param ResourceLocation $resource
* @param LocationInterfaceFactory $locationFactory
* @param LocationCollectionFactory $locationCollectionFactory
* @param LocationSearchResultsInterfaceFactory $searchResultsFactory
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
ResourceLocation $resource,
LocationInterfaceFactory $locationFactory,
LocationCollectionFactory $locationCollectionFactory,
LocationSearchResultsInterfaceFactory $searchResultsFactory,
CollectionProcessorInterface $collectionProcessor
) {
$this->resource = $resource;
$this->locationFactory = $locationFactory;
$this->locationCollectionFactory = $locationCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
$this->collectionProcessor = $collectionProcessor;
}
/**
* @inheritDoc
*/
public function save(LocationInterface $location)
{
try {
$this->resource->save($location);
} catch (\Exception $exception) {
throw new CouldNotSaveException(
__(
'Could not save the location: %1',
$exception->getMessage()
)
);
}
return $location;
}
/**
* @inheritDoc
*/
public function get($locationId)
{
$location = $this->locationFactory->create();
$this->resource->load($location, $locationId);
if (!$location->getId()) {
throw new NoSuchEntityException(__('location with id "%1" does not exist.', $locationId));
}
return $location;
}
/**
* @inheritDoc
*/
public function getList(
\Magento\Framework\Api\SearchCriteriaInterface $criteria
) {
$collection = $this->locationCollectionFactory->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(LocationInterface $location)
{
try {
$locationModel = $this->locationFactory->create();
$this->resource->load($locationModel, $location->getLocationId());
$this->resource->delete($locationModel);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(
__(
'Could not delete the location: %1',
$exception->getMessage()
)
);
}
return true;
}
/**
* @inheritDoc
*/
public function deleteById($locationId)
{
return $this->delete($this->get($locationId));
}
}