Components/Bundle/ShopwareAdapter/CommandHandler/HandleBundleCommandHandler.php
public function handle(CommandInterface $command)
    {
        /**
         * @var Bundle $bundle
         */
        $bundle = $command->getPayload();

        $identity = $this->identityService->findOneBy(
            [
                'objectIdentifier' => (string) $bundle->getIdentifier(),
                'objectType' => Bundle::TYPE,
                'adapterName' => ShopwareAdapter::NAME,
            ]
        );

        $this->bundleHelper->registerBundleModels();

        /**
         * @var BundleRepository $repository
         */
        $repository = $this->entityManager->getRepository(BundleModel::class);

        if (null === $identity) {
            $existingBundle = $repository->findOneBy(['number' => $bundle->getNumber()]);

            if (null !== $existingBundle) {
                $identity = $this->identityService->insert(
                    $bundle->getIdentifier(),
                    Bundle::TYPE,
                    (string) $existingBundle->getId(),
                    ShopwareAdapter::NAME
                );
            }
        } else {
            $existingBundle = $repository->find($identity->getAdapterIdentifier());

            if (null === $existingBundle) {
                $this->identityService->remove($identity);

                $identity = null;
            }
        }

        if (null === $identity) {
            $bundleModel = new BundleModel();

            $bundleModel->setDisplayGlobal(true);
            $bundleModel->setSells(0);
            $bundleModel->setCreated();
            $bundleModel->setType(1);
            $bundleModel->setDiscountType('abs');
            $bundleModel->setQuantity(0);
            $bundleModel->setShowName(false);
        } else {
            /**
             * @var BundleModel $bundleModel
             */
            $bundleModel = $repository->find($identity->getAdapterIdentifier());

            foreach ($bundleModel->getPrices() as $price) {
                $this->entityManager->remove($price);
            }

            foreach ($bundleModel->getArticles() as $article) {
                $this->entityManager->remove($article);
            }

            $this->entityManager->flush();
        }

        try {
            $mainVariant = $this->getMainVariant($bundle);
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());

            return false;
        }

        if (null === $mainVariant) {
            return false;
        }

        $mainArticle = $mainVariant->getArticle();

        $this->active = $mainArticle->getActive();

        $bundleModel->setName($bundle->getName());
        $bundleModel->setValidFrom($bundle->getAvailableFrom());
        $bundleModel->setValidTo($bundle->getAvailableTo());
        $bundleModel->setLimited($bundle->hasStockLimitation());
        $bundleModel->setQuantity($this->getBundleStock($bundle->getNumber()));
        $bundleModel->setNumber($bundle->getNumber());
        $bundleModel->setArticle($mainArticle);
        $bundleModel->setCustomerGroups($this->getCustomerGroups($bundle));
        $bundleModel->setPrices($this->getPrices($bundle, $bundleModel));
        $bundleModel->setPosition($bundle->getPosition());
        $bundleModel->setArticles($this->getArticles($bundle, $bundleModel, $mainVariant));
        $bundleModel->setActive($this->active);

        $this->entityManager->persist($bundleModel);
        $this->entityManager->flush();
        $this->entityManager->clear();

        return true;
    }
public function handle(CommandInterface $command)
    {
        /**
         * @var Bundle $bundle
         */
        $bundle = $command->getPayload();

        $identity = $this->identityService->findOneBy(
            [
                'objectIdentifier' => (string) $bundle->getIdentifier(),
                'objectType' => Bundle::TYPE,
                'adapterName' => ShopwareAdapter::NAME,
            ]
        );

        $this->bundleHelper->registerBundleModels();

        /**
         * @var BundleRepository $repository
         */
        $repository = $this->entityManager->getRepository(BundleModel::class);

        if (null === $identity) {
            $existingBundle = $repository->findOneBy(['number' => $bundle->getNumber()]);

            if (null !== $existingBundle) {
                $identity = $this->identityService->insert(
                    $bundle->getIdentifier(),
                    Bundle::TYPE,
                    (string) $existingBundle->getId(),
                    ShopwareAdapter::NAME
                );
            }
        } else {
            $existingBundle = $repository->find($identity->getAdapterIdentifier());

            if (null === $existingBundle) {
                $this->identityService->remove($identity);

                $identity = null;
            }
        }

        if (null === $identity) {
            $bundleModel = new BundleModel();

            $bundleModel->setDisplayGlobal(true);
            $bundleModel->setSells(0);
            $bundleModel->setCreated();
            $bundleModel->setType(1);
            $bundleModel->setDiscountType('abs');
            $bundleModel->setQuantity(0);
            $bundleModel->setShowName(false);
        } else {
            /**
             * @var BundleModel $bundleModel
             */
            $bundleModel = $repository->find($identity->getAdapterIdentifier());

            foreach ($bundleModel->getPrices() as $price) {
                $this->entityManager->remove($price);
            }

            foreach ($bundleModel->getArticles() as $article) {
                $this->entityManager->remove($article);
            }

            $this->entityManager->flush();
        }

        try {
            $mainVariant = $this->getMainVariant($bundle);
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());

            return false;
        }

        if (null === $mainVariant) {
            return false;
        }

        $mainArticle = $mainVariant->getArticle();

        $this->active = $mainArticle->getActive();

        $bundleModel->setName($bundle->getName());
        $bundleModel->setValidFrom($bundle->getAvailableFrom());
        $bundleModel->setValidTo($bundle->getAvailableTo());
        $bundleModel->setLimited($bundle->hasStockLimitation());
        $bundleModel->setQuantity($this->getBundleStock($bundle->getNumber()));
        $bundleModel->setNumber($bundle->getNumber());
        $bundleModel->setArticle($mainArticle);
        $bundleModel->setCustomerGroups($this->getCustomerGroups($bundle));
        $bundleModel->setPrices($this->getPrices($bundle, $bundleModel));
        $bundleModel->setPosition($bundle->getPosition());
        $bundleModel->setArticles($this->getArticles($bundle, $bundleModel, $mainVariant));
        $bundleModel->setActive($this->active);

        $this->entityManager->persist($bundleModel);
        $this->entityManager->flush();
        $this->entityManager->clear();

        return true;
    }
