<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, 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\Utils\EventListener;

use Darvin\Utils\Sluggable\SluggableManagerInterface;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;

/**
 * Sluggable event subscriber
 */
class SluggableSubscriber extends AbstractOnFlushListener implements EventSubscriber
{
    /**
     * @var \Darvin\Utils\Sluggable\SluggableManagerInterface
     */
    private $sluggableManager;

    /**
     * @var array
     */
    private $entityBlacklist;

    /**
     * @param \Darvin\Utils\Sluggable\SluggableManagerInterface $sluggableManager Sluggable manager
     */
    public function __construct(SluggableManagerInterface $sluggableManager)
    {
        $this->sluggableManager = $sluggableManager;

        $this->entityBlacklist = [];
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return [
            Events::onFlush,
        ];
    }

    /**
     * @param object $entity Entity
     */
    public function blacklistEntity($entity)
    {
        $hash = spl_object_hash($entity);
        $this->entityBlacklist[$hash] = $hash;
    }

    /**
     * {@inheritdoc}
     */
    public function onFlush(OnFlushEventArgs $args)
    {
        parent::onFlush($args);

        $generateSlugsCallback = [$this, 'generateSlugs'];

        $this
            ->onInsert($generateSlugsCallback)
            ->onUpdate($generateSlugsCallback);
    }

    /**
     * @param object $entity    Entity
     * @param string $operation Operation type
     */
    protected function generateSlugs($entity, $operation)
    {
        if (isset($this->entityBlacklist[spl_object_hash($entity)]) || !$this->sluggableManager->isSluggable($entity)) {
            return;
        }
        if ($this->sluggableManager->generateSlugs($entity, AbstractOnFlushListener::OPERATION_UPDATE === $operation)) {
            $this->recomputeChangeSet($entity);
        }
    }
}
<commit_msg>Group blacklisted entities by class in the sluggable event subscriber.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, 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\Utils\EventListener;

use Darvin\Utils\Sluggable\SluggableManagerInterface;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;

/**
 * Sluggable event subscriber
 */
class SluggableSubscriber extends AbstractOnFlushListener implements EventSubscriber
{
    /**
     * @var \Darvin\Utils\Sluggable\SluggableManagerInterface
     */
    private $sluggableManager;

    /**
     * @var array
     */
    private $entityBlacklist;

    /**
     * @param \Darvin\Utils\Sluggable\SluggableManagerInterface $sluggableManager Sluggable manager
     */
    public function __construct(SluggableManagerInterface $sluggableManager)
    {
        $this->sluggableManager = $sluggableManager;

        $this->entityBlacklist = [];
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return [
            Events::onFlush,
        ];
    }

    /**
     * @param object $entity Entity
     */
    public function blacklistEntity($entity)
    {
        $class = ClassUtils::getClass($entity);
        $hash  = spl_object_hash($entity);

        if (!isset($this->entityBlacklist[$class])) {
            $this->entityBlacklist[$class] = [];
        }

        $this->entityBlacklist[$class][$hash] = $hash;
    }

    /**
     * {@inheritdoc}
     */
    public function onFlush(OnFlushEventArgs $args)
    {
        parent::onFlush($args);

        $generateSlugsCallback = [$this, 'generateSlugs'];

        $this
            ->onInsert($generateSlugsCallback)
            ->onUpdate($generateSlugsCallback);
    }

    /**
     * @param object $entity    Entity
     * @param string $operation Operation type
     */
    protected function generateSlugs($entity, $operation)
    {
        if (isset($this->entityBlacklist[ClassUtils::getClass($entity)][spl_object_hash($entity)])
            || !$this->sluggableManager->isSluggable($entity)
        ) {
            return;
        }
        if ($this->sluggableManager->generateSlugs($entity, AbstractOnFlushListener::OPERATION_UPDATE === $operation)) {
            $this->recomputeChangeSet($entity);
        }
    }
}

{'fraction_non_alphanumeric': 0.12234706616729088, 'fraction_numerical': 0.0016645859342488557, 'mean_word_length': 2.7205882352941178, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6871428', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1374, 'n_words': 258}