<commit_before><?php declare(strict_types=1);
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2019, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ContentBundle\Sorting;

use Darvin\ContentBundle\Entity\Position;
use Darvin\ContentBundle\Entity\SlugMapItem;
use Darvin\ContentBundle\Repository\PositionRepository;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Sorter
 */
class Sorter implements SorterInterface
{
    /**
     * @var \Doctrine\Common\Persistence\ObjectManager
     */
    private $om;

    /**
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    private $requestStack;

    /**
     * @param \Doctrine\Common\Persistence\ObjectManager     $om           Object manager
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack Request stack
     */
    public function __construct(ObjectManager $om, RequestStack $requestStack)
    {
        $this->om = $om;
        $this->requestStack = $requestStack;
    }

    /**
     * {@inheritDoc}
     */
    public function sort(iterable $objects, ?string $tag = null, ?string $slug = null): array
    {
        $objects = $this->objectsToArray($objects);

        if (empty($objects)) {
            return [];
        }
        if (null === $slug) {
            $request = $this->requestStack->getCurrentRequest();

            if (null !== $request) {
                $params = $request->attributes->get('_route_params', []);

                if (isset($params['slug'])) {
                    $slug = $params['slug'];
                }
            }
        }

        $first = reset($objects);

        $class = ClassUtils::getClass($first);

        $meta = $this->om->getClassMetadata($class);
        $ids  = [];

        foreach ($objects as $object) {
            $ids[] = $this->getObjectId($object, $meta);
        }

        $positions = $this->getPositionRepository()->getObjectIdsForSorter($this->om->getRepository(SlugMapItem::class)->findOneBy(['slug' => $slug]), $tag, $class, $ids);
        $sorted    = [];

        foreach ($positions as $id) {
            foreach ($objects as $key => $object) {
                if ((string)$this->getObjectId($object, $meta) === $id) {
                    $sorted[$key] = $object;

                    unset($objects[$key]);
                }
            }
        }

        return array_merge($sorted, $objects);
    }

    /**
     * @param object                                             $object Object
     * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $meta   Metadata
     *
     * @return mixed
     */
    private function getObjectId($object, ClassMetadata $meta)
    {
        $ids = $meta->getIdentifierValues($object);

        return reset($ids);
    }

    /**
     * @param iterable $objects Objects
     *
     * @return array
     */
    private function objectsToArray(iterable $objects): array
    {
        $array = [];

        foreach ($objects as $key => $object) {
            $array[$key] = $object;
        }

        return $array;
    }<commit_msg>Refactor sorting.<commit_after><?php declare(strict_types=1);
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2019, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ContentBundle\Sorting;

use Darvin\ContentBundle\Entity\Position;
use Darvin\ContentBundle\Entity\SlugMapItem;
use Darvin\ContentBundle\Repository\PositionRepository;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Sorter
 */
class Sorter implements SorterInterface
{
    /**
     * @var \Doctrine\Common\Persistence\ObjectManager
     */
    private $om;

    /**
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    private $requestStack;

    /**
     * @param \Doctrine\Common\Persistence\ObjectManager     $om           Object manager
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack Request stack
     */
    public function __construct(ObjectManager $om, RequestStack $requestStack)
    {
        $this->om = $om;
        $this->requestStack = $requestStack;
    }

    /**
     * {@inheritDoc}
     */
    public function sort(iterable $objects, ?string $tag = null, ?string $slug = null): array
    {
        $objects = $this->objectsToArray($objects);

        if (empty($objects)) {
            return [];
        }
        if (null === $slug) {
            $request = $this->requestStack->getCurrentRequest();

            if (null !== $request) {
                $params = $request->attributes->get('_route_params', []);

                if (isset($params['slug'])) {
                    $slug = $params['slug'];
                }
            }
        }

        $first = reset($objects);

        $class = ClassUtils::getClass($first);

        $meta = $this->om->getClassMetadata($class);
        $ids  = [];

        foreach ($objects as $object) {
            $ids[] = $this->getObjectId($object, $meta);
        }

        $sortedIds     = $this->getPositionRepository()->getObjectIdsForSorter($this->om->getRepository(SlugMapItem::class)->findOneBy(['slug' => $slug]), $tag, $class, $ids);
        $sortedObjects = [];

        foreach ($sortedIds as $id) {
            foreach ($objects as $key => $object) {
                if ((string)$this->getObjectId($object, $meta) === $id) {
                    $sortedObjects[$key] = $object;

                    unset($objects[$key]);
                }
            }
        }

        return array_merge($sortedObjects, $objects);
    }

    /**
     * @param object                                             $object Object
     * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $meta   Metadata
     *
     * @return mixed
     */
    private function getObjectId($object, ClassMetadata $meta)
    {
        $ids = $meta->getIdentifierValues($object);

        return reset($ids);
    }

    /**
     * @param iterable $objects Objects
     *
     * @return array
     */
    private function objectsToArray(iterable $objects): array
    {
        $array = [];

        foreach ($objects as $key => $object) {
            $array[$key] = $object;
        }

        return $array;
    }
{'fraction_non_alphanumeric': 0.13870014771048744, 'fraction_numerical': 0.0014771048744460858, 'mean_word_length': 2.164018691588785, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4074066', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1866, 'n_words': 385}