Adapter/ShopwareAdapter/ServiceBus/CommandHandler/Category/HandleCategoryCommandHandler.php
private function handleCategory(Category $category, Identity $shopIdentity)
    {
        $deepCopy = new DeepCopy();
        $category = $deepCopy->copy($category);

        /**
         * @var null|ShopModel $shop
         */
        $shop = $this->shopRepository->find($shopIdentity->getAdapterIdentifier());

        if (null === $shop) {
            return null;
        }

        $this->prepareCategory($category);

        $mainCategory = $shop->getCategory();

        if (null === $mainCategory) {
            throw new InvalidArgumentException('shop without main cateogry assignment');
        }

        $shopLocale = $shop->getLocale();

        if (null === $shopLocale) {
            throw new InvalidArgumentException('shop without locale assignment');
        }

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

        if (null !== $languageIdentity) {
            /**
             * @var Category $translatedCategory
             */
            $translatedCategory = $this->translationHelper->translate($languageIdentity->getObjectIdentifier(), $category);
            $translatedAttributes = [];

            if (null !== $translatedCategory) {
                foreach ($translatedCategory->getAttributes() as $attribute) {
                    $translatedAttributes[] = $this->translationHelper->translate($languageIdentity->getObjectIdentifier(), $attribute);
                }
                $translatedCategory->setAttributes($translatedAttributes);

                $category = $translatedCategory;
            }
        }

        if (null === $category->getParentIdentifier()) {
            $parentCategory = $mainCategory->getId();
        } else {
            $parentCategoryIdentities = $this->identityService->findBy([
                'objectIdentifier' => (string) $category->getParentIdentifier(),
                'objectType' => Category::TYPE,
                'adapterName' => ShopwareAdapter::NAME,
            ]);

            $possibleIdentities = array_filter($parentCategoryIdentities, function (Identity $identity) use ($mainCategory) {
                return $this->validIdentity($identity, $mainCategory);
            });

            $parentCategoryIdentity = null;
            if (!empty($possibleIdentities)) {
                /**
                 * @var Identity $categoryIdentity
                 */
                $parentCategoryIdentity = array_shift($possibleIdentities);
            }

            if (null === $parentCategoryIdentity) {
                throw new InvalidArgumentException('missing parent category - ' . $category->getParentIdentifier());
            }

            $parentCategory = $parentCategoryIdentity->getAdapterIdentifier();
        }

        $categoryIdentities = $this->identityService->findBy([
            'objectIdentifier' => $category->getIdentifier(),
            'objectType' => Category::TYPE,
            'adapterName' => ShopwareAdapter::NAME,
        ]);

        $possibleIdentities = array_filter($categoryIdentities, function (Identity $identity) use ($mainCategory) {
            return $this->validIdentity($identity, $mainCategory);
        });

        $categoryIdentity = null;
        if (!empty($possibleIdentities)) {
            /**
             * @var Identity $categoryIdentity
             */
            $categoryIdentity = array_shift($possibleIdentities);
        }

        $resource = $this->getCategoryResource();

        if (null !== $categoryIdentity) {
            try {
                $resource->getOne($categoryIdentity->getAdapterIdentifier());
            } catch (NotFoundException $exception) {
                $categoryIdentity = null;
            }
        }

        if (null === $categoryIdentity) {
            $existingCategory = $this->findExistingCategory($category, $parentCategory);

            if (null !== $existingCategory) {
                $categoryIdentity = $this->identityService->insert(
                    (string) $category->getIdentifier(),
                    Category::TYPE,
                    (string) $existingCategory,
                    ShopwareAdapter::NAME
                );
            }
        }

        $params = [
            'active' => $category->getActive(),
            'position' => $category->getPosition(),
            'name' => $category->getName(),
            'parent' => $parentCategory,
            'metaTitle' => $category->getMetaTitle(),
            'metaKeywords' => $category->getMetaKeywords(),
            'metaDescription' => $category->getMetaDescription(),
            'cmsHeadline' => $category->getDescription(),
            'cmsText' => $category->getLongDescription(),
        ];

        if (!empty($category->getImageIdentifiers())) {
            $mediaIdentifiers = $category->getImageIdentifiers();
            $mediaIdentifier = array_shift($mediaIdentifiers);

            $mediaIdentity = $this->identityService->findOneBy([
                'objectIdentifier' => (string) $mediaIdentifier,
                'objectType' => Media::TYPE,
                'adapterName' => ShopwareAdapter::NAME,
            ]);

            if (null === $mediaIdentity) {
                throw new IdentityNotFoundException('media not found - ' . $mediaIdentifier);
            }

            $params['media']['mediaId'] = $mediaIdentity->getAdapterIdentifier();
        }

        if (null !== $categoryIdentity) {
            try {
                $resource->getOne($categoryIdentity->getAdapterIdentifier());
            } catch (NotFoundException $exception) {
                $this->identityService->remove($categoryIdentity);

                $categoryIdentity = null;
            }
        }

        if (null === $categoryIdentity) {
            $categoryModel = $resource->create($params);

            $categoryIdentity = $this->identityService->insert(
                (string) $category->getIdentifier(),
                Category::TYPE,
                (string) $categoryModel->getId(),
                ShopwareAdapter::NAME
            );
        } else {
            $categoryModel = $resource->update($categoryIdentity->getAdapterIdentifier(), $params);
        }

        $this->attributePersister->saveCategoryAttributes($categoryModel, $category->getAttributes());
        $this->translationDataPersister->writeCategoryTranslations($category);

        return $categoryIdentity;
    }
