/** * Verifies that the tax class model exists and is a customer tax class type. * * @param int $taxClassId The id of the tax class model to check * @param CustomerGroup $group The original group parameters * @return void * @throws InputException Thrown if the tax class model is invalid */ protected function _verifyTaxClassModel($taxClassId, $group) { try { /* @var TaxClass $taxClassData */ $taxClassData = $this->_taxClassService->getTaxClass($taxClassId); } catch (NoSuchEntityException $e) { throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId()); } if ($taxClassData->getClassType() !== TaxClassServiceInterface::TYPE_CUSTOMER) { throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId()); } }
/** * {@inheritdoc} */ public function saveGroup(Data\CustomerGroup $group) { if (!$group->getCode()) { throw InputException::invalidFieldValue('code', $group->getCode()); } $customerGroup = null; if ($group->getId()) { try { $customerGroup = $this->_groupRegistry->retrieve($group->getId()); } catch (NoSuchEntityException $e) { throw NoSuchEntityException::singleField('id', $group->getId()); } } if (!$customerGroup) { $customerGroup = $this->_groupFactory->create(); } $customerGroup->setCode($group->getCode()); $taxClassId = $group->getTaxClassId(); if (!$taxClassId) { $taxClassId = self::DEFAULT_TAX_CLASS_ID; } $this->_verifyTaxClassModel($taxClassId, $group); $customerGroup->setTaxClassId($taxClassId); try { $customerGroup->save(); } catch (\Magento\Framework\Model\Exception $e) { /** * Would like a better way to determine this error condition but * difficult to do without imposing more database calls */ if ($e->getMessage() === __('Customer Group already exists.')) { throw new InvalidTransitionException('Customer Group already exists.'); } throw $e; } return $customerGroup->getId(); }