OptionManagement.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Bundle\Model;
  8. use Magento\Framework\Exception\InputException;
  9. class OptionManagement implements \Magento\Bundle\Api\ProductOptionManagementInterface
  10. {
  11. /**
  12. * @var \Magento\Bundle\Api\ProductOptionRepositoryInterface
  13. */
  14. protected $optionRepository;
  15. /**
  16. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  17. */
  18. protected $productRepository;
  19. /**
  20. * @param \Magento\Bundle\Api\ProductOptionRepositoryInterface $optionRepository
  21. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  22. */
  23. public function __construct(
  24. \Magento\Bundle\Api\ProductOptionRepositoryInterface $optionRepository,
  25. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  26. ) {
  27. $this->optionRepository = $optionRepository;
  28. $this->productRepository = $productRepository;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function save(\Magento\Bundle\Api\Data\OptionInterface $option)
  34. {
  35. $product = $this->productRepository->get($option->getSku(), true);
  36. if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
  37. throw new InputException(__('This is implemented for bundle products only.'));
  38. }
  39. return $this->optionRepository->save($product, $option);
  40. }
  41. }