src/Observers/AttributeObserverTrait.php
/**
     * Process the observer's business logic.
     *
     * @return void
     */
Process the observer's business logic.
@return void
Process the observer ' s business logic . @return void
protected function process()
    {
        $this->prepareStoreViewCode();
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
        $this->getAttributesByPrimaryKeyAndStoreId($this->getPrimaryKey(), $storeId);
        $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN);
        if ($this->storeViewHasBeenProcessed($pk = $this->getPrimaryKeyValue(), $storeViewCode)) {
            $this->getSystemLogger()->warning(
                $this->appendExceptionSuffix(
                    sprintf(
                        'Attributes for "%s" "%s" + store view code "%s" has already been processed',
                        $this->getPrimaryKeyColumnName(),
                        $pk,
                        $storeViewCode
                    )
                )
            );
            return;
        }
        $attributes = $this->getAttributes();
        $backendTypes = $this->getBackendTypes();
        $headers = array_flip($this->getHeaders());
        $row = $this->clearRow();
        foreach ($row as $key => $attributeValue) {
            if (!isset($attributes[$attributeCode = $headers[$key]])) {
                 in debug mode
                if ($this->isDebugMode()) {
                    $this->getSystemLogger()->debug(
                        $this->appendExceptionSuffix(
                            sprintf(
                                'Can\'t find attribute with attribute code "%s"',
                                $attributeCode
                            )
                        )
                    );
                }
                continue;
            } else {
                 in debug mode
                if ($this->isDebugMode()) {
                 in debug mode
                    $this->getSystemLogger()->debug(
                        $this->appendExceptionSuffix(
                            sprintf(
                                'Found attribute with attribute code "%s"',
                                $attributeCode
                            )
                        )
                    );
                }
            }
            $this->attribute = $attributes[$attributeCode];
            $backendType = $this->attribute[MemberNames::BACKEND_TYPE];
            if ($backendType === null) {
                 in debug mode
                $this->getSystemLogger()->warning(
                    $this->appendExceptionSuffix(
                        sprintf(
                            'Found EMTPY backend type for attribute "%s"',
                            $attributeCode
                        )
                    )
                );
                continue;
            }
            if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) {
                continue;
            }
            if (isset($attribute[MemberNames::IS_GLOBAL]) &&
                $attribute[MemberNames::IS_GLOBAL] == 1 &&
                $storeId !== 0) {
                continue;
            }
            if (isset($backendTypes[$backendType])) {
                $this->backendType = $backendType;
                $this->attributeCode = $attributeCode;
                $this->attributeId = $this->attribute[MemberNames::ATTRIBUTE_ID];
                $this->attributeValue = $attributeValue;
                list ($persistMethod, , $deleteMethod) = $backendTypes[$backendType];
                if ($this->hasChanges($value = $this->initializeAttribute($this->prepareAttributes()))) {
                    switch ($this->operation) {
                        case OperationNames::CREATE:
                        case OperationNames::UPDATE:
                            $this->$persistMethod($value);
                            break;
                        case OperationNames::DELETE:
                            $this->$deleteMethod(array(MemberNames::VALUE_ID => $value[MemberNames::VALUE_ID]));
                            break;
                        case OperationNames::SKIP:
                            $this->getSubject()->getSystemLogger()->debug(sprintf('Skipped processing attribute "%s"', $attributeCode));
                            break;
                        default:
                            $this->getSubject()->getSystemLogger()->debug(sprintf('Found invalid entity status "%s" for attribute "%s"', $value[MemberNames::VALUE] ?? 'NULL', $attributeCode));
                    }
                } else {
                    $this->getSubject()->getSystemLogger()->debug(sprintf('Skip to persist value for attribute "%s"', $attributeCode));
                }
                continue;
            }
            $this->getSystemLogger()->debug(
                $this->getSubject()->appendExceptionSuffix(
                    sprintf(
                        'Found invalid backend type %s for attribute "%s"',
                        $backendType,
                        $attributeCode
                    )
                )
            );
        }
    }
