AttributeSetManagement.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. use Magento\Eav\Api\AttributeSetManagementInterface;
  8. use Magento\Eav\Api\AttributeSetRepositoryInterface;
  9. use Magento\Eav\Api\Data\AttributeSetInterface;
  10. use Magento\Eav\Model\Config as EavConfig;
  11. use Magento\Framework\Exception\InputException;
  12. class AttributeSetManagement implements AttributeSetManagementInterface
  13. {
  14. /**
  15. * @var EavConfig
  16. */
  17. private $eavConfig;
  18. /**
  19. * @var AttributeSetRepositoryInterface
  20. */
  21. private $repository;
  22. /**
  23. * @param Config $eavConfig
  24. * @param AttributeSetRepositoryInterface $repository
  25. * @codeCoverageIgnore
  26. */
  27. public function __construct(
  28. EavConfig $eavConfig,
  29. AttributeSetRepositoryInterface $repository
  30. ) {
  31. $this->eavConfig = $eavConfig;
  32. $this->repository = $repository;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function create($entityTypeCode, AttributeSetInterface $attributeSet, $skeletonId)
  38. {
  39. /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
  40. if ($attributeSet->getId() !== null) {
  41. throw InputException::invalidFieldValue('id', $attributeSet->getId());
  42. }
  43. if ($skeletonId == 0) {
  44. throw InputException::invalidFieldValue('skeletonId', $skeletonId);
  45. }
  46. // Make sure that skeleton attribute set is valid (try to load it)
  47. $this->repository->get($skeletonId);
  48. try {
  49. $attributeSet->setEntityTypeId($this->eavConfig->getEntityType($entityTypeCode)->getId());
  50. $attributeSet->validate();
  51. } catch (\Exception $exception) {
  52. throw new InputException(__($exception->getMessage()));
  53. }
  54. $this->repository->save($attributeSet);
  55. $attributeSet->initFromSkeleton($skeletonId);
  56. return $this->repository->save($attributeSet);
  57. }
  58. }