techdivision /
import-attribute-set
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * TechDivision\Import\Attribute\Set\Observers\CopyParentAttributeSetObserver |
||
| 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\Attribute\Set\Utils\ColumnKeys; |
||
| 18 | use TechDivision\Import\Attribute\Set\Utils\MemberNames; |
||
| 19 | use TechDivision\Import\Dbal\Utils\EntityStatus; |
||
| 20 | use TechDivision\Import\Attribute\Set\Utils\ConfigurationKeys; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Observer that copies the EAV attribute groups and attribute relations from the parent. |
||
| 24 | * |
||
| 25 | * @author Tim Wagner <[email protected]> |
||
| 26 | * @copyright 2019 TechDivision GmbH <[email protected]> |
||
| 27 | * @license https://opensource.org/licenses/MIT |
||
| 28 | * @link https://github.com/techdivision/import-attribute-set |
||
| 29 | * @link http://www.techdivision.com |
||
| 30 | */ |
||
| 31 | class CopyParentAttributeSetObserver extends AbstractAttributeSetObserver |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Process the observer's business logic. |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | protected function process() |
||
| 40 | { |
||
| 41 | |||
| 42 | // load the last attribute set |
||
| 43 | $attributeSet = $this->getLastAttributeSet(); |
||
| 44 | |||
| 45 | // query whether or not the a given attribute set has to be extended |
||
| 46 | if ($this->shouldCopy()) { |
||
| 47 | // load the attribute set the new one is based on |
||
| 48 | $basedOn = $this->loadAttributeSetByEntityTypeCodeAndAttributeSetName( |
||
| 49 | $this->getValue(ColumnKeys::ENTITY_TYPE_CODE), |
||
| 50 | $this->getValue(ColumnKeys::BASED_ON) |
||
| 51 | ); |
||
| 52 | |||
| 53 | // load the attribute groups of the attribute set the new one should be based on |
||
| 54 | $attributeGroupsBasedOn = $this->loadAttributeGroupsByAttributeSetId($basedOn[MemberNames::ATTRIBUTE_SET_ID]); |
||
| 55 | |||
| 56 | // copy the attribute groups from the based on |
||
| 57 | foreach ($attributeGroupsBasedOn as $attributeGroupBasedOn) { |
||
| 58 | // create the attribute group |
||
| 59 | $attributeGroupId = $this->persistAttributeGroup( |
||
|
0 ignored issues
–
show
|
|||
| 60 | $attributeGroup = $this->initializeAttributeForAttributeGroup( |
||
| 61 | $this->prepareAttributesForAttributeGroup($attributeSet, $attributeGroupBasedOn) |
||
| 62 | ) |
||
| 63 | ); |
||
| 64 | |||
| 65 | // set the new attribute group ID |
||
| 66 | $attributeGroup[MemberNames::ATTRIBUTE_GROUP_ID] = $attributeGroupId; |
||
| 67 | |||
| 68 | // load the entity attributes of the attribute group |
||
| 69 | $entityAttributes = $this->loadEntityAttributesByAttributeGroupId($attributeGroupBasedOn[MemberNames::ATTRIBUTE_GROUP_ID]); |
||
| 70 | |||
| 71 | // copy the attribute group => attribute relation |
||
| 72 | foreach ($entityAttributes as $entityAttribute) { |
||
| 73 | $this->persistEntityAttribute( |
||
| 74 | $this->initializeAttributeForEntityAttribute( |
||
| 75 | $this->prepareAttributesForEntityAttribute($attributeSet, $attributeGroup, $entityAttribute) |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Queries whether or not the attribute groups and attribute relations of the attribute |
||
| 85 | * set the actual one is based on, should be copied or not. |
||
| 86 | * |
||
| 87 | * They SHOULD be copied if the attribute set is new and has been created recently or |
||
| 88 | * if the attribute set already exists and the `copy-parent-on-update` flag parameter |
||
| 89 | * has been set to TRUE. |
||
| 90 | * |
||
| 91 | * @return boolean TRUE if the attribute groups and attribute relations should be copied, else FALSE |
||
| 92 | */ |
||
| 93 | protected function shouldCopy() |
||
| 94 | { |
||
| 95 | |||
| 96 | // load the last attribute set |
||
| 97 | $attributeSet = $this->getLastAttributeSet(); |
||
| 98 | |||
| 99 | // query whether or not the a given attribute set has to be extended |
||
| 100 | return $this->hasValue(ColumnKeys::BASED_ON) && ( |
||
| 101 | $attributeSet[EntityStatus::MEMBER_NAME] === EntityStatus::STATUS_CREATE || ( |
||
| 102 | $attributeSet[EntityStatus::MEMBER_NAME] === EntityStatus::STATUS_UPDATE && $this->shouldCopyParentOnUpdate()) |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Queries whether or not the configuration param `copy-parent-on-update` has been set, |
||
| 108 | * if yes it returns the value. |
||
| 109 | * |
||
| 110 | * @return boolean TRUE if the param is set and the value is TRUE, else FALSE |
||
| 111 | */ |
||
| 112 | protected function shouldCopyParentOnUpdate() |
||
| 113 | { |
||
| 114 | |||
| 115 | // load the subject's configuration |
||
| 116 | $configuration = $this->getSubject()->getConfiguration(); |
||
| 117 | |||
| 118 | // return the configuration value |
||
| 119 | if ($configuration->hasParam(ConfigurationKeys::COPY_PARENT_ON_UPDATE)) { |
||
| 120 | return $configuration->getParam(ConfigurationKeys::COPY_PARENT_ON_UPDATE); |
||
| 121 | } |
||
| 122 | |||
| 123 | // return FALSE |
||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Prepare the attributes of the entity that has to be persisted. |
||
| 129 | * |
||
| 130 | * @param array $attributeSet The attribute set with the data used to prepare the entity attribute |
||
| 131 | * @param array $attributeGroup The attribute group with the data used to prepare the entity attribute |
||
| 132 | * @param array $entityAttribute The entity attribute with the data used to prepare the entity attribute |
||
| 133 | * |
||
| 134 | * @return array The prepared attributes |
||
| 135 | */ |
||
| 136 | protected function prepareAttributesForEntityAttribute(array $attributeSet, array $attributeGroup, array $entityAttribute) |
||
| 137 | { |
||
| 138 | |||
| 139 | // return the prepared product |
||
| 140 | return $this->initializeEntity( |
||
| 141 | array( |
||
| 142 | MemberNames::ATTRIBUTE_SET_ID => $attributeSet[MemberNames::ATTRIBUTE_SET_ID], |
||
| 143 | MemberNames::ATTRIBUTE_GROUP_ID => $attributeGroup[MemberNames::ATTRIBUTE_GROUP_ID], |
||
| 144 | MemberNames::ATTRIBUTE_ID => $entityAttribute[MemberNames::ATTRIBUTE_ID], |
||
| 145 | MemberNames::ENTITY_TYPE_ID => $entityAttribute[MemberNames::ENTITY_TYPE_ID], |
||
| 146 | MemberNames::SORT_ORDER => $entityAttribute[MemberNames::SORT_ORDER] |
||
| 147 | ) |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Prepare the attributes of the entity that has to be persisted. |
||
| 153 | * |
||
| 154 | * @param array $attributeSet The attribute set with the data used to prepare the attribute group |
||
| 155 | * @param array $attributeGroup The attribute group with the data used to prepare the attribute group |
||
| 156 | * |
||
| 157 | * @return array The prepared attributes |
||
| 158 | */ |
||
| 159 | protected function prepareAttributesForAttributeGroup(array $attributeSet, array $attributeGroup) |
||
| 160 | { |
||
| 161 | |||
| 162 | // return the prepared product |
||
| 163 | return $this->initializeEntity( |
||
| 164 | array( |
||
| 165 | MemberNames::ATTRIBUTE_SET_ID => $attributeSet[MemberNames::ATTRIBUTE_SET_ID], |
||
| 166 | MemberNames::ATTRIBUTE_GROUP_NAME => $attributeGroup[MemberNames::ATTRIBUTE_GROUP_NAME], |
||
| 167 | MemberNames::SORT_ORDER => $attributeGroup[MemberNames::SORT_ORDER], |
||
| 168 | MemberNames::DEFAULT_ID => $attributeGroup[MemberNames::DEFAULT_ID], |
||
| 169 | MemberNames::ATTRIBUTE_GROUP_CODE => $attributeGroup[MemberNames::ATTRIBUTE_GROUP_CODE], |
||
| 170 | MemberNames::TAB_GROUP_CODE => $attributeGroup[MemberNames::TAB_GROUP_CODE] |
||
| 171 | ) |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Initialize the attribute with the passed attributes and returns an instance. |
||
| 177 | * |
||
| 178 | * @param array $attr The attribute attributes |
||
| 179 | * |
||
| 180 | * @return array The initialized attribute |
||
| 181 | */ |
||
| 182 | protected function initializeAttributeForAttributeGroup(array $attr) |
||
| 183 | { |
||
| 184 | return $attr; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Initialize the attribute with the passed attributes and returns an instance. |
||
| 189 | * |
||
| 190 | * @param array $attr The attribute attributes |
||
| 191 | * |
||
| 192 | * @return array The initialized attribute |
||
| 193 | */ |
||
| 194 | protected function initializeAttributeForEntityAttribute(array $attr) |
||
| 195 | { |
||
| 196 | return $attr; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return's the entity type for the passed code, of if no entity type code has |
||
| 201 | * been passed, the default one from the configuration will be used. |
||
| 202 | * |
||
| 203 | * @param string|null $entityTypeCode The entity type code |
||
| 204 | * |
||
| 205 | * @return array The requested entity type |
||
| 206 | * @throws \Exception Is thrown, if the entity type with the passed code is not available |
||
| 207 | */ |
||
| 208 | protected function getEntityType($entityTypeCode = null) |
||
| 209 | { |
||
| 210 | return $this->getSubject()->getEntityType($entityTypeCode); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Load's and return's the EAV attribute set with the passed entity type code and attribute set name. |
||
| 215 | * |
||
| 216 | * @param string $entityTypeCode The entity type code of the EAV attribute set to load |
||
| 217 | * @param string $attributeSetName The attribute set name of the EAV attribute set to return |
||
| 218 | * |
||
| 219 | * @return array The EAV attribute set |
||
| 220 | */ |
||
| 221 | protected function loadAttributeSetByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName) |
||
| 222 | { |
||
| 223 | return $this->getAttributeSetBunchProcessor()->loadAttributeSetByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return's the attribute groups for the passed attribute set ID, whereas the array |
||
| 228 | * is prepared with the attribute group names as keys. |
||
| 229 | * |
||
| 230 | * @param mixed $attributeSetId The EAV attribute set ID to return the attribute groups for |
||
| 231 | * |
||
| 232 | * @return array|boolean The EAV attribute groups for the passed attribute ID |
||
| 233 | */ |
||
| 234 | protected function loadAttributeGroupsByAttributeSetId($attributeSetId) |
||
| 235 | { |
||
| 236 | return $this->getAttributeSetBunchProcessor()->loadAttributeGroupsByAttributeSetId($attributeSetId); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns the EAV entity attributes for the attribute group with the passed ID. |
||
| 241 | * |
||
| 242 | * @param integer $attributeGroupId The attribute group ID to load the EAV entity attributes for |
||
| 243 | * |
||
| 244 | * @return array|null The EAV attributes with for the passed attribute group ID |
||
| 245 | */ |
||
| 246 | protected function loadEntityAttributesByAttributeGroupId($attributeGroupId) |
||
| 247 | { |
||
| 248 | return $this->getAttributeSetBunchProcessor()->loadEntityAttributesByAttributeGroupId($attributeGroupId); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Persist the passed attribute group. |
||
| 253 | * |
||
| 254 | * @param array $attributeGroup The attribute group to persist |
||
| 255 | * |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | protected function persistAttributeGroup(array $attributeGroup) |
||
| 259 | { |
||
| 260 | return $this->getAttributeSetBunchProcessor()->persistAttributeGroup($attributeGroup); |
||
|
0 ignored issues
–
show
|
|||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Persist the passed entity attribute. |
||
| 265 | * |
||
| 266 | * @param array $entityAttribute The entity attribute to persist |
||
| 267 | * |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | protected function persistEntityAttribute(array $entityAttribute) |
||
| 271 | { |
||
| 272 | return $this->getAttributeSetBunchProcessor()->persistEntityAttribute($entityAttribute); |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->getAttributeSetBu...ibute($entityAttribute) targeting TechDivision\Import\Attr...ersistEntityAttribute() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 273 | } |
||
| 274 | } |
||
| 275 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.