techdivision /
import-attribute-set
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * TechDivision\Import\Attribute\Set\Observers\AttributeGroupObserver |
||
| 5 | * |
||
| 6 | * PHP version 7 |
||
| 7 | * |
||
| 8 | * @author Tim Wagner <[email protected]> |
||
| 9 | * @copyright 2019 TechDivision GmbH <[email protected]> |
||
| 10 | * @license https://opensource.org/licenses/MIT |
||
| 11 | * @link https://github.com/techdivision/import-attribute-set |
||
| 12 | * @link http://www.techdivision.com |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace TechDivision\Import\Attribute\Set\Observers; |
||
| 16 | |||
| 17 | use TechDivision\Import\Utils\BackendTypeKeys; |
||
| 18 | use TechDivision\Import\Observers\AttributeLoaderInterface; |
||
| 19 | use TechDivision\Import\Observers\DynamicAttributeObserverInterface; |
||
| 20 | use TechDivision\Import\Attribute\Set\Utils\ColumnKeys; |
||
| 21 | use TechDivision\Import\Attribute\Set\Utils\MemberNames; |
||
| 22 | use TechDivision\Import\Attribute\Set\Utils\EntityTypeCodes; |
||
| 23 | use TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface; |
||
| 24 | use TechDivision\Import\Observers\EntityMergers\EntityMergerInterface; |
||
| 25 | use TechDivision\Import\Observers\StateDetectorInterface; |
||
| 26 | use TechDivision\Import\Dbal\Utils\EntityStatus; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Observer that create's the EAV attribute group itself. |
||
| 30 | * |
||
| 31 | * @author Tim Wagner <[email protected]> |
||
| 32 | * @copyright 2019 TechDivision GmbH <[email protected]> |
||
| 33 | * @license https://opensource.org/licenses/MIT |
||
| 34 | * @link https://github.com/techdivision/import-attribute-set |
||
| 35 | * @link http://www.techdivision.com |
||
| 36 | */ |
||
| 37 | class AttributeGroupObserver extends AbstractAttributeSetObserver implements DynamicAttributeObserverInterface |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The attribute loader instance. |
||
| 42 | * |
||
| 43 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
| 44 | */ |
||
| 45 | protected $attributeLoader; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The entity merger instance. |
||
| 49 | * |
||
| 50 | * @var \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface |
||
| 51 | */ |
||
| 52 | protected $entityMerger; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initialize the dedicated column. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $columns = array(MemberNames::SORT_ORDER => array(ColumnKeys::ATTRIBUTE_GROUP_SORT_ORDER, BackendTypeKeys::BACKEND_TYPE_INT)); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initializes the observer with the passed subject instance. |
||
| 63 | * |
||
| 64 | * @param \TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface $attributeSetBunchProcessor The attribute set bunch processor instance |
||
| 65 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface|null $attributeLoader The attribute loader instance |
||
| 66 | * @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface|null $entityMerger The entity merger instance |
||
| 67 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
||
| 68 | */ |
||
| 69 | public function __construct( |
||
| 70 | AttributeSetBunchProcessorInterface $attributeSetBunchProcessor, |
||
| 71 | AttributeLoaderInterface $attributeLoader = null, |
||
| 72 | EntityMergerInterface $entityMerger = null, |
||
| 73 | StateDetectorInterface $stateDetector = null |
||
| 74 | ) { |
||
| 75 | |||
| 76 | // set the attribute loader |
||
| 77 | $this->attributeLoader = $attributeLoader; |
||
| 78 | $this->entityMerger = $entityMerger; |
||
| 79 | |||
| 80 | // pass the processor to th eparend constructor |
||
| 81 | parent::__construct($attributeSetBunchProcessor, $stateDetector); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Process the observer's business logic. |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | protected function process() |
||
| 90 | { |
||
| 91 | |||
| 92 | // query whether or not at least attribute group name & code has been set |
||
| 93 | if ($this->hasValue(ColumnKeys::ATTRIBUTE_GROUP_NAME) && |
||
| 94 | $this->hasValue(ColumnKeys::ATTRIBUTE_GROUP_CODE) |
||
| 95 | ) { |
||
| 96 | // prepare the attribue set values |
||
| 97 | $attributeSetGroup = $this->initializeAttribute($this->prepareAttributes()); |
||
| 98 | // persist the entity |
||
| 99 | $this->persistAttributeGroup($attributeSetGroup); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Merge's and return's the entity with the passed attributes and set's the |
||
| 105 | * passed status. |
||
| 106 | * |
||
| 107 | * @param array $entity The entity to merge the attributes into |
||
| 108 | * @param array $attr The attributes to be merged |
||
| 109 | * @param string|null $changeSetName The change set name to use |
||
| 110 | * |
||
| 111 | * @return array The merged entity |
||
| 112 | * @todo https://github.com/techdivision/import/issues/179 |
||
| 113 | */ |
||
| 114 | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
||
| 115 | { |
||
| 116 | return array_merge( |
||
| 117 | $entity, |
||
| 118 | $this->entityMerger ? $this->entityMerger->merge($this, $entity, $attr) : $attr, |
||
| 119 | array(EntityStatus::MEMBER_NAME => $this->detectState($entity, $attr, $changeSetName)) |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Appends the dynamic to the static attributes for the EAV attribute |
||
| 125 | * and returns them. |
||
| 126 | * |
||
| 127 | * @return array The array with all available attributes |
||
| 128 | */ |
||
| 129 | protected function prepareDynamicAttributes() |
||
| 130 | { |
||
| 131 | return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array()); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Prepare the attributes of the entity that has to be persisted. |
||
| 136 | * |
||
| 137 | * @return array The prepared attributes |
||
| 138 | */ |
||
| 139 | protected function prepareAttributes() |
||
| 140 | { |
||
| 141 | |||
| 142 | // load the last attribute set |
||
| 143 | $attributeSet = $this->getLastAttributeSet(); |
||
| 144 | |||
| 145 | // Backward compatibility as default |
||
| 146 | $groupSortOrder = $this->getValue(ColumnKeys::SORT_ORDER, 0); |
||
| 147 | |||
| 148 | // load the attribute set values from the column |
||
| 149 | $defaultId = $this->getValue(ColumnKeys::DEFAULT_ID, 0); |
||
| 150 | $tabGroupCode = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_TAB_GROUP_CODE, 'basic'); |
||
| 151 | $attributeGroupName = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_NAME); |
||
| 152 | $attributeGroupCode = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_CODE); |
||
| 153 | $attributeGroupSortOrder = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_SORT_ORDER, $groupSortOrder); |
||
| 154 | |||
| 155 | // return the prepared product |
||
| 156 | return $this->initializeEntity( |
||
| 157 | $this->loadRawEntity( |
||
| 158 | array( |
||
| 159 | MemberNames::ATTRIBUTE_SET_ID => $attributeSet[MemberNames::ATTRIBUTE_SET_ID], |
||
| 160 | MemberNames::ATTRIBUTE_GROUP_NAME => $attributeGroupName, |
||
| 161 | MemberNames::DEFAULT_ID => $defaultId, |
||
| 162 | MemberNames::ATTRIBUTE_GROUP_CODE => $attributeGroupCode, |
||
| 163 | MemberNames::TAB_GROUP_CODE => $tabGroupCode, |
||
| 164 | MemberNames::SORT_ORDER => $attributeGroupSortOrder |
||
| 165 | ) |
||
| 166 | ) |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
| 172 | * |
||
| 173 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
| 174 | * |
||
| 175 | * @return array The initialized entity |
||
| 176 | */ |
||
| 177 | protected function loadRawEntity(array $data = array()) |
||
| 178 | { |
||
| 179 | return $this->getAttributeSetBunchProcessor()->loadRawEntity(EntityTypeCodes::EAV_ATTRIBUTE_GROUP, $data); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Initialize the attribute with the passed attributes and returns an instance. |
||
| 184 | * |
||
| 185 | * @param array $attr The attribute attributes |
||
| 186 | * |
||
| 187 | * @return array The initialized attribute |
||
| 188 | */ |
||
| 189 | protected function initializeAttribute(array $attr) |
||
| 190 | { |
||
| 191 | return $attr; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return's the entity type code to be used. |
||
| 196 | * |
||
| 197 | * @return string The entity type code to be used |
||
| 198 | */ |
||
| 199 | protected function getEntityTypeCode() |
||
| 200 | { |
||
| 201 | return $this->getSubject()->getEntityTypeCode(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Persist the passed attribute group. |
||
| 206 | * |
||
| 207 | * @param array $attributeGroup The attribute group to persist |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | protected function persistAttributeGroup(array $attributeGroup) |
||
| 212 | { |
||
| 213 | return $this->getAttributeSetBunchProcessor()->persistAttributeGroup($attributeGroup); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 214 | } |
||
| 215 | } |
||
| 216 |