/**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function addChild(\Magento\Catalog\Api\Data\ProductInterface $product, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct)
 {
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new InputException(__('Product with specified sku: "%1" is not a bundle product', $product->getSku()));
     }
     $options = $this->optionCollection->create();
     $options->setProductIdFilter($product->getId())->joinValues($this->storeManager->getStore()->getId());
     $isNewOption = true;
     /** @var \Magento\Bundle\Model\Option $option */
     foreach ($options as $option) {
         if ($option->getOptionId() == $optionId) {
             $isNewOption = false;
             break;
         }
     }
     if ($isNewOption) {
         throw new InputException(__('Product with specified sku: "%1" does not contain option: "%2"', [$product->getSku(), $optionId]));
     }
     /* @var $resource \Magento\Bundle\Model\Resource\Bundle */
     $resource = $this->bundleFactory->create();
     $selections = $resource->getSelectionsData($product->getId());
     /** @var \Magento\Catalog\Model\Product $linkProductModel */
     $linkProductModel = $this->productRepository->get($linkedProduct->getSku());
     if ($linkProductModel->isComposite()) {
         throw new InputException(__('Bundle product could not contain another composite product'));
     }
     if ($selections) {
         foreach ($selections as $selection) {
             if ($selection['option_id'] == $optionId && $selection['product_id'] == $linkProductModel->getId()) {
                 throw new CouldNotSaveException(__('Child with specified sku: "%1" already assigned to product: "%2"', [$linkedProduct->getSku(), $product->getSku()]));
             }
         }
     }
     $selectionModel = $this->bundleSelection->create();
     $selectionModel->setOptionId($optionId)->setPosition($linkedProduct->getPosition())->setSelectionQty($linkedProduct->getQty())->setSelectionPriceType($linkedProduct->getPriceType())->setSelectionPriceValue($linkedProduct->getPrice())->setSelectionCanChangeQty($linkedProduct->getCanChangeQuantity())->setProductId($linkProductModel->getId())->setParentProductId($product->getId())->setIsDefault($linkedProduct->getIsDefault())->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
     try {
         $selectionModel->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
     }
     return $selectionModel->getId();
 }
 /**
  * @param \Magento\Bundle\Model\Selection $selectionModel
  * @param \Magento\Bundle\Api\Data\LinkInterface $productLink
  * @param string $linkedProductId
  * @param string $parentProductId
  * @return \Magento\Bundle\Model\Selection
  */
 protected function mapProductLinkToSelectionModel(\Magento\Bundle\Model\Selection $selectionModel, \Magento\Bundle\Api\Data\LinkInterface $productLink, $linkedProductId, $parentProductId)
 {
     $selectionModel->setProductId($linkedProductId);
     $selectionModel->setParentProductId($parentProductId);
     if ($productLink->getOptionId() !== null) {
         $selectionModel->setOptionId($productLink->getOptionId());
     }
     if ($productLink->getPosition() !== null) {
         $selectionModel->setPosition($productLink->getPosition());
     }
     if ($productLink->getQty() !== null) {
         $selectionModel->setSelectionQty($productLink->getQty());
     }
     if ($productLink->getPriceType() !== null) {
         $selectionModel->setSelectionPriceType($productLink->getPriceType());
     }
     if ($productLink->getPrice() !== null) {
         $selectionModel->setSelectionPriceValue($productLink->getPrice());
     }
     if ($productLink->getCanChangeQuantity() !== null) {
         $selectionModel->setSelectionCanChangeQty($productLink->getCanChangeQuantity());
     }
     if ($productLink->getIsDefault() !== null) {
         $selectionModel->setIsDefault($productLink->getIsDefault());
     }
     return $selectionModel;
 }