Adapter/PlentymarketsAdapter/ResponseParser/Product/Variation/VariationResponseParser.php
public function parse(array $product)
    {
        $productIdentitiy = $this->identityService->findOneBy([
            'adapterIdentifier' => (string) $product['id'],
            'adapterName' => PlentymarketsAdapter::NAME,
            'objectType' => Product::TYPE,
        ]);

        if (null === $productIdentitiy) {
            return [];
        }

        $variations = $product['variations'];

        $mainVariation = $this->variationHelper->getMainVariation($variations);

        if (empty($mainVariation)) {
            return [];
        }

        if (Product::MULTIPACK === $product['itemType']) {
            $variations = array_filter($variations, function (array $variation) {
                return $variation['isMain'];
            });
        }

        if (count($variations) > 1) {
            $variations = array_filter($variations, function (array $variation) {
                return !empty($variation['variationAttributeValues']);
            });
        }

        usort($variations, function (array $a, array $b) {
            if ((int) $a['position'] === (int) $b['position']) {
                return 0;
            }

            return ((int) $a['position'] < (int) $b['position']) ? -1 : 1;
        });

        $result = [];

        foreach ($variations as $variation) {
            $identity = $this->identityService->findOneOrCreate(
                (string) $variation['id'],
                PlentymarketsAdapter::NAME,
                Variation::TYPE
            );

            $variationObject = new Variation();
            $variationObject->setIdentifier($identity->getObjectIdentifier());
            $variationObject->setProductIdentifier($productIdentitiy->getObjectIdentifier());
            $variationObject->setActive((bool) $variation['isActive']);
            $variationObject->setNumber($this->getVariationNumber($variation));
            $variationObject->setStockLimitation($this->getStockLimitation($variation));
            $variationObject->setBarcodes($this->getBarcodes($variation));
            $variationObject->setPosition((int) $variation['position']);
            $variationObject->setModel((string) $variation['model']);
            $variationObject->setImages($this->getVariationImages($product['texts'], $variation, $result));
            $variationObject->setPrices($this->priceResponseParser->parse($variation));
            $variationObject->setPurchasePrice((float) $variation['purchasePrice']);
            $variationObject->setUnitIdentifier($this->getUnitIdentifier($variation));
            $variationObject->setContent((float) $variation['unit']['content']);
            $variationObject->setReferenceAmount($this->referenceAmountCalculator->calculate($variation));
            $variationObject->setMaximumOrderQuantity((float) $variation['maximumOrderQuantity']);
            $variationObject->setMinimumOrderQuantity((float) $variation['minimumOrderQuantity']);
            $variationObject->setIntervalOrderQuantity((float) $variation['intervalOrderQuantity']);
            $variationObject->setReleaseDate($this->getReleaseDate($variation));
            $variationObject->setShippingTime($this->getShippingTime($variation));
            $variationObject->setWidth((int) $variation['widthMM']);
            $variationObject->setHeight((int) $variation['heightMM']);
            $variationObject->setLength((int) $variation['lengthMM']);
            $variationObject->setWeight($this->getVariationWeight($variation));
            $variationObject->setProperties($this->getVariationProperties($variation));

            $stockObject = $this->stockResponseParser->parse($variation);

            if (null === $stockObject) {
                continue;
            }

            $importVariationsWithoutStock = json_decode($this->configService->get('import_variations_without_stock', true));

            if (!$importVariationsWithoutStock && empty($stockObject->getStock())) {
                continue;
            }

            $result[$variationObject->getIdentifier()] = $variationObject;
            $result[$stockObject->getIdentifier()] = $stockObject;
        }

        $variations = array_filter($result, function (TransferObjectInterface $object) {
            return $object instanceof Variation;
        });

        $mainVariationNumber = $this->variationHelper->getMainVariationNumber($mainVariation, $variations);

        /**
         * @var Variation $variation
         */
        foreach ($variations as &$variation) {
            if ($variation->getNumber() === $mainVariationNumber) {
                $variation->setIsMain(true);

                $checkActiveMainVariation = json_decode($this->configService->get('check_active_main_variation'));

                if ($checkActiveMainVariation && !$mainVariation['isActive']) {
                    $variation->setActive(false);
                }

                break;
            }
        }

        return $result;
    }
