<commit_before><?php

namespace Bernard;

use Bernard\Middleware\MiddlewareBuilder;

declare(ticks=1);

/**
 * @package Consumer
 */
class Consumer implements Middleware
{
    protected $router;
    protected $middleware;
    protected $shutdown = false;
    protected $configured = false;
    protected $options = array(
        'max-runtime' => PHP_INT_MAX,
        'max-messages' => null,
    );

    /**
     * @param Router   $router
     * @param MiddlewareBuilder $middleware
     */
    public function __construct(Router $router, MiddlewareBuilder $middleware)
    {
        $this->router = $router;
        $this->middleware = $middleware;
    }

    /**
     * Starts an infinite loop calling Consumer::tick();
     *
     * @param Queue $queue
     * @param array $options
     */
    public function consume(Queue $queue, array $options = array())
    {
        $this->bind();

        while ($this->tick($queue, $options)) {
            // NO op
        }
    }

    /**
     * Returns true do indicate it should be run again or false to indicate
     * it should not be run again.
     *
     * @param  Queue   $queue
     * @param  array   $options
     * @return boolean
     */
    public function tick(Queue $queue, array $options = array())
    {
        $this->configure($options);

        if ($this->shutdown) {
            return false;
        }

        if (microtime(true) > $this->options['max-runtime']) {
            return false;
        }

        if (!$envelope = $queue->dequeue()) {
            return true;
        }


        $this->invoke($envelope, $queue);

        if (null != $this->options['max-messages']) {
            $this->options['max-messages'] -= 1;
        }

        return (boolean) $this->options['max-messages']--;
    }

    /**
     * Mark Consumer as shutdown
     */
    public function shutdown()
    {
        $this->shutdown = true;
    }

    /**
     * Until there is a real extension point to doing invoked stuff, this can be used
     * by wrapping the invoke method.
     *
     * @param Envelope $envelope
     * @param Queue    $queue
     */
    public function invoke(Envelope $envelope, Queue $queue)
    {<commit_msg>should not remove 2 from max-messages<commit_after><?php

namespace Bernard;

use Bernard\Middleware\MiddlewareBuilder;

declare(ticks=1);

/**
 * @package Consumer
 */
class Consumer implements Middleware
{
    protected $router;
    protected $middleware;
    protected $shutdown = false;
    protected $configured = false;
    protected $options = array(
        'max-runtime' => PHP_INT_MAX,
        'max-messages' => null,
    );

    /**
     * @param Router   $router
     * @param MiddlewareBuilder $middleware
     */
    public function __construct(Router $router, MiddlewareBuilder $middleware)
    {
        $this->router = $router;
        $this->middleware = $middleware;
    }

    /**
     * Starts an infinite loop calling Consumer::tick();
     *
     * @param Queue $queue
     * @param array $options
     */
    public function consume(Queue $queue, array $options = array())
    {
        $this->bind();

        while ($this->tick($queue, $options)) {
            // NO op
        }
    }

    /**
     * Returns true do indicate it should be run again or false to indicate
     * it should not be run again.
     *
     * @param  Queue   $queue
     * @param  array   $options
     * @return boolean
     */
    public function tick(Queue $queue, array $options = array())
    {
        $this->configure($options);

        if ($this->shutdown) {
            return false;
        }

        if (microtime(true) > $this->options['max-runtime']) {
            return false;
        }

        if (!$envelope = $queue->dequeue()) {
            return true;
        }


        $this->invoke($envelope, $queue);

        if (null === $this->options['max-messages']) {
            return true;
        }

        return (boolean) --$this->options['max-messages'];
    }

    /**
     * Mark Consumer as shutdown
     */
    public function shutdown()
    {
        $this->shutdown = true;
    }

    /**
     * Until there is a real extension point to doing invoked stuff, this can be used
     * by wrapping the invoke method.
     *
     * @param Envelope $envelope
     * @param Queue    $queue
     */
    public function invoke(Envelope $envelope, Queue $queue)
    {
{'fraction_non_alphanumeric': 0.12639744467259867, 'fraction_numerical': 0.0009126169290440338, 'mean_word_length': 1.9843430905377808, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '2726335', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1234, 'n_words': 345}