<commit_before><?php

namespace SlmQueue\Strategy;

use SlmQueue\Exception\RunTimeException;
use SlmQueue\Worker\AbstractWorker;
use SlmQueue\Worker\WorkerEvent;
use Zend\EventManager\EventManagerInterface;

class AttachQueueListenersStrategy extends AbstractStrategy
{
    /**
     * @var StrategyPluginManager
     */
    protected $pluginManager;

    /**
     * @var array
     */
    protected $strategyConfig;

    public function __construct(StrategyPluginManager $pluginManager, array $strategyConfig)
    {
        $this->pluginManager  = $pluginManager;
        $this->strategyConfig = $strategyConfig;
    }

    /**
     * {@inheritDoc}
     */
    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_BOOTSTRAP,
            array($this, 'attachQueueListeners'),
            PHP_INT_MAX
        );
    }

    /**
     * @param WorkerEvent $e
     * @throws \SlmQueue\Exception\RunTimeException
     */
    public function attachQueueListeners(WorkerEvent $e)
    {
        /** @var AbstractWorker $worker */
        $worker = $e->getTarget();
        $name = $e->getQueue()->getName();
        $eventManager = $worker->getEventManager();

        $eventManager->detachAggregate($this);

        if (!array_key_exists($name, $this->strategyConfig)) {
            return;
        }

        $strategies = $this->strategyConfig[$name];

        foreach ($strategies as $strategy => $options) {
            if (is_numeric($strategy) && is_string($options)) { // no options given, name stored as value
                $strategy = $options;
                $options = array();
            }

            if (!is_string($strategy) || !is_array($options)) {
                continue;
            }

            $priority = null;
            if (isset($options['priority'])) {
                $priority = $options['priority'];
                unset($options['priority']);
            }

            $listener = $this->pluginManager->get($strategy, $options);

            if (!is_null($priority)) {
                $eventManager->attachAggregate($listener, $priority);
            } else {
                $eventManager->attachAggregate($listener);
            }
        }<commit_msg>islet vs array_key_exists<commit_after><?php

namespace SlmQueue\Strategy;

use SlmQueue\Exception\RunTimeException;
use SlmQueue\Worker\AbstractWorker;
use SlmQueue\Worker\WorkerEvent;
use Zend\EventManager\EventManagerInterface;

class AttachQueueListenersStrategy extends AbstractStrategy
{
    /**
     * @var StrategyPluginManager
     */
    protected $pluginManager;

    /**
     * @var array
     */
    protected $strategyConfig;

    public function __construct(StrategyPluginManager $pluginManager, array $strategyConfig)
    {
        $this->pluginManager  = $pluginManager;
        $this->strategyConfig = $strategyConfig;
    }

    /**
     * {@inheritDoc}
     */
    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_BOOTSTRAP,
            array($this, 'attachQueueListeners'),
            PHP_INT_MAX
        );
    }

    /**
     * @param WorkerEvent $e
     * @throws \SlmQueue\Exception\RunTimeException
     */
    public function attachQueueListeners(WorkerEvent $e)
    {
        /** @var AbstractWorker $worker */
        $worker = $e->getTarget();
        $name = $e->getQueue()->getName();
        $eventManager = $worker->getEventManager();

        $eventManager->detachAggregate($this);

        if (!isset($this->strategyConfig[$name])) {
            return;
        }

        $strategies = $this->strategyConfig[$name];

        foreach ($strategies as $strategy => $options) {
            if (is_numeric($strategy) && is_string($options)) { // no options given, name stored as value
                $strategy = $options;
                $options = array();
            }

            if (!is_string($strategy) || !is_array($options)) {
                continue;
            }

            $priority = null;
            if (isset($options['priority'])) {
                $priority = $options['priority'];
                unset($options['priority']);
            }

            $listener = $this->pluginManager->get($strategy, $options);

            if (!is_null($priority)) {
                $eventManager->attachAggregate($listener, $priority);
            } else {
                $eventManager->attachAggregate($listener);
            }
        }
{'fraction_non_alphanumeric': 0.12832929782082325, 'fraction_numerical': 0.0, 'mean_word_length': 2.1555555555555554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '932203', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1209, 'n_words': 217}