<commit_before><?php

namespace Silktide\QueueBall\Sqs;

use Aws\AwsClient;
use Aws\Sqs\SqsClient;
use Silktide\QueueBall\Message\QueueMessage;
use Silktide\QueueBall\Queue\AbstractQueue;
use Silktide\QueueBall\Exception\QueueException;
use Silktide\QueueBall\Message\QueueMessageFactoryInterface;

/**
 *
 */
class Queue extends AbstractQueue
{

    const DEFAULT_MESSAGE_LOCK_TIMEOUT = 120;

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

    /**
     * @var SqsClient
     */
    protected $queueClient;

    /**
     * @var QueueMessageFactoryInterface
     */
    protected $messageFactory;

    /**
     * @var int
     */
    protected $waitTime = 20;

    /**
     * @param SqsClient $sqsClient
     * @param QueueMessageFactoryInterface $messageFactory
     * @param string|null $queueId
     */
    public function __construct(SqsClient $sqsClient, QueueMessageFactoryInterface $messageFactory, $queueId = null)
    {
        parent::__construct($queueId);
        $this->queueClient = $sqsClient;
        $this->messageFactory = $messageFactory;
    }

    /**
     * {@inheritDoc}
     */
    public function setQueueId($queueId)
    {
        parent::setQueueId($queueId);
        $this->queueUrl = null;
    }

    /**
     * @param string $queueId
     * @return string
     */
    protected function getQueueUrl($queueId)
    {
        if (empty($this->queueUrl)) {
            if (empty($queueId)) {
                $queueId = $this->getQueueId();
            }
            $response = $this->queueClient->getQueueUrl(["QueueName" => $queueId]);
            $this->queueUrl = $response->get("QueueUrl");
        }
        return $this->queueUrl;
    }

    /**
     * {@inheritDoc}
     */
    public function createQueue($queueId, $options = [])
    {
        $timeout = empty($options["messageLockTimeout"])? self::DEFAULT_MESSAGE_LOCK_TIMEOUT: (int) $options["messageLockTimeout"];
        $attributes = [
            "VisibilityTimeout" => $timeout
        ];
        $this->queueClient->createQueue([
            "QueueName" => $queueId,
            "Attributes" => $attributes
        ]);
        $this->setQueueId($queueId);
    }

    /**
     * {@inheritDoc}
     */
    public function deleteQueue($queueId = null)
    {
        $queueUrl = $this->getQueueUrl($queueId);
        $this->queueClient->deleteQueue(["QueueUrl" => $queueUrl]);
    }

    /**
     * {@inheritDoc}<commit_msg>Fix bug where an invalid messageLockTimeout would be converted to integer zero (which is a valid timeout value). Now using the default value instead<commit_after><?php

namespace Silktide\QueueBall\Sqs;

use Aws\AwsClient;
use Aws\Sqs\SqsClient;
use Silktide\QueueBall\Message\QueueMessage;
use Silktide\QueueBall\Queue\AbstractQueue;
use Silktide\QueueBall\Exception\QueueException;
use Silktide\QueueBall\Message\QueueMessageFactoryInterface;

/**
 *
 */
class Queue extends AbstractQueue
{

    const DEFAULT_MESSAGE_LOCK_TIMEOUT = 120;

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

    /**
     * @var SqsClient
     */
    protected $queueClient;

    /**
     * @var QueueMessageFactoryInterface
     */
    protected $messageFactory;

    /**
     * @var int
     */
    protected $waitTime = 20;

    /**
     * @param SqsClient $sqsClient
     * @param QueueMessageFactoryInterface $messageFactory
     * @param string|null $queueId
     */
    public function __construct(SqsClient $sqsClient, QueueMessageFactoryInterface $messageFactory, $queueId = null)
    {
        parent::__construct($queueId);
        $this->queueClient = $sqsClient;
        $this->messageFactory = $messageFactory;
    }

    /**
     * {@inheritDoc}
     */
    public function setQueueId($queueId)
    {
        parent::setQueueId($queueId);
        $this->queueUrl = null;
    }

    /**
     * @param string $queueId
     * @return string
     */
    protected function getQueueUrl($queueId)
    {
        if (empty($this->queueUrl)) {
            if (empty($queueId)) {
                $queueId = $this->getQueueId();
            }
            $response = $this->queueClient->getQueueUrl(["QueueName" => $queueId]);
            $this->queueUrl = $response->get("QueueUrl");
        }
        return $this->queueUrl;
    }

    /**
     * {@inheritDoc}
     */
    public function createQueue($queueId, $options = [])
    {
        $timeout = (empty($options["messageLockTimeout"]) || !is_numeric($options["messageLockTimeout"]))
            ? self::DEFAULT_MESSAGE_LOCK_TIMEOUT
            : (int) $options["messageLockTimeout"];
        $attributes = [
            "VisibilityTimeout" => $timeout
        ];
        $this->queueClient->createQueue([
            "QueueName" => $queueId,
            "Attributes" => $attributes
        ]);
        $this->setQueueId($queueId);
    }

    /**
     * {@inheritDoc}
     */
    public function deleteQueue($queueId = null)
    {
        $queueUrl = $this->getQueueUrl($queueId);
        $this->queueClient->deleteQueue(["QueueUrl" => $queueUrl]);
    }

    /**
     * {@inheritDoc}
{'fraction_non_alphanumeric': 0.1368483412322275, 'fraction_numerical': 0.0019747235387045812, 'mean_word_length': 2.4016118200134318, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '3339049', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1480, 'n_words': 259}