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)); } }