<commit_before><?php namespace WebTorque\QueuedMailer\Queue;

use WebTorque\QueuedMailer\Transport\Transport;

class QueueProcessor implements QueueProcessorInterface
{
    /**
     * @var Transport
     */
    private $transport;

    public function __construct(Transport $transport)
    {
        $this->transport = $transport;
    }

    /**
     * Process the email queue
     *
     * To configure add to yml file
     *
     * <code>
     * QueueProcessor:
     *   batch_size: 100
     *   retry_time: 30 #minutes to wait unit retrying
     *   application_identifier: 'MyApplication' #identifier if using multiple apps
     * </code>
     */
    public function process()
    {
        $batchSize = self::config()->batch_size;
        $retryTime = self::config()->retry_time;
        $appIdentifier = self::config()->application_indentifier;

        $lastAttemptTime = \DBField::create_field('SS_Datetime',
            strtotime('-' . $retryTime . 'm', strtotime(\SS_Datetime::now())));

        $batch = QueuedEmail::get()->where(
            sprintf('"LastAttempt" <= \'%s\' OR LastAttempt IS NULL', $lastAttemptTime->getValue())
        )->limit($batchSize);

        foreach ($batch as $email) {
            $result = $this->transport->send(
                $appIdentifier,
                $appIdentifier . '-' . $email->ID,
                $email->To,
                $email->From,
                $email->Subject,
                $email->HTML,
                $email->Plain,
                $email->CC,
                $email->BCC,
                $email->loadAttachments(),
                $email->loadHeaders(),
                $email->ReplyTo
            );

            if ($result) {
                $email->Status = 'Sent';
                $email->Identifier = $result;
            } else {
                $email->Status = 'Failed';
            }

            $email->write();
        }
    }

    /**
     * Remove old emails so we don't clutter up the db over time
     */
    public function cleanup()
    {
        $cleanupDays = self::config()->cleanup_days;
        $batchSize = self::config()->batch_size;

        $cleanupTime = \DBField::create_field('SS_Datetime',
            strtotime('-' . $cleanupDays . 'd', strtotime(\SS_Datetime::now())));

        $emails = QueuedEmail::get()->where(sprintf(
                '("Status" = \'Sent\' AND "Created" <= \'%s\') OR ("Status" = \'Failed\' AND "LastAttempt" <= \'%s\')',
                $cleanupTime->getValue(),
                $cleanupTime->getValue()
            )<commit_msg>Fix cleanup date<commit_after><?php namespace WebTorque\QueuedMailer\Queue;

use WebTorque\QueuedMailer\Transport\Transport;

class QueueProcessor implements QueueProcessorInterface
{
    /**
     * @var Transport
     */
    private $transport;

    public function __construct(Transport $transport)
    {
        $this->transport = $transport;
    }

    /**
     * Process the email queue
     *
     * To configure add to yml file
     *
     * <code>
     * QueueProcessor:
     *   batch_size: 100
     *   retry_time: 30 #minutes to wait unit retrying
     *   application_identifier: 'MyApplication' #identifier if using multiple apps
     * </code>
     */
    public function process()
    {
        $batchSize = self::config()->batch_size;
        $retryTime = self::config()->retry_time;
        $appIdentifier = self::config()->application_indentifier;

        $lastAttemptTime = \DBField::create_field('SS_Datetime',
            strtotime('-' . $retryTime . 'm', strtotime(\SS_Datetime::now())));

        $batch = QueuedEmail::get()->where(
            sprintf('"LastAttempt" <= \'%s\' OR LastAttempt IS NULL', $lastAttemptTime->getValue())
        )->limit($batchSize);

        foreach ($batch as $email) {
            $result = $this->transport->send(
                $appIdentifier,
                $appIdentifier . '-' . $email->ID,
                $email->To,
                $email->From,
                $email->Subject,
                $email->HTML,
                $email->Plain,
                $email->CC,
                $email->BCC,
                $email->loadAttachments(),
                $email->loadHeaders(),
                $email->ReplyTo
            );

            if ($result) {
                $email->Status = 'Sent';
                $email->Identifier = $result;
            } else {
                $email->Status = 'Failed';
            }

            $email->write();
        }
    }

    /**
     * Remove old emails so we don't clutter up the db over time
     */
    public function cleanup()
    {
        $cleanupDays = self::config()->cleanup_days;
        $batchSize = self::config()->batch_size;

        $cleanupTime = \DBField::create_field('SS_Datetime',
            strtotime('-' . $cleanupDays . 'days', strtotime(\SS_Datetime::now())));

        $emails = QueuedEmail::get()->where(sprintf(
                '("Status" = \'Sent\' AND "Created" <= \'%s\') OR ("Status" = \'Failed\' AND "LastAttempt" <= \'%s\')',
                $cleanupTime->getValue(),
                $cleanupTime->getValue()
            )
{'fraction_non_alphanumeric': 0.14736842105263157, 'fraction_numerical': 0.001949317738791423, 'mean_word_length': 1.7961852861035423, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 65, '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': '6174667', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1399, 'n_words': 286}