protected function processAttributes(array $data, ProductInterface $product, $locale)
    {
        if (isset($data['attributes'])) {
            // Create local array of all currently assigned attributes of product.
            $productAttributes = [];
            foreach ($product->getProductAttributes() as $productAttribute) {
                $productAttributes[$productAttribute->getAttribute()->getId()] = $productAttribute;
            }

            // Save all attribute ids which are given in the request.
            $attributeIdsInRequest = [];

            // Add and change attributes.
            foreach ($data['attributes'] as $attributeData) {
                if (!isset($attributeData['attributeId'])) {
                    continue;
                }

                $attributeId = $attributeData['attributeId'];
                $attributeIdsInRequest[$attributeId] = $attributeId;

                // Check if a attributeValueName is provided, otherwise do nothing.
                if (!array_key_exists('attributeValueName', $attributeData)) {
                    continue;
                }
                $attributeDataValueName = trim($attributeData['attributeValueName']);

                // Get attributeValueLocale from request.
                $attributeValueLocale = $locale;
                if (isset($attributeData['attributeValueLocale'])) {
                    $attributeValueLocale = $attributeData['attributeValueLocale'];
                }

                // If attribute value is empty do not add.
                if ('' === $attributeDataValueName || null === $attributeDataValueName) {
                    // If already set on product, remove attribute value translation.
                    if (array_key_exists($attributeId, $productAttributes)) {
                        /** @var ProductAttribute $productAttribute */
                        $productAttribute = $productAttributes[$attributeId];

                        // Remove attribute value translation from attribute value.
                        $this->productAttributeManager->removeAttributeValueTranslation(
                            $productAttribute->getAttributeValue(),
                            $attributeValueLocale
                        );

                        // If no more attribute value translation exists,
                        // remove the whole product attribute from the product.
                        if ($productAttribute->getAttributeValue()->getTranslations()->isEmpty()) {
                            $product->removeProductAttribute($productAttribute);
                            $this->em->remove($productAttribute);
                        }
                    }
                    continue;
                }

                // Product attribute does not exists.
                if (!array_key_exists($attributeId, $productAttributes)) {
                    $attribute = $this->retrieveAttributeById($attributeId);
                    // Create new attribute value with translation.
                    $attributeValue = $this->productAttributeManager->createAttributeValue(
                        $attribute,
                        $attributeDataValueName,
                        $attributeValueLocale
                    );
                    // Create new product attribute.
                    $this->productAttributeManager->createProductAttribute($attribute, $product, $attributeValue);
                } else {
                    // Product attribute exists.
                    /** @var ProductAttribute $productAttribute */
                    $productAttribute = $productAttributes[$attributeId];
                    $attributeValue = $productAttribute->getAttributeValue();
                    // Create translation in current locale.
                    $this->productAttributeManager->setOrCreateAttributeValueTranslation(
                        $attributeValue,
                        $attributeDataValueName,
                        $attributeValueLocale
                    );
                }
            }

            // Delete attributes.
            foreach ($productAttributes as $productAttribute) {
                if (!array_key_exists($productAttribute->getAttribute()->getId(), $attributeIdsInRequest)) {
                    // Remove all attribute value translations from attribute value.
                    $this->productAttributeManager->removeAllAttributeValueTranslations(
                        $productAttribute->getAttributeValue()
                    );

                    // Remove attribute from product.
                    $product->removeProductAttribute($productAttribute);
                    $this->em->remove($productAttribute);
                }
            }
        }
    }