Adapter/PlentymarketsAdapter/ResponseParser/Category/CategoryResponseParser.php
public function parse(array $entry)
    {
        if (empty($entry['details'])) {
            $this->logger->warning('category without details');

            return [];
        }

        if ($entry['right'] !== 'all') {
            $this->logger->warning('unsupported category rights');

            return [];
        }

        $categoryIdentity = $this->identityService->findOneOrCreate(
            (string) $entry['id'],
            PlentymarketsAdapter::NAME,
            Category::TYPE
        );

        if (null !== $entry['parentCategoryId']) {
            $parentIdentity = $this->identityService->findOneBy([
                'adapterIdentifier' => (string) $entry['parentCategoryId'],
                'adapterName' => PlentymarketsAdapter::NAME,
                'objectType' => Category::TYPE,
            ]);

            if (null === $parentIdentity) {
                $this->logger->warning('parent category was not found', ['category' => $categoryIdentity->getObjectIdentifier()]);

                return [];
            }

            $parentCategoryIdentifier = $parentIdentity->getObjectIdentifier();
        } else {
            $parentCategoryIdentifier = null;
        }

        if (empty($entry['clients'])) {
            return [];
        }

        $shopIdentifiers = [];
        foreach ($entry['clients'] as $client) {
            if (empty($client['plentyId'])) {
                continue;
            }

            $identity = $this->getShopIdentity($client['plentyId']);

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

            $shopIdentifiers[] = $identity->getObjectIdentifier();
        }

        foreach ($entry['details'] as $key => $detail) {
            $isDefaultPlentyId = $key === 0;
            $isPlentyIdEnabled = in_array($entry['details'][$key]['plentyId'], array_column($entry['clients'], 'plentyId'));
            if (!$isPlentyIdEnabled && !$isDefaultPlentyId) {
                unset($entry['details'][$key]);

                continue;
            }
        }

        $validDetails = array_values(array_filter($entry['details'], function (array $detail) {
            if (empty($detail['plentyId'])) {
                return false;
            }

            $identity = $this->getShopIdentity($detail['plentyId']);

            return !(null === $identity);
        }));

        if (empty($validDetails)) {
            $validDetails = $entry['details'];
        }

        $result = [];

        $category = new Category();
        $category->setIdentifier($categoryIdentity->getObjectIdentifier());
        $category->setParentIdentifier($parentCategoryIdentifier);
        $category->setShopIdentifiers($shopIdentifiers);
        $category->setImageIdentifiers($this->getImages($validDetails['0'], $result));
        $category->setName($validDetails['0']['name']);
        $category->setActive(true);
        $category->setPosition((int) $validDetails['0']['position']);
        $category->setDescription($validDetails['0']['shortDescription']);
        $category->setLongDescription($validDetails['0']['description']);
        $category->setMetaTitle($validDetails['0']['metaTitle']);
        $category->setMetaDescription($validDetails['0']['metaDescription']);
        $category->setMetaKeywords($validDetails['0']['metaKeywords']);
        $category->setMetaRobots($this->getMetaRobots($validDetails['0']['metaRobots']));
        $category->setTranslations($this->getTranslations($validDetails, $result));
        $category->setAttributes($this->getAttributes($validDetails));

        return array_merge($result, [$category]);
    }
public function parse(array $entry)
    {
        if (empty($entry['details'])) {
            $this->logger->warning('category without details');

            return [];
        }

        if ($entry['right'] !== 'all') {
            $this->logger->warning('unsupported category rights');

            return [];
        }

        $categoryIdentity = $this->identityService->findOneOrCreate(
            (string) $entry['id'],
            PlentymarketsAdapter::NAME,
            Category::TYPE
        );

        if (null !== $entry['parentCategoryId']) {
            $parentIdentity = $this->identityService->findOneBy([
                'adapterIdentifier' => (string) $entry['parentCategoryId'],
                'adapterName' => PlentymarketsAdapter::NAME,
                'objectType' => Category::TYPE,
            ]);

            if (null === $parentIdentity) {
                $this->logger->warning('parent category was not found', ['category' => $categoryIdentity->getObjectIdentifier()]);

                return [];
            }

            $parentCategoryIdentifier = $parentIdentity->getObjectIdentifier();
        } else {
            $parentCategoryIdentifier = null;
        }

        if (empty($entry['clients'])) {
            return [];
        }

        $shopIdentifiers = [];
        foreach ($entry['clients'] as $client) {
            if (empty($client['plentyId'])) {
                continue;
            }

            $identity = $this->getShopIdentity($client['plentyId']);

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

            $shopIdentifiers[] = $identity->getObjectIdentifier();
        }

        foreach ($entry['details'] as $key => $detail) {
            $isDefaultPlentyId = $key === 0;
            $isPlentyIdEnabled = in_array($entry['details'][$key]['plentyId'], array_column($entry['clients'], 'plentyId'));
            if (!$isPlentyIdEnabled && !$isDefaultPlentyId) {
                unset($entry['details'][$key]);

                continue;
            }
        }

        $validDetails = array_values(array_filter($entry['details'], function (array $detail) {
            if (empty($detail['plentyId'])) {
                return false;
            }

            $identity = $this->getShopIdentity($detail['plentyId']);

            return !(null === $identity);
        }));

        if (empty($validDetails)) {
            $validDetails = $entry['details'];
        }

        $result = [];

        $category = new Category();
        $category->setIdentifier($categoryIdentity->getObjectIdentifier());
        $category->setParentIdentifier($parentCategoryIdentifier);
        $category->setShopIdentifiers($shopIdentifiers);
        $category->setImageIdentifiers($this->getImages($validDetails['0'], $result));
        $category->setName($validDetails['0']['name']);
        $category->setActive(true);
        $category->setPosition((int) $validDetails['0']['position']);
        $category->setDescription($validDetails['0']['shortDescription']);
        $category->setLongDescription($validDetails['0']['description']);
        $category->setMetaTitle($validDetails['0']['metaTitle']);
        $category->setMetaDescription($validDetails['0']['metaDescription']);
        $category->setMetaKeywords($validDetails['0']['metaKeywords']);
        $category->setMetaRobots($this->getMetaRobots($validDetails['0']['metaRobots']));
        $category->setTranslations($this->getTranslations($validDetails, $result));
        $category->setAttributes($this->getAttributes($validDetails));

        return array_merge($result, [$category]);
    }
{@inheritdoc}