public function handle ( CommandInterface $ command ) { /**
         * @var Bundle $bundle
         */ $ bundle = $ command -> getPayload ( ) ; $ identity = $ this -> identityService -> findOneBy ( [ 'objectIdentifier' => ( string ) $ bundle -> getIdentifier ( ) , 'objectType' => Bundle :: TYPE , 'adapterName' => ShopwareAdapter :: NAME , ] ) ; $ this -> bundleHelper -> registerBundleModels ( ) ; /**
         * @var BundleRepository $repository
         */ $ repository = $ this -> entityManager -> getRepository ( BundleModel :: class ) ; if ( null === $ identity ) { $ existingBundle = $ repository -> findOneBy ( [ 'number' => $ bundle -> getNumber ( ) ] ) ; if ( null !== $ existingBundle ) { $ identity = $ this -> identityService -> insert ( $ bundle -> getIdentifier ( ) , Bundle :: TYPE , ( string ) $ existingBundle -> getId ( ) , ShopwareAdapter :: NAME ) ; } } else { $ existingBundle = $ repository -> find ( $ identity -> getAdapterIdentifier ( ) ) ; if ( null === $ existingBundle ) { $ this -> identityService -> remove ( $ identity ) ; $ identity = null ; } } if ( null === $ identity ) { $ bundleModel = new BundleModel ( ) ; $ bundleModel -> setDisplayGlobal ( true ) ; $ bundleModel -> setSells ( 0 ) ; $ bundleModel -> setCreated ( ) ; $ bundleModel -> setType ( 1 ) ; $ bundleModel -> setDiscountType ( 'abs' ) ; $ bundleModel -> setQuantity ( 0 ) ; $ bundleModel -> setShowName ( false ) ; } else { /**
             * @var BundleModel $bundleModel
             */ $ bundleModel = $ repository -> find ( $ identity -> getAdapterIdentifier ( ) ) ; foreach ( $ bundleModel -> getPrices ( ) as $ price ) { $ this -> entityManager -> remove ( $ price ) ; } foreach ( $ bundleModel -> getArticles ( ) as $ article ) { $ this -> entityManager -> remove ( $ article ) ; } $ this -> entityManager -> flush ( ) ; } try { $ mainVariant = $ this -> getMainVariant ( $ bundle ) ; } catch ( Exception $ exception ) { $ this -> logger -> error ( $ exception -> getMessage ( ) ) ; return false ; } if ( null === $ mainVariant ) { return false ; } $ mainArticle = $ mainVariant -> getArticle ( ) ; $ this -> active = $ mainArticle -> getActive ( ) ; $ bundleModel -> setName ( $ bundle -> getName ( ) ) ; $ bundleModel -> setValidFrom ( $ bundle -> getAvailableFrom ( ) ) ; $ bundleModel -> setValidTo ( $ bundle -> getAvailableTo ( ) ) ; $ bundleModel -> setLimited ( $ bundle -> hasStockLimitation ( ) ) ; $ bundleModel -> setQuantity ( $ this -> getBundleStock ( $ bundle -> getNumber ( ) ) ) ; $ bundleModel -> setNumber ( $ bundle -> getNumber ( ) ) ; $ bundleModel -> setArticle ( $ mainArticle ) ; $ bundleModel -> setCustomerGroups ( $ this -> getCustomerGroups ( $ bundle ) ) ; $ bundleModel -> setPrices ( $ this -> getPrices ( $ bundle , $ bundleModel ) ) ; $ bundleModel -> setPosition ( $ bundle -> getPosition ( ) ) ; $ bundleModel -> setArticles ( $ this -> getArticles ( $ bundle , $ bundleModel , $ mainVariant ) ) ; $ bundleModel -> setActive ( $ this -> active ) ; $ this -> entityManager -> persist ( $ bundleModel ) ; $ this -> entityManager -> flush ( ) ; $ this -> entityManager -> clear ( ) ; return true ; }
{@inheritdoc}

@param TransferObjectCommand $command
{ @inheritdoc }