Skip to content

Commit

Permalink
Merge pull request #28 from BBrunekreeft/master
Browse files Browse the repository at this point in the history
Remove obsolete database fields
  • Loading branch information
nterms authored Feb 19, 2017
2 parents e09361b + c7b04a4 commit 7d50cd7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion MailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function process()

$success = true;

$items = Queue::find()->where(['and', ['sent_time' => NULL], ['!=', 'to', 'a:0:{}'], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound);
$items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound);
foreach ($items->each() as $item) {
if ($message = $item->toMessage()) {
$attributes = ['attempts', 'last_attempt_time'];
Expand Down
30 changes: 0 additions & 30 deletions Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,11 @@ public function queue($time_to_send = 'now')

$item = new Queue();

$item->from = serialize($this->from);
$item->to = serialize($this->getTo());
$item->cc = serialize($this->getCc());
$item->bcc = serialize($this->getBcc());
$item->reply_to = serialize($this->getReplyTo());
$item->charset = $this->getCharset();
$item->subject = $this->getSubject();
$item->attempts = 0;
$item->swift_message = base64_encode(serialize($this));
$item->time_to_send = date('Y-m-d H:i:s', $time_to_send);

$parts = $this->getSwiftMessage()->getChildren();
// if message has no parts, use message
if ( !is_array($parts) || !sizeof($parts) ) {
$parts = [ $this->getSwiftMessage() ];
}

foreach( $parts as $part ) {
if( !( $part instanceof \Swift_Mime_Attachment ) ) {
/* @var $part \Swift_Mime_MimeEntity */
switch( $part->getContentType() ) {
case 'text/html':
$item->html_body = $part->getBody();
break;
case 'text/plain':
$item->text_body = $part->getBody();
break;
}

if( !$item->charset ) {
$item->charset = $part->getCharset();
}
}
}

return $item->save();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use yii\db\Migration;
use nterms\mailqueue\MailQueue;

/**
* Handles adding swift_message to table `mail_queue`.
*/
class m170217_124201_drop_obsolete_columns_from_mail_queue_table extends Migration
{
/**
* @inheritdoc
*/
public function up()
{
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'from');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'to');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'cc');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'bcc');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'html_body');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'text_body');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'reply_to');
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'charset');
}

/**
* @inheritdoc
*/
public function down()
{
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'from', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'to', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'cc', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'bcc', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'html_body', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'text_body', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'reply_to', 'text');
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'charset', 'string');
}
}
12 changes: 2 additions & 10 deletions models/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@
/**
* This is the model class for table "{{%mail_queue}}".
*
* @property string $from
* @property string $to
* @property string $cc
* @property string $bcc
* @property string $subject
* @property string $html_body
* @property string $text_body
* @property string $reply_to
* @property string $charset
* @property integer $created_at
* @property integer $attempts
* @property integer $last_attempt_time
* @property integer $sent_time
* @property string $time_to_send
* @property string swift_message
* @property string $swift_message
*/
class Queue extends ActiveRecord
{
Expand Down Expand Up @@ -61,7 +53,7 @@ public function rules()
return [
[['created_at', 'attempts', 'last_attempt_time', 'sent_time'], 'integer'],
[['time_to_send', 'swift_message'], 'required'],
[['to', 'cc', 'bcc', 'subject', 'html_body', 'text_body', 'charset'], 'safe'],
[['subject'], 'safe'],
];
}

Expand Down

0 comments on commit 7d50cd7

Please sign in to comment.