<commit_before><?php

namespace DoS\QueueBundle\Provider;

use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\EntityManager;
use DoS\QueueBundle\Model\QueueMessageInterface;
use DoS\ResourceBundle\Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Uecode\Bundle\QPushBundle\Event\Events;
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
use Uecode\Bundle\QPushBundle\Message\Message;
use Uecode\Bundle\QPushBundle\Provider\AbstractProvider;

class DoctrineProvider extends AbstractProvider
{
    /**
     * @var array
     */
    protected $queues = array();

    /**
     * @var EntityManager
     */
    protected $dispatcher;

    /**
     * @var EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * @var EntityRepository
     */
    protected $repisotory;

    /**
     * @var string
     */
    protected $dataClass;

    /**
     * @var array
     */
    protected $queueBuffers = [];

    /**
     * @var array
     */
    protected $queueOptions = array();

    public function __construct($name, array $options, $client, Cache $cache, Logger $logger)
    {
        $options = array_merge(array(
            'messages_to_receive' => 1,
            'fifo_receive' => true,
            'logging_enabled' => true,
        ), $options);

        $this->name = $name;
        $this->options = $options;
        $this->dispatcher = $client;
        $this->cache = $cache;
        $this->logger = $logger;
    }

    /**
     * @param string $dataClass
     */
    public function setRepositoryForClass($dataClass)
    {
        $this->dataClass = $dataClass;
        $this->repisotory = $this->dispatcher->getRepository($this->dataClass);
    }

    /**
     * @return QueueMessageInterface
     */
    public function create()
    {
        $this->log(200, "Queue has been created.");
        return $this->repisotory->createNew();
    }

    /**
     * @param EventDispatcherInterface $eventDispatcher
     */
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    /**
     * {@inheritdoc}
     */
    public function getProvider()
    {
        return 'DoctrineORM';
    }

    /**
     * {@inheritdoc}
     */
    public function publish(array $message, array $options = [])
    {
        $publishStart = microtime(true);
        $queue = $this->create();
        $queue->setBody($message);
        $queue->setName($this->getNameWithPrefix());
        $this->dispatcher->persist($queue);
        $this->dispatcher->flush($queue);

        $this->queueBuffers[$queue->getId()] = $queue;

        $context = array(
            'message_id' => $queue->getId(),
            'publish_time' => microtime(true) - $publishStart
        );

        $this->log(200, "Message has been published.", $context);
    }

    /**
     * {@inheritdoc}
     */
    public function receive(array $options = [])
    {
        $this->options = array_merge($this->options, $options);
        $results = $this->repisotory->createQueryBuilder('o')
            ->orderBy('o.id', $this->options['fifo_receive'] ? 'ASC' : 'DESC')
            ->setMaxResults($this->options['messages_to_receive'])
            ->getQuery()->getResult()
        ;

        if (!count($results)) {
            $this->log(200, "No messages found in queue.");

            return array();
        }

        $messages = array();

        /** @var QueueMessageInterface $message */
        foreach($results as $message)
        {
            $message->setReceivedAt(new \DateTime());

            $messages[] = new Message($message->getId(), $message->getBody(), array());

            $this->log(200, "Message has been received.", ['message_id' => $message->getId()]);

            $this->eventDispatcher->dispatch(
                Events::Message($this->name),
                new MessageEvent($this->name, new Message($message->getId(), $message->getBody(), array()))
            );

            // recived then delete.
            $message->setDeletedAt(new \DateTime());
            $this->dispatcher->persist($message);
        }<commit_msg>bug fix missing queue name.<commit_after><?php

namespace DoS\QueueBundle\Provider;

use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\EntityManager;
use DoS\QueueBundle\Model\QueueMessageInterface;
use DoS\ResourceBundle\Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Uecode\Bundle\QPushBundle\Event\Events;
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
use Uecode\Bundle\QPushBundle\Message\Message;
use Uecode\Bundle\QPushBundle\Provider\AbstractProvider;

class DoctrineProvider extends AbstractProvider
{
    /**
     * @var array
     */
    protected $queues = array();

    /**
     * @var EntityManager
     */
    protected $dispatcher;

    /**
     * @var EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * @var EntityRepository
     */
    protected $repisotory;

    /**
     * @var string
     */
    protected $dataClass;

    /**
     * @var array
     */
    protected $queueBuffers = [];

    /**
     * @var array
     */
    protected $queueOptions = array();

    public function __construct($name, array $options, $client, Cache $cache, Logger $logger)
    {
        $options = array_merge(array(
            'messages_to_receive' => 1,
            'fifo_receive' => true,
            'logging_enabled' => true,
        ), $options);

        $this->name = $name;
        $this->options = $options;
        $this->dispatcher = $client;
        $this->cache = $cache;
        $this->logger = $logger;
    }

    /**
     * @param string $dataClass
     */
    public function setRepositoryForClass($dataClass)
    {
        $this->dataClass = $dataClass;
        $this->repisotory = $this->dispatcher->getRepository($this->dataClass);
    }

    /**
     * @return QueueMessageInterface
     */
    public function create()
    {
        $this->log(200, "Queue has been created.");
        return $this->repisotory->createNew();
    }

    /**
     * @param EventDispatcherInterface $eventDispatcher
     */
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    /**
     * {@inheritdoc}
     */
    public function getProvider()
    {
        return 'DoctrineORM';
    }

    /**
     * {@inheritdoc}
     */
    public function publish(array $message, array $options = [])
    {
        $publishStart = microtime(true);
        $queue = $this->create();
        $queue->setBody($message);
        $queue->setName($this->getNameWithPrefix());
        $this->dispatcher->persist($queue);
        $this->dispatcher->flush($queue);

        $this->queueBuffers[$queue->getId()] = $queue;

        $context = array(
            'message_id' => $queue->getId(),
            'publish_time' => microtime(true) - $publishStart
        );

        $this->log(200, "Message has been published.", $context);
    }

    /**
     * {@inheritdoc}
     */
    public function receive(array $options = [])
    {
        $this->options = array_merge($this->options, $options);
        $results = $this->repisotory->createQueryBuilder('o')
            ->orderBy('o.id', $this->options['fifo_receive'] ? 'ASC' : 'DESC')
            ->where('o.name = :name')->setParameter('name', $this->getNameWithPrefix())
            ->setMaxResults($this->options['messages_to_receive'])
            ->getQuery()->getResult()
        ;

        if (!count($results)) {
            $this->log(200, "No messages found in queue.");

            return array();
        }

        $messages = array();

        /** @var QueueMessageInterface $message */
        foreach($results as $message)
        {
            $message->setReceivedAt(new \DateTime());

            $messages[] = new Message($message->getId(), $message->getBody(), array());

            $this->log(200, "Message has been received.", ['message_id' => $message->getId()]);

            $this->eventDispatcher->dispatch(
                Events::Message($this->name),
                new MessageEvent($this->name, new Message($message->getId(), $message->getBody(), array()))
            );

            // recived then delete.
            $message->setDeletedAt(new \DateTime());
            $this->dispatcher->persist($message);
        }
{'fraction_non_alphanumeric': 0.1519135947405494, 'fraction_numerical': 0.0030523597088518433, 'mean_word_length': 2.3251366120218577, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 120, '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': '1050690', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2379, 'n_words': 437}