private function handleCategory(Category $category, Identity $shopIdentity)
    {
        $deepCopy = new DeepCopy();
        $category = $deepCopy->copy($category);

        /**
         * @var null|ShopModel $shop
         */
        $shop = $this->shopRepository->find($shopIdentity->getAdapterIdentifier());

        if (null === $shop) {
            return null;
        }

        $this->prepareCategory($category);

        $mainCategory = $shop->getCategory();

        if (null === $mainCategory) {
            throw new InvalidArgumentException('shop without main cateogry assignment');
        }

        $shopLocale = $shop->getLocale();

        if (null === $shopLocale) {
            throw new InvalidArgumentException('shop without locale assignment');
        }

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

        if (null !== $languageIdentity) {
            /**
             * @var Category $translatedCategory
             */
            $translatedCategory = $this->translationHelper->translate($languageIdentity->getObjectIdentifier(), $category);
            $translatedAttributes = [];

            if (null !== $translatedCategory) {
                foreach ($translatedCategory->getAttributes() as $attribute) {
                    $translatedAttributes[] = $this->translationHelper->translate($languageIdentity->getObjectIdentifier(), $attribute);
                }
                $translatedCategory->setAttributes($translatedAttributes);

                $category = $translatedCategory;
            }
        }

        if (null === $category->getParentIdentifier()) {
            $parentCategory = $mainCategory->getId();
        } else {
            $parentCategoryIdentities = $this->identityService->findBy([
                'objectIdentifier' => (string) $category->getParentIdentifier(),
                'objectType' => Category::TYPE,
                'adapterName' => ShopwareAdapter::NAME,
            ]);

            $possibleIdentities = array_filter($parentCategoryIdentities, function (Identity $identity) use ($mainCategory) {
                return $this->validIdentity($identity, $mainCategory);
            });

            $parentCategoryIdentity = null;
            if (!empty($possibleIdentities)) {
                /**
                 * @var Identity $categoryIdentity
                 */
                $parentCategoryIdentity = array_shift($possibleIdentities);
            }

            if (null === $parentCategoryIdentity) {
                throw new InvalidArgumentException('missing parent category - ' . $category->getParentIdentifier());
            }

            $parentCategory = $parentCategoryIdentity->getAdapterIdentifier();
        }

        $categoryIdentities = $this->identityService->findBy([
            'objectIdentifier' => $category->getIdentifier(),
            'objectType' => Category::TYPE,
            'adapterName' => ShopwareAdapter::NAME,
        ]);

        $possibleIdentities = array_filter($categoryIdentities, function (Identity $identity) use ($mainCategory) {
            return $this->validIdentity($identity, $mainCategory);
        });

        $categoryIdentity = null;
        if (!empty($possibleIdentities)) {
            /**
             * @var Identity $categoryIdentity
             */
            $categoryIdentity = array_shift($possibleIdentities);
        }

        $resource = $this->getCategoryResource();

        if (null !== $categoryIdentity) {
            try {
                $resource->getOne($categoryIdentity->getAdapterIdentifier());
            } catch (NotFoundException $exception) {
                $categoryIdentity = null;
            }
        }

        if (null === $categoryIdentity) {
            $existingCategory = $this->findExistingCategory($category, $parentCategory);

            if (null !== $existingCategory) {
                $categoryIdentity = $this->identityService->insert(
                    (string) $category->getIdentifier(),
                    Category::TYPE,
                    (string) $existingCategory,
                    ShopwareAdapter::NAME
                );
            }
        }

        $params = [
            'active' => $category->getActive(),
            'position' => $category->getPosition(),
            'name' => $category->getName(),
            'parent' => $parentCategory,
            'metaTitle' => $category->getMetaTitle(),
            'metaKeywords' => $category->getMetaKeywords(),
            'metaDescription' => $category->getMetaDescription(),
            'cmsHeadline' => $category->getDescription(),
            'cmsText' => $category->getLongDescription(),
        ];

        if (!empty($category->getImageIdentifiers())) {
            $mediaIdentifiers = $category->getImageIdentifiers();
            $mediaIdentifier = array_shift($mediaIdentifiers);

            $mediaIdentity = $this->identityService->findOneBy([
                'objectIdentifier' => (string) $mediaIdentifier,
                'objectType' => Media::TYPE,
                'adapterName' => ShopwareAdapter::NAME,
            ]);

            if (null === $mediaIdentity) {
                throw new IdentityNotFoundException('media not found - ' . $mediaIdentifier);
            }

            $params['media']['mediaId'] = $mediaIdentity->getAdapterIdentifier();
        }

        if (null !== $categoryIdentity) {
            try {
                $resource->getOne($categoryIdentity->getAdapterIdentifier());
            } catch (NotFoundException $exception) {
                $this->identityService->remove($categoryIdentity);

                $categoryIdentity = null;
            }
        }

        if (null === $categoryIdentity) {
            $categoryModel = $resource->create($params);

            $categoryIdentity = $this->identityService->insert(
                (string) $category->getIdentifier(),
                Category::TYPE,
                (string) $categoryModel->getId(),
                ShopwareAdapter::NAME
            );
        } else {
            $categoryModel = $resource->update($categoryIdentity->getAdapterIdentifier(), $params);
        }

        $this->attributePersister->saveCategoryAttributes($categoryModel, $category->getAttributes());
        $this->translationDataPersister->writeCategoryTranslations($category);

        return $categoryIdentity;
    }
