|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Customer\Test\Fixture; |
| 9 | + |
| 10 | +use Magento\Customer\Model\Attribute; |
| 11 | +use Magento\Customer\Model\ResourceModel\Attribute as ResourceModelAttribute; |
| 12 | +use Magento\Eav\Api\AttributeRepositoryInterface; |
| 13 | +use Magento\Eav\Model\AttributeFactory; |
| 14 | +use Magento\Framework\DataObject; |
| 15 | +use Magento\Framework\Exception\InvalidArgumentException; |
| 16 | +use Magento\TestFramework\Fixture\Api\DataMerger; |
| 17 | +use Magento\TestFramework\Fixture\Data\ProcessorInterface; |
| 18 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 19 | + |
| 20 | +class CustomerAttribute implements RevertibleDataFixtureInterface |
| 21 | +{ |
| 22 | + private const DEFAULT_DATA = [ |
| 23 | + 'entity_type_id' => null, |
| 24 | + 'attribute_id' => null, |
| 25 | + 'attribute_code' => 'attribute%uniqid%', |
| 26 | + 'default_frontend_label' => 'Attribute%uniqid%', |
| 27 | + 'frontend_labels' => [], |
| 28 | + 'frontend_input' => 'text', |
| 29 | + 'backend_type' => 'varchar', |
| 30 | + 'is_required' => false, |
| 31 | + 'is_user_defined' => true, |
| 32 | + 'note' => null, |
| 33 | + 'backend_model' => null, |
| 34 | + 'source_model' => null, |
| 35 | + 'default_value' => null, |
| 36 | + 'is_unique' => '0', |
| 37 | + 'frontend_class' => null, |
| 38 | + 'used_in_forms' => [], |
| 39 | + ]; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var DataMerger |
| 43 | + */ |
| 44 | + private DataMerger $dataMerger; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var ProcessorInterface |
| 48 | + */ |
| 49 | + private ProcessorInterface $processor; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var AttributeFactory |
| 53 | + */ |
| 54 | + private AttributeFactory $attributeFactory; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var ResourceModelAttribute |
| 58 | + */ |
| 59 | + private ResourceModelAttribute $resourceModelAttribute; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var AttributeRepositoryInterface |
| 63 | + */ |
| 64 | + private AttributeRepositoryInterface $attributeRepository; |
| 65 | + |
| 66 | + /** |
| 67 | + * @param DataMerger $dataMerger |
| 68 | + * @param ProcessorInterface $processor |
| 69 | + * @param AttributeRepositoryInterface $attributeRepository |
| 70 | + * @param AttributeFactory $attributeFactory |
| 71 | + * @param ResourceModelAttribute $resourceModelAttribute |
| 72 | + */ |
| 73 | + public function __construct( |
| 74 | + DataMerger $dataMerger, |
| 75 | + ProcessorInterface $processor, |
| 76 | + AttributeRepositoryInterface $attributeRepository, |
| 77 | + AttributeFactory $attributeFactory, |
| 78 | + ResourceModelAttribute $resourceModelAttribute |
| 79 | + ) { |
| 80 | + $this->dataMerger = $dataMerger; |
| 81 | + $this->processor = $processor; |
| 82 | + $this->attributeFactory = $attributeFactory; |
| 83 | + $this->resourceModelAttribute = $resourceModelAttribute; |
| 84 | + $this->attributeRepository = $attributeRepository; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @inheritdoc |
| 89 | + */ |
| 90 | + public function apply(array $data = []): ?DataObject |
| 91 | + { |
| 92 | + if (empty($data['entity_type_id'])) { |
| 93 | + throw new InvalidArgumentException( |
| 94 | + __( |
| 95 | + '"%field" value is required to create an attribute', |
| 96 | + [ |
| 97 | + 'field' => 'entity_type_id' |
| 98 | + ] |
| 99 | + ) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** @var Attribute $attr */ |
| 104 | + $attr = $this->attributeFactory->createAttribute(Attribute::class, self::DEFAULT_DATA); |
| 105 | + $mergedData = $this->processor->process($this, $this->dataMerger->merge(self::DEFAULT_DATA, $data)); |
| 106 | + $attr->setData($mergedData); |
| 107 | + $this->resourceModelAttribute->save($attr); |
| 108 | + return $attr; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @inheritdoc |
| 113 | + */ |
| 114 | + public function revert(DataObject $data): void |
| 115 | + { |
| 116 | + $this->attributeRepository->deleteById($data['attribute_id']); |
| 117 | + } |
| 118 | +} |
0 commit comments