<commit_before><?php

namespace PlentymarketsAdapter\ServiceBus\QueryHandler\Category;

use PlentyConnector\Connector\ServiceBus\Query\Category\FetchChangedCategoriesQuery;
use PlentyConnector\Connector\ServiceBus\Query\QueryInterface;
use PlentyConnector\Connector\ServiceBus\QueryHandler\QueryHandlerInterface;
use PlentymarketsAdapter\Client\ClientInterface;
use PlentymarketsAdapter\Helper\LanguageHelper;
use PlentymarketsAdapter\PlentymarketsAdapter;
use PlentymarketsAdapter\ResponseParser\Category\CategoryResponseParserInterface;
use PlentymarketsAdapter\ServiceBus\ChangedDateTimeTrait;

/**
 * Class FetchChangedCategoriesQueryHandler.
 */
class FetchChangedCategoriesQueryHandler implements QueryHandlerInterface
{
    use ChangedDateTimeTrait;

    /**
     * @var ClientInterface
     */
    private $client;

    /**
     * @var CategoryResponseParserInterface
     */
    private $categoryResponseParser;

    /**
     * @var LanguageHelper
     */
    private $languageHelper;

    /**
     * FetchCategoryQueryHandler constructor.
     *
     * @param ClientInterface $client
     * @param CategoryResponseParserInterface $categoryResponseParser
     * @param LanguageHelper $languageHelper
     */
    public function __construct(
        ClientInterface $client,
        CategoryResponseParserInterface $categoryResponseParser,
        LanguageHelper $languageHelper
    ) {
        $this->client = $client;
        $this->categoryResponseParser = $categoryResponseParser;
        $this->languageHelper = $languageHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function supports(QueryInterface $query)
    {
        return $query instanceof FetchChangedCategoriesQuery &&
            $query->getAdapterName() === PlentymarketsAdapter::NAME;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(QueryInterface $query)
    {
        $elements = $this->client->request('GET', 'categories', [
            'with' => 'details',
            'lang' => $this->languageHelper->getLanguagesQueryString(),
        ]);

        $elements = array_filter($elements, function ($element) {
            return $element['type'] === 'item' && $element['right'] === 'all';
        });

        $result = [];

        array_walk($elements, function (array $element) use (&$result) {
            if (empty($element['details'])) {
                return;
            }

            $categoriesGrouped = [];
            foreach ($element['details'] as $detail) {
                $categoriesGrouped[$detail['plentyId']][] = $detail;
            }

            foreach ($categoriesGrouped as $plentyId => $details) {
                $parsedElements = $this->categoryResponseParser->parse([
                    'plentyId' => $plentyId,
                    'categoryId' => $element['id'],
                    'parentCategoryId' => $element['parentCategoryId'],
                    'details' => $details,
                ]);

                foreach ($parsedElements as $parsedElement) {
                    $result[] = $parsedElement;
                }
            }
        });

        return array_filter($result);
    }
}<commit_msg>updated the changed category query handler<commit_after><?php

namespace PlentymarketsAdapter\ServiceBus\QueryHandler\Category;

use PlentyConnector\Connector\ServiceBus\Query\Category\FetchChangedCategoriesQuery;
use PlentyConnector\Connector\ServiceBus\Query\QueryInterface;
use PlentyConnector\Connector\ServiceBus\QueryHandler\QueryHandlerInterface;
use PlentymarketsAdapter\Client\ClientInterface;
use PlentymarketsAdapter\Helper\LanguageHelper;
use PlentymarketsAdapter\PlentymarketsAdapter;
use PlentymarketsAdapter\ResponseParser\Category\CategoryResponseParserInterface;
use PlentymarketsAdapter\ServiceBus\ChangedDateTimeTrait;

/**
 * Class FetchChangedCategoriesQueryHandler.
 */
class FetchChangedCategoriesQueryHandler implements QueryHandlerInterface
{
    use ChangedDateTimeTrait;

    /**
     * @var ClientInterface
     */
    private $client;

    /**
     * @var CategoryResponseParserInterface
     */
    private $categoryResponseParser;

    /**
     * @var LanguageHelper
     */
    private $languageHelper;

    /**
     * FetchCategoryQueryHandler constructor.
     *
     * @param ClientInterface $client
     * @param CategoryResponseParserInterface $categoryResponseParser
     * @param LanguageHelper $languageHelper
     */
    public function __construct(
        ClientInterface $client,
        CategoryResponseParserInterface $categoryResponseParser,
        LanguageHelper $languageHelper
    ) {
        $this->client = $client;
        $this->categoryResponseParser = $categoryResponseParser;
        $this->languageHelper = $languageHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function supports(QueryInterface $query)
    {
        return $query instanceof FetchChangedCategoriesQuery &&
            $query->getAdapterName() === PlentymarketsAdapter::NAME;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(QueryInterface $query)
    {
        $lastCangedTime = $this->getChangedDateTime();
        $currentDateTime = $this->getCurrentDateTime();

        $elements = $this->client->request('GET', 'categories', [
            'with' => 'details',
            'type' => 'item',
            'updatedAt' => $lastCangedTime->format(DATE_W3C),
            'lang' => $this->languageHelper->getLanguagesQueryString(),
        ]);

        $elements = array_filter($elements, function ($element) {
            return $element['right'] === 'all';
        });

        $result = [];

        array_walk($elements, function (array $element) use (&$result) {
            if (empty($element['details'])) {
                return;
            }

            $categoriesGrouped = [];
            foreach ($element['details'] as $detail) {
                $categoriesGrouped[$detail['plentyId']][] = $detail;
            }

            foreach ($categoriesGrouped as $plentyId => $details) {
                $parsedElements = $this->categoryResponseParser->parse([
                    'plentyId' => $plentyId,
                    'categoryId' => $element['id'],
                    'parentCategoryId' => $element['parentCategoryId'],
                    'details' => $details,
                ]);

                foreach ($parsedElements as $parsedElement) {
                    $result[] = $parsedElement;
                }
            }
        });

        if (!empty($result)) {
            $this->setChangedDateTime($currentDateTime);
        }

        return array_filter($result);
    }
}
{'fraction_non_alphanumeric': 0.1150188679245283, 'fraction_numerical': 0.0001509433962264151, 'mean_word_length': 2.56428187197418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992837', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1662, 'n_words': 288}