/**
  * Create a matching attribute option
  *
  * @param string $attributeCode Attribute the option should exist in
  * @param string $label Label to add
  * @return \Magento\Eav\Api\Data\AttributeOptionInterface
  */
 public function createAttributeOption($attributeCode, $label)
 {
     $option = $this->findAttributeOptionByLabel($attributeCode, $label);
     if (!$option) {
         $option = $this->optionDataFactory->create();
         $option->setLabel($label);
         $this->attributeOptionManagementInterface->add($attributeCode, $option);
         $option = $this->findAttributeOptionByLabel($attributeCode, $label);
     }
     return $option;
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
 {
     if ($attribute->getAttributeId()) {
         $existingModel = $this->get($attribute->getAttributeCode());
         if (!$existingModel->getAttributeId()) {
             throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
         }
         $attribute->setAttributeId($existingModel->getAttributeId());
         $attribute->setIsUserDefined($existingModel->getIsUserDefined());
         $attribute->setFrontendInput($existingModel->getFrontendInput());
         if (is_array($attribute->getFrontendLabels())) {
             $frontendLabel[0] = $existingModel->getDefaultFrontendLabel();
             foreach ($attribute->getFrontendLabels() as $item) {
                 $frontendLabel[$item->getStoreId()] = $item->getLabel();
             }
             $attribute->setDefaultFrontendLabel($frontendLabel);
         }
         if (!$attribute->getIsUserDefined()) {
             // Unset attribute field for system attributes
             $attribute->setApplyTo(null);
         }
     } else {
         $attribute->setAttributeId(null);
         if (!$attribute->getFrontendLabels() && !$attribute->getDefaultFrontendLabel()) {
             throw InputException::requiredField('frontend_label');
         }
         $frontendLabels = [];
         if ($attribute->getDefaultFrontendLabel()) {
             $frontendLabels[0] = $attribute->getDefaultFrontendLabel();
         }
         if ($attribute->getFrontendLabels() && is_array($attribute->getFrontendLabels())) {
             foreach ($attribute->getFrontendLabels() as $label) {
                 $frontendLabels[$label->getStoreId()] = $label->getLabel();
             }
             if (!isset($frontendLabels[0]) || !$frontendLabels[0]) {
                 throw InputException::invalidFieldValue('frontend_label', null);
             }
             $attribute->setDefaultFrontendLabel($frontendLabels);
         }
         $attribute->setAttributeCode($attribute->getAttributeCode() ?: $this->generateCode($frontendLabels[0]));
         $this->validateCode($attribute->getAttributeCode());
         $this->validateFrontendInput($attribute->getFrontendInput());
         $attribute->setBackendType($attribute->getBackendTypeByInput($attribute->getFrontendInput()));
         $attribute->setSourceModel($this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput()));
         $attribute->setBackendModel($this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput()));
         $attribute->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)->getId());
         $attribute->setIsUserDefined(1);
     }
     $this->attributeResource->save($attribute);
     foreach ($attribute->getOptions() as $option) {
         $this->optionManagement->add($attribute->getAttributeCode(), $option);
     }
     return $this->get($attribute->getAttributeCode());
 }