Files

73 lines
2.7 KiB
PHP

<?php
namespace Lts\Customer\Model;
use JR\NetSuite\Logger\Logger;
use Lts\Customer\Api\CustomerRepositoryInterface;
use Lts\Customer\Helper\NetsuiteStage;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ImageProcessorInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
class CustomerRepository implements CustomerRepositoryInterface
{
private \Magento\Customer\Api\CustomerRepositoryInterface $coreCustomerRepository;
private Logger $logger;
private DataObjectHelper $dataObjectHelper;
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $coreCustomerRepository,
DataObjectHelper $dataObjectHelper,
Logger $logger,
) {
$this->coreCustomerRepository = $coreCustomerRepository;
$this->logger = $logger;
$this->dataObjectHelper = $dataObjectHelper;
}
/**
* @param CustomerInterface $customer
* @return CustomerInterface
* @throws InputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\State\InputMismatchException
*/
public function save(CustomerInterface $customer)
{
if (!$customer->getId() && !$customer->getEmail()) {
throw new InputException(__('Customer id or email is required.'));
}
$netsuiteStatusId = $customer->getExtensionAttributes()?->getNetsuiteStatus();
if ($netsuiteStatusId && is_numeric($netsuiteStatusId)) {
$customer->getExtensionAttributes()->setNetsuiteStatus(NetsuiteStage::getNetsuiteStageCode($netsuiteStatusId));
}
if ($customer->getEmail() && !$customer->getId()) {
$savedCustomer = $this->coreCustomerRepository->get($customer->getEmail());
if ($savedCustomer->getId()) {
$this->dataObjectHelper->populateWithArray(
$customer,
$savedCustomer->__toArray(),
CustomerInterface::class
);
$customer->setId($savedCustomer->getId());
}
}
if ($customer->getId()) {
return $this->coreCustomerRepository->save($customer);
}
$with = '';
if ($customer->getEmail()) {
$with = sprintf('with email "%s"', $customer->getEmail());
} elseif ($customer->getId()) {
$with = sprintf('with id "%s"', $customer->getEmail());
}
throw new NoSuchEntityException(__('Customer "%1" does not exist.', $with));
}
}