<commit_before><?php

/**
 * Copyright 2014 Underground Elephant
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package     qpush-bundle
 * @copyright   Underground Elephant 2014
 * @license     Apache License, Version 2.0
 */

namespace Uecode\Bundle\QPushBundle\Provider;

use IronMQ;
use Doctrine\Common\Cache\Cache;
use Symfony\Bridge\Monolog\Logger;
use Uecode\Bundle\QPushBundle\Event\Events;
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
use Uecode\Bundle\QPushBundle\Event\NotificationEvent;
use Uecode\Bundle\QPushBundle\Message\Message;

/**
 * @author <NAME> <<EMAIL>>
 */
class IronMqProvider extends AbstractProvider
{
    /**
     * IronMQ Client
     *
     * @var IronMQ
     */
    private $ironmq;

    /**
     * IronMQ Queue
     *
     * @var stdObject
     */
    private $queue;

    public function __construct($name, array $options, $client, Cache $cache, Logger $logger)
    {
        $this->name     = $name;
        $this->options  = $options;
        $this->ironmq   = $client;
        $this->cache    = $cache;
        $this->logger   = $logger;
    }

    public function getProvider()
    {
        return "IronMQ";
    }

    /**
     * Builds the configured queues
     *
     * If a Queue name is passed and configured, this method will build only that
     * Queue.
     *
     * All Create methods are idempotent, if the resource exists, the current ARN
     * will be returned
     *
     * @return Boolean
     */
    public function create()
    {
        if ($this->options['push_notifications']) {
            $params = [
                'push_type'     => 'multicast',
                'retries'       => $this->options['notification_retries'],
                'subscribers'   => []
            ];

            foreach ($this->options['subscribers'] as $subscriber) {
                if ($subscriber['protocol'] == "email") {
                    throw new \InvalidArgumentException(
                        'IronMQ only supports `http` or `https` subscribers!'
                    );
                }

                $params['subscribers'][] = ['url' => $subscriber['endpoint']];
            }

        } else {
            $params = ['push_type' => 'pull'];
        }

        $result = $this->ironmq->updateQueue($this->getNameWithPrefix(), $params);
        $this->queue = $result;

        $key = $this->getNameWithPrefix();
        $this->cache->save($key, json_encode($this->queue));

        $this->log(200, "Queue has been created.", $params);

        return true;
    }

    /**
     * @return Boolean
     */
    public function destroy()
    {
        // Catch `queue not found` exceptions, throw the rest.
        try {
            $this->ironmq->deleteQueue($this->getNameWithPrefix());
        } catch ( \Exception $e) {
            if (false !== strpos($e->getMessage(), "Queue not found")) {
                $this->log(400, "Queue did not exist");
            } else {
                throw $e;
            }
        }

        $key = $this->getNameWithPrefix();
        $this->cache->delete($key);

        $this->log(200, "Queue has been destroyed.");

        return true;
    }

    /**
     * Pushes a message to the Queue
     *
     * @param array $message The message to queue
     *
     * @return int
     */
    public function publish(array $message)
    {
        $publishStart = microtime(true);

        if (!$this->queueExists()) {
            $this->create();
        }

        $result = $this->ironmq->postMessage(
            $this->getNameWithPrefix(),
            json_encode([$this->name => $message]),
            [
                'timeout'       => $this->options['message_timeout'],
                'delay'         => $this->options['message_delay'],
                'expires_in'    => $this->options['message_expiration']
            ]
        );

        $context = [
            'message_id'    => $result->id,
            'publish_time'  => microtime(true) - $publishStart
        ];
        $this->log(200, "Message has been published.", $context);

        return $result->id;
    }