public function parse(array $product)
    {
        $productIdentitiy = $this->identityService->findOneBy([
            'adapterIdentifier' => (string) $product['id'],
            'adapterName' => PlentymarketsAdapter::NAME,
            'objectType' => Product::TYPE,
        ]);

        if (null === $productIdentitiy) {
            return [];
        }

        $variations = $product['variations'];

        $mainVariation = $this->variationHelper->getMainVariation($variations);

        if (empty($mainVariation)) {
            return [];
        }

        if (Product::MULTIPACK === $product['itemType']) {
            $variations = array_filter($variations, function (array $variation) {
                return $variation['isMain'];
            });
        }

        if (count($variations) > 1) {
            $variations = array_filter($variations, function (array $variation) {
                return !empty($variation['variationAttributeValues']);
            });
        }

        usort($variations, function (array $a, array $b) {
            if ((int) $a['position'] === (int) $b['position']) {
                return 0;
            }

            return ((int) $a['position'] < (int) $b['position']) ? -1 : 1;
        });

        $result = [];

        foreach ($variations as $variation) {
            $identity = $this->identityService->findOneOrCreate(
                (string) $variation['id'],
                PlentymarketsAdapter::NAME,
                Variation::TYPE
            );

            $variationObject = new Variation();
            $variationObject->setIdentifier($identity->getObjectIdentifier());
            $variationObject->setProductIdentifier($productIdentitiy->getObjectIdentifier());
            $variationObject->setActive((bool) $variation['isActive']);
            $variationObject->setNumber($this->getVariationNumber($variation));
            $variationObject->setStockLimitation($this->getStockLimitation($variation));
            $variationObject->setBarcodes($this->getBarcodes($variation));
            $variationObject->setPosition((int) $variation['position']);
            $variationObject->setModel((string) $variation['model']);
            $variationObject->setImages($this->getVariationImages($product['texts'], $variation, $result));
            $variationObject->setPrices($this->priceResponseParser->parse($variation));
            $variationObject->setPurchasePrice((float) $variation['purchasePrice']);
            $variationObject->setUnitIdentifier($this->getUnitIdentifier($variation));
            $variationObject->setContent((float) $variation['unit']['content']);
            $variationObject->setReferenceAmount($this->referenceAmountCalculator->calculate($variation));
            $variationObject->setMaximumOrderQuantity((float) $variation['maximumOrderQuantity']);
            $variationObject->setMinimumOrderQuantity((float) $variation['minimumOrderQuantity']);
            $variationObject->setIntervalOrderQuantity((float) $variation['intervalOrderQuantity']);
            $variationObject->setReleaseDate($this->getReleaseDate($variation));
            $variationObject->setShippingTime($this->getShippingTime($variation));
            $variationObject->setWidth((int) $variation['widthMM']);
            $variationObject->setHeight((int) $variation['heightMM']);
            $variationObject->setLength((int) $variation['lengthMM']);
            $variationObject->setWeight($this->getVariationWeight($variation));
            $variationObject->setProperties($this->getVariationProperties($variation));

            $stockObject = $this->stockResponseParser->parse($variation);

            if (null === $stockObject) {
                continue;
            }

            $importVariationsWithoutStock = json_decode($this->configService->get('import_variations_without_stock', true));

            if (!$importVariationsWithoutStock && empty($stockObject->getStock())) {
                continue;
            }

            $result[$variationObject->getIdentifier()] = $variationObject;
            $result[$stockObject->getIdentifier()] = $stockObject;
        }

        $variations = array_filter($result, function (TransferObjectInterface $object) {
            return $object instanceof Variation;
        });

        $mainVariationNumber = $this->variationHelper->getMainVariationNumber($mainVariation, $variations);

        /**
         * @var Variation $variation
         */
        foreach ($variations as &$variation) {
            if ($variation->getNumber() === $mainVariationNumber) {
                $variation->setIsMain(true);

                $checkActiveMainVariation = json_decode($this->configService->get('check_active_main_variation'));

                if ($checkActiveMainVariation && !$mainVariation['isActive']) {
                    $variation->setActive(false);
                }

                break;
            }
        }

        return $result;
    }
@param array $product

@return TransferObjectInterface[]