Adapter/ShopwareAdapter/ServiceBus/CommandHandler/Product/HandleProductCommandHandler.php
public function handle(CommandInterface $command)
    {
        $shopLocaleId = $this->shopDataProvider->getDefaultShop()->getLocale()->getId();

        $languageIdentity = $this->identityService->findOneBy([
            'adapterIdentifier' => (string) $shopLocaleId,
            'adapterName' => ShopwareAdapter::NAME,
            'objectType' => Language::TYPE,
        ]);

        /**
         * @var Product $product
         */
        $product = $command->getPayload();

        if (null !== $languageIdentity) {
            /**
             * @var Product $translated
             */
            $translated = $this->translationHelper->translate($languageIdentity->getObjectIdentifier(), $product);

            if (null !== $translated) {
                $product = $translated;
            }
        }

        $params = $this->productRequestGenerator->generate($product);

        if (empty($params)) {
            return false;
        }

        $articleResource = $this->getArticleResource();

        $productIdentity = $this->identityService->findOneBy([
            'objectIdentifier' => $product->getIdentifier(),
            'objectType' => Product::TYPE,
            'adapterName' => ShopwareAdapter::NAME,
        ]);

        $variationRepository = $this->entityManager->getRepository(Detail::class);

        /**
         * @var null|Detail $mainVariation
         */
        $mainVariation = $variationRepository->findOneBy(['number' => $product->getNumber()]);

        if (null === $mainVariation && null !== $productIdentity) {
            $mainVariation = $variationRepository->findOneBy(['articleId' => $productIdentity->getAdapterIdentifier(), 'kind' => 1]);
        }

        if (null === $productIdentity) {
            if (null === $mainVariation) {
                $productModel = $articleResource->create($params);
            } else {
                $this->correctMainDetailAssignment($mainVariation);

                foreach ($mainVariation->getImages() as $image) {
                    if (null !== $image) {
                        $this->translationDataPersister->removeMediaTranslation($image);
                    }
                }

                $productModel = $articleResource->update($mainVariation->getArticleId(), $params);
            }

            $this->identityService->insert(
                $product->getIdentifier(),
                Product::TYPE,
                (string) $productModel->getId(),
                ShopwareAdapter::NAME
            );
        } else {
            try {
                $articleRepository = $this->entityManager->getRepository(ArticleModel::class);

                $productModel = $articleRepository->find($productIdentity->getAdapterIdentifier());

                if (null === $mainVariation) {
                    $variationResource = $this->getVariationResource();

                    $mainVariation = $variationResource->create([
                        'articleId' => $productIdentity->getAdapterIdentifier(),
                        'number' => $product->getNumber(),
                        'active' => true,
                    ]);
                }

                foreach ($productModel->getImages() as $image) {
                    if (null !== $image) {
                        $this->translationDataPersister->removeMediaTranslation($image);
                    }
                }

                $this->correctMainDetailAssignment($mainVariation);
                $productModel = $articleResource->update($productModel->getId(), $params);
            } catch (NotFoundException $exception) {
                $productModel = $articleResource->create($params);

                $this->identityService->update(
                    $productIdentity,
                    [
                        'adapterIdentifier' => (string) $productModel->getId(),
                    ]
                );
            }

            $this->attributeDataPersister->saveProductDetailAttributes(
                $productModel->getMainDetail(),
                $product->getAttributes()
            );

            $this->translationDataPersister->writeProductTranslations($product);
        }

        return true;
    }
public function handle(CommandInterface $command)
    {
        $shopLocaleId = $this->shopDataProvider->getDefaultShop()->getLocale()->getId();

        $languageIdentity = $this->identityService->findOneBy([
            'adapterIdentifier' => (string) $shopLocaleId,
            'adapterName' => ShopwareAdapter::NAME,
            'objectType' => Language::TYPE,
        ]);

        /**
         * @var Product $product
         */
        $product = $command->getPayload();

        if (null !== $languageIdentity) {
            /**
             * @var Product $translated
             */
            $translated = $this->translationHelper->translate($languageIdentity->getObjectIdentifier(), $product);

            if (null !== $translated) {
                $product = $translated;
            }
        }

        $params = $this->productRequestGenerator->generate($product);

        if (empty($params)) {
            return false;
        }

        $articleResource = $this->getArticleResource();

        $productIdentity = $this->identityService->findOneBy([
            'objectIdentifier' => $product->getIdentifier(),
            'objectType' => Product::TYPE,
            'adapterName' => ShopwareAdapter::NAME,
        ]);

        $variationRepository = $this->entityManager->getRepository(Detail::class);

        /**
         * @var null|Detail $mainVariation
         */
        $mainVariation = $variationRepository->findOneBy(['number' => $product->getNumber()]);

        if (null === $mainVariation && null !== $productIdentity) {
            $mainVariation = $variationRepository->findOneBy(['articleId' => $productIdentity->getAdapterIdentifier(), 'kind' => 1]);
        }

        if (null === $productIdentity) {
            if (null === $mainVariation) {
                $productModel = $articleResource->create($params);
            } else {
                $this->correctMainDetailAssignment($mainVariation);

                foreach ($mainVariation->getImages() as $image) {
                    if (null !== $image) {
                        $this->translationDataPersister->removeMediaTranslation($image);
                    }
                }

                $productModel = $articleResource->update($mainVariation->getArticleId(), $params);
            }

            $this->identityService->insert(
                $product->getIdentifier(),
                Product::TYPE,
                (string) $productModel->getId(),
                ShopwareAdapter::NAME
            );
        } else {
            try {
                $articleRepository = $this->entityManager->getRepository(ArticleModel::class);

                $productModel = $articleRepository->find($productIdentity->getAdapterIdentifier());

                if (null === $mainVariation) {
                    $variationResource = $this->getVariationResource();

                    $mainVariation = $variationResource->create([
                        'articleId' => $productIdentity->getAdapterIdentifier(),
                        'number' => $product->getNumber(),
                        'active' => true,
                    ]);
                }

                foreach ($productModel->getImages() as $image) {
                    if (null !== $image) {
                        $this->translationDataPersister->removeMediaTranslation($image);
                    }
                }

                $this->correctMainDetailAssignment($mainVariation);
                $productModel = $articleResource->update($productModel->getId(), $params);
            } catch (NotFoundException $exception) {
                $productModel = $articleResource->create($params);

                $this->identityService->update(
                    $productIdentity,
                    [
                        'adapterIdentifier' => (string) $productModel->getId(),
                    ]
                );
            }

            $this->attributeDataPersister->saveProductDetailAttributes(
                $productModel->getMainDetail(),
                $product->getAttributes()
            );

            $this->translationDataPersister->writeProductTranslations($product);
        }

        return true;
    }
{@inheritdoc}

@param TransferObjectCommand $command