    /**
     * Polls the Queue for Messages
     *
     * @return array
     */
    public function receive()
    {
        if (!$this->queueExists()) {
            $this->create();
        }

        $messages = $this->ironmq->getMessages(
            $this->getNameWithPrefix(),
            $this->options['messages_to_receive'],
            $this->options['message_timeout']
        );

        // Convert to Message Class
        foreach ($messages as &$message) {
            $id         = $message->id;
            $body       = $message->body;
            $metadata   = [
                'timeout'           => $message->timeout,
                'reserved_count'    => $message->reserved_count,
                'push_status'       => $message->push_status
            ];

            $message = new Message($id, $body, $metadata);

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

        return $messages;
    }

    /**
     * @return Boolean
     */<commit_msg>IronMqProvider no message bug fixed<commit_after><?php

/**
 * Copyright 2014 Underground Elephant
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package     qpush-bundle
 * @copyright   Underground Elephant 2014
 * @license     Apache License, Version 2.0
 */

namespace Uecode\Bundle\QPushBundle\Provider;

use IronMQ;
use Doctrine\Common\Cache\Cache;
use Symfony\Bridge\Monolog\Logger;
use Uecode\Bundle\QPushBundle\Event\Events;
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
use Uecode\Bundle\QPushBundle\Event\NotificationEvent;
use Uecode\Bundle\QPushBundle\Message\Message;

/**
 * @author <NAME> <<EMAIL>>
 */
class IronMqProvider extends AbstractProvider
{
    /**
     * IronMQ Client
     *
     * @var IronMQ
     */
    private $ironmq;

    /**
     * IronMQ Queue
     *
     * @var stdObject
     */
    private $queue;

    public function __construct($name, array $options, $client, Cache $cache, Logger $logger)
    {
        $this->name     = $name;
        $this->options  = $options;
        $this->ironmq   = $client;
        $this->cache    = $cache;
        $this->logger   = $logger;
    }

    public function getProvider()
    {
        return "IronMQ";
    }

    /**
     * Builds the configured queues
     *
     * If a Queue name is passed and configured, this method will build only that
     * Queue.
     *
     * All Create methods are idempotent, if the resource exists, the current ARN
     * will be returned
     *
     * @return Boolean
     */
    public function create()
    {
        if ($this->options['push_notifications']) {
            $params = [
                'push_type'     => 'multicast',
                'retries'       => $this->options['notification_retries'],
                'subscribers'   => []
            ];

            foreach ($this->options['subscribers'] as $subscriber) {
                if ($subscriber['protocol'] == "email") {
                    throw new \InvalidArgumentException(
                        'IronMQ only supports `http` or `https` subscribers!'
                    );
                }

                $params['subscribers'][] = ['url' => $subscriber['endpoint']];
            }

        } else {
            $params = ['push_type' => 'pull'];
        }

        $result = $this->ironmq->updateQueue($this->getNameWithPrefix(), $params);
        $this->queue = $result;

        $key = $this->getNameWithPrefix();
        $this->cache->save($key, json_encode($this->queue));

        $this->log(200, "Queue has been created.", $params);

        return true;
    }

    /**
     * @return Boolean
     */
    public function destroy()
    {
        // Catch `queue not found` exceptions, throw the rest.
        try {
            $this->ironmq->deleteQueue($this->getNameWithPrefix());
        } catch ( \Exception $e) {
            if (false !== strpos($e->getMessage(), "Queue not found")) {
                $this->log(400, "Queue did not exist");
            } else {
                throw $e;
            }
        }

        $key = $this->getNameWithPrefix();
        $this->cache->delete($key);

        $this->log(200, "Queue has been destroyed.");

        return true;
    }

    /**
     * Pushes a message to the Queue
     *
     * @param array $message The message to queue
     *
     * @return int
     */
    public function publish(array $message)
    {
        $publishStart = microtime(true);

        if (!$this->queueExists()) {
            $this->create();
        }

        $result = $this->ironmq->postMessage(
            $this->getNameWithPrefix(),
            json_encode([$this->name => $message]),
            [
                'timeout'       => $this->options['message_timeout'],
                'delay'         => $this->options['message_delay'],
                'expires_in'    => $this->options['message_expiration']
            ]
        );

        $context = [
            'message_id'    => $result->id,
            'publish_time'  => microtime(true) - $publishStart
        ];
        $this->log(200, "Message has been published.", $context);

        return $result->id;
    }

    /**
     * Polls the Queue for Messages
     *
     * @return array
     */
    public function receive()
    {
        if (!$this->queueExists()) {
            $this->create();
        }

        $messages = $this->ironmq->getMessages(
            $this->getNameWithPrefix(),
            $this->options['messages_to_receive'],
            $this->options['message_timeout']
        );
        
        if (!$messages) {
            return array();
        }

        // Convert to Message Class
        foreach ($messages as &$message) {
            $id         = $message->id;
            $body       = $message->body;
            $metadata   = [
                'timeout'           => $message->timeout,
                'reserved_count'    => $message->reserved_count,
                'push_status'       => $message->push_status
            ];

            $message = new Message($id, $body, $metadata);

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

        return $messages;
    }

    /**
     * @return Boolean
     */
{'fraction_non_alphanumeric': 0.1332450331125828, 'fraction_numerical': 0.005121412803532009, 'mean_word_length': 1.8146123260437377, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 141, 'https://': 0, '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': '6251816', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3083, 'n_words': 780}