Extract subscribers to a separate table in the Bounce worker [MAILPOET-940]
This commit is contained in:
61
lib/Models/ScheduledTaskSubscriber.php
Normal file
61
lib/Models/ScheduledTaskSubscriber.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class ScheduledTaskSubscriber extends Model {
|
||||
const STATUS_TO_PROCESS = 0;
|
||||
const STATUS_PROCESSED = 1;
|
||||
|
||||
public static $_table = MP_SCHEDULED_TASK_SUBSCRIBERS_TABLE;
|
||||
public static $_id_column = array('task_id', 'subscriber_id');
|
||||
|
||||
static function createOrUpdate($data = array()) {
|
||||
if(!is_array($data) || empty($data['task_id']) || empty($data['subscriber_id'])) return;
|
||||
|
||||
$task_subscriber = self::where('subscriber_id', $data['subscriber_id'])
|
||||
->where('task_id', $data['task_id'])
|
||||
->findOne();
|
||||
|
||||
if(empty($task_subscriber)) $task_subscriber = self::create();
|
||||
|
||||
$task_subscriber->task_id = $data['task_id'];
|
||||
$task_subscriber->subscriber_id = $data['subscriber_id'];
|
||||
$task_subscriber->processed = self::STATUS_TO_PROCESS;
|
||||
$task_subscriber->save();
|
||||
|
||||
return $task_subscriber;
|
||||
}
|
||||
|
||||
static function addSubscribers($task_id, array $subscriber_ids) {
|
||||
foreach($subscriber_ids as $subscriber_id) {
|
||||
self::createOrUpdate(array(
|
||||
'task_id' => $task_id,
|
||||
'subscriber_id' => $subscriber_id
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
static function getToProcessCount($task_id) {
|
||||
return self::getCount($task_id, false);
|
||||
}
|
||||
|
||||
static function getProcessedCount($task_id) {
|
||||
return self::getCount($task_id, true);
|
||||
}
|
||||
|
||||
static function getTotalCount($task_id) {
|
||||
return self::getCount($task_id);
|
||||
}
|
||||
|
||||
private static function getCount($task_id, $processed = null) {
|
||||
$orm = self::where('task_id', $task_id);
|
||||
if(!is_null($processed)) {
|
||||
$orm->where(
|
||||
'processed',
|
||||
($processed) ? self::STATUS_PROCESSED : self::STATUS_TO_PROCESS
|
||||
);
|
||||
}
|
||||
return $orm->count();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user