private function handleCategory ( Category $ category , Identity $ shopIdentity ) { $ deepCopy = new DeepCopy ( ) ; $ category = $ deepCopy -> copy ( $ category ) ; /**
         * @var null|ShopModel $shop
         */ $ shop = $ this -> shopRepository -> find ( $ shopIdentity -> getAdapterIdentifier ( ) ) ; if ( null === $ shop ) { return null ; } $ this -> prepareCategory ( $ category ) ; $ mainCategory = $ shop -> getCategory ( ) ; if ( null === $ mainCategory ) { throw new InvalidArgumentException ( 'shop without main cateogry assignment' ) ; } $ shopLocale = $ shop -> getLocale ( ) ; if ( null === $ shopLocale ) { throw new InvalidArgumentException ( 'shop without locale assignment' ) ; } $ languageIdentity = $ this -> identityService -> findOneBy ( [ 'adapterIdentifier' => ( string ) $ shopLocale -> getId ( ) , 'adapterName' => ShopwareAdapter :: NAME , 'objectType' => Language :: TYPE , ] ) ; if ( null !== $ languageIdentity ) { /**
             * @var Category $translatedCategory
             */ $ translatedCategory = $ this -> translationHelper -> translate ( $ languageIdentity -> getObjectIdentifier ( ) , $ category ) ; $ translatedAttributes = [ ] ; if ( null !== $ translatedCategory ) { foreach ( $ translatedCategory -> getAttributes ( ) as $ attribute ) { $ translatedAttributes [ ] = $ this -> translationHelper -> translate ( $ languageIdentity -> getObjectIdentifier ( ) , $ attribute ) ; } $ translatedCategory -> setAttributes ( $ translatedAttributes ) ; $ category = $ translatedCategory ; } } if ( null === $ category -> getParentIdentifier ( ) ) { $ parentCategory = $ mainCategory -> getId ( ) ; } else { $ parentCategoryIdentities = $ this -> identityService -> findBy ( [ 'objectIdentifier' => ( string ) $ category -> getParentIdentifier ( ) , 'objectType' => Category :: TYPE , 'adapterName' => ShopwareAdapter :: NAME , ] ) ; $ possibleIdentities = array_filter ( $ parentCategoryIdentities , function ( Identity $ identity ) use ( $ mainCategory ) { return $ this -> validIdentity ( $ identity , $ mainCategory ) ; } ) ; $ parentCategoryIdentity = null ; if ( ! empty ( $ possibleIdentities ) ) { /**
                 * @var Identity $categoryIdentity
                 */ $ parentCategoryIdentity = array_shift ( $ possibleIdentities ) ; } if ( null === $ parentCategoryIdentity ) { throw new InvalidArgumentException ( 'missing parent category - ' . $ category -> getParentIdentifier ( ) ) ; } $ parentCategory = $ parentCategoryIdentity -> getAdapterIdentifier ( ) ; } $ categoryIdentities = $ this -> identityService -> findBy ( [ 'objectIdentifier' => $ category -> getIdentifier ( ) , 'objectType' => Category :: TYPE , 'adapterName' => ShopwareAdapter :: NAME , ] ) ; $ possibleIdentities = array_filter ( $ categoryIdentities , function ( Identity $ identity ) use ( $ mainCategory ) { return $ this -> validIdentity ( $ identity , $ mainCategory ) ; } ) ; $ categoryIdentity = null ; if ( ! empty ( $ possibleIdentities ) ) { /**
             * @var Identity $categoryIdentity
             */ $ categoryIdentity = array_shift ( $ possibleIdentities ) ; } $ resource = $ this -> getCategoryResource ( ) ; if ( null !== $ categoryIdentity ) { try { $ resource -> getOne ( $ categoryIdentity -> getAdapterIdentifier ( ) ) ; } catch ( NotFoundException $ exception ) { $ categoryIdentity = null ; } } if ( null === $ categoryIdentity ) { $ existingCategory = $ this -> findExistingCategory ( $ category , $ parentCategory ) ; if ( null !== $ existingCategory ) { $ categoryIdentity = $ this -> identityService -> insert ( ( string ) $ category -> getIdentifier ( ) , Category :: TYPE , ( string ) $ existingCategory , ShopwareAdapter :: NAME ) ; } } $ params = [ 'active' => $ category -> getActive ( ) , 'position' => $ category -> getPosition ( ) , 'name' => $ category -> getName ( ) , 'parent' => $ parentCategory , 'metaTitle' => $ category -> getMetaTitle ( ) , 'metaKeywords' => $ category -> getMetaKeywords ( ) , 'metaDescription' => $ category -> getMetaDescription ( ) , 'cmsHeadline' => $ category -> getDescription ( ) , 'cmsText' => $ category -> getLongDescription ( ) , ] ; if ( ! empty ( $ category -> getImageIdentifiers ( ) ) ) { $ mediaIdentifiers = $ category -> getImageIdentifiers ( ) ; $ mediaIdentifier = array_shift ( $ mediaIdentifiers ) ; $ mediaIdentity = $ this -> identityService -> findOneBy ( [ 'objectIdentifier' => ( string ) $ mediaIdentifier , 'objectType' => Media :: TYPE , 'adapterName' => ShopwareAdapter :: NAME , ] ) ; if ( null === $ mediaIdentity ) { throw new IdentityNotFoundException ( 'media not found - ' . $ mediaIdentifier ) ; } $ params [ 'media' ] [ 'mediaId' ] = $ mediaIdentity -> getAdapterIdentifier ( ) ; } if ( null !== $ categoryIdentity ) { try { $ resource -> getOne ( $ categoryIdentity -> getAdapterIdentifier ( ) ) ; } catch ( NotFoundException $ exception ) { $ this -> identityService -> remove ( $ categoryIdentity ) ; $ categoryIdentity = null ; } } if ( null === $ categoryIdentity ) { $ categoryModel = $ resource -> create ( $ params ) ; $ categoryIdentity = $ this -> identityService -> insert ( ( string ) $ category -> getIdentifier ( ) , Category :: TYPE , ( string ) $ categoryModel -> getId ( ) , ShopwareAdapter :: NAME ) ; } else { $ categoryModel = $ resource -> update ( $ categoryIdentity -> getAdapterIdentifier ( ) , $ params ) ; } $ this -> attributePersister -> saveCategoryAttributes ( $ categoryModel , $ category -> getAttributes ( ) ) ; $ this -> translationDataPersister -> writeCategoryTranslations ( $ category ) ; return $ categoryIdentity ; }
@param Category $category
@param Identity $shopIdentity

@return null|Identity
@param Category $category @param Identity $shopIdentity