protected function process ( ) { $ this -> prepareStoreViewCode ( ) ; $ storeId = $ this -> getRowStoreId ( StoreViewCodes :: ADMIN ) ; $ this -> getAttributesByPrimaryKeyAndStoreId ( $ this -> getPrimaryKey ( ) , $ storeId ) ; $ storeViewCode = $ this -> getSubject ( ) -> getStoreViewCode ( StoreViewCodes :: ADMIN ) ; if ( $ this -> storeViewHasBeenProcessed ( $ pk = $ this -> getPrimaryKeyValue ( ) , $ storeViewCode ) ) { $ this -> getSystemLogger ( ) -> warning ( $ this -> appendExceptionSuffix ( sprintf ( 'Attributes for "%s" "%s" + store view code "%s" has already been processed' , $ this -> getPrimaryKeyColumnName ( ) , $ pk , $ storeViewCode ) ) ) ; return ; } $ attributes = $ this -> getAttributes ( ) ; $ backendTypes = $ this -> getBackendTypes ( ) ; $ headers = array_flip ( $ this -> getHeaders ( ) ) ; $ row = $ this -> clearRow ( ) ; foreach ( $ row as $ key => $ attributeValue ) { if ( ! isset ( $ attributes [ $ attributeCode = $ headers [ $ key ] ] ) ) { if ( $ this -> isDebugMode ( ) ) { $ this -> getSystemLogger ( ) -> debug ( $ this -> appendExceptionSuffix ( sprintf ( 'Can\'t find attribute with attribute code "%s"' , $ attributeCode ) ) ) ; } continue ; } else { if ( $ this -> isDebugMode ( ) ) { $ this -> getSystemLogger ( ) -> debug ( $ this -> appendExceptionSuffix ( sprintf ( 'Found attribute with attribute code "%s"' , $ attributeCode ) ) ) ; } } $ this -> attribute = $ attributes [ $ attributeCode ] ; $ backendType = $ this -> attribute [ MemberNames :: BACKEND_TYPE ] ; if ( $ backendType === null ) { $ this -> getSystemLogger ( ) -> warning ( $ this -> appendExceptionSuffix ( sprintf ( 'Found EMTPY backend type for attribute "%s"' , $ attributeCode ) ) ) ; continue ; } if ( $ backendType === BackendTypeKeys :: BACKEND_TYPE_STATIC ) { continue ; } if ( isset ( $ attribute [ MemberNames :: IS_GLOBAL ] ) && $ attribute [ MemberNames :: IS_GLOBAL ] == 1 && $ storeId !== 0 ) { continue ; } if ( isset ( $ backendTypes [ $ backendType ] ) ) { $ this -> backendType = $ backendType ; $ this -> attributeCode = $ attributeCode ; $ this -> attributeId = $ this -> attribute [ MemberNames :: ATTRIBUTE_ID ] ; $ this -> attributeValue = $ attributeValue ; list ( $ persistMethod , , $ deleteMethod ) = $ backendTypes [ $ backendType ] ; if ( $ this -> hasChanges ( $ value = $ this -> initializeAttribute ( $ this -> prepareAttributes ( ) ) ) ) { switch ( $ this -> operation ) { case OperationNames :: CREATE : case OperationNames :: UPDATE : $ this -> $ persistMethod ( $ value ) ; break ; case OperationNames :: DELETE : $ this -> $ deleteMethod ( array ( MemberNames :: VALUE_ID => $ value [ MemberNames :: VALUE_ID ] ) ) ; break ; case OperationNames :: SKIP : $ this -> getSubject ( ) -> getSystemLogger ( ) -> debug ( sprintf ( 'Skipped processing attribute "%s"' , $ attributeCode ) ) ; break ; default : $ this -> getSubject ( ) -> getSystemLogger ( ) -> debug ( sprintf ( 'Found invalid entity status "%s" for attribute "%s"' , $ value [ MemberNames :: VALUE ] ?? 'NULL' , $ attributeCode ) ) ; } } else { $ this -> getSubject ( ) -> getSystemLogger ( ) -> debug ( sprintf ( 'Skip to persist value for attribute "%s"' , $ attributeCode ) ) ; } continue ; } $ this -> getSystemLogger ( ) -> debug ( $ this -> getSubject ( ) -> appendExceptionSuffix ( sprintf ( 'Found invalid backend type %s for attribute "%s"' , $ backendType , $ attributeCode ) ) ) ; } }
Process the observer's business logic.
Process the observer ' s business logic .
// initialize the store view code // load the store ID, use the admin store if NO store view code has been set // load the entity's existing attributes // load the store view - if no store view has been set, we assume the admin // store view, which will contain the default (fallback) attribute values // query whether or not the row has already been processed // log a message // return immediately // load the attributes by the found attribute set and the backend types // load the header keys // remove all the empty values from the row // iterate over the attributes and append them to the row // query whether or not attribute with the found code exists // log a message in debug mode // stop processing // log a message in debug mode // log a message in debug mode // if yes, load the attribute by its code // load the backend type => to find the apropriate entity // log a message in debug mode // stop processing // do nothing on static backend type // do nothing in non-default stores with global attributes // query whether or not we've found a supported backend type // initialize attribute ID/code and backend type // set the attribute value as well as the original attribute value // initialize the persist method for the found backend type // prepare the attribute vale and query whether or not it has to be persisted // query whether or not the entity's value has to be persisted or deleted. if the value is // an empty string and the status is UPDATE, then the value exists and has to be deleted // We need to user $attributeValue instead of $value[MemberNames::VALUE] in cases where // value was casted by attribute type. E.g. special_price = 0 if value is empty string in CSV // create/update the attribute // delete the attribute // skip the attribute // should never happen // continue with the next value // log the debug message