Add stats notifications email scheduler
[MAILPOET-1571]
This commit is contained in:
85
lib/Cron/Workers/StatsNotifications.php
Normal file
85
lib/Cron/Workers/StatsNotifications.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Cron\Workers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Tasks\Sending;
|
||||
|
||||
class StatsNotifications {
|
||||
|
||||
const TASK_TYPE = 'stats_notification';
|
||||
const SETTINGS_KEY = 'stats_notifications';
|
||||
|
||||
/**
|
||||
* How many hours after the newsletter will be the stats notification sent
|
||||
* @var int
|
||||
*/
|
||||
const HOURS_TO_SEND_AFTER_NEWSLETTER = 24;
|
||||
|
||||
function schedule($newsletter_id) {
|
||||
if(!$this->shouldSchedule($newsletter_id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$task = ScheduledTask::create();
|
||||
$task->type = self::TASK_TYPE;
|
||||
$task->status = ScheduledTask::STATUS_SCHEDULED;
|
||||
$task->scheduled_at = $this->getNextRunDate();
|
||||
$task->save();
|
||||
|
||||
$queue = SendingQueue::create();
|
||||
$queue->newsletter_id = $newsletter_id;
|
||||
$queue->task_id = $task->id;
|
||||
$queue->save();
|
||||
}
|
||||
|
||||
function process() {
|
||||
|
||||
}
|
||||
|
||||
private function shouldSchedule($newsletter_id) {
|
||||
if($this->isDisabled()) {
|
||||
return false;
|
||||
}
|
||||
if($this->isTaskScheduled($newsletter_id)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function isDisabled() {
|
||||
$settings = Setting::getValue(self::SETTINGS_KEY);
|
||||
if(!is_array($settings)) {
|
||||
return true;
|
||||
}
|
||||
if(!isset($settings['enabled'])) {
|
||||
return true;
|
||||
}
|
||||
if(!isset($settings['address'])) {
|
||||
return true;
|
||||
}
|
||||
if(empty(trim($settings['address']))) {
|
||||
return true;
|
||||
}
|
||||
return !(bool)$settings['enabled'];
|
||||
}
|
||||
|
||||
private function isTaskScheduled($newsletter_id) {
|
||||
$existing = ScheduledTask::table_alias('tasks')
|
||||
->join(SendingQueue::$_table, 'tasks.id = queues.task_id', 'queues')
|
||||
->where('tasks.type', self::TASK_TYPE)
|
||||
->where('queues.newsletter_id', $newsletter_id)
|
||||
->findMany();
|
||||
return (bool) $existing;
|
||||
}
|
||||
|
||||
private function getNextRunDate() {
|
||||
$date = new Carbon();
|
||||
$date->addHours(self::HOURS_TO_SEND_AFTER_NEWSLETTER);
|
||||
return $date;
|
||||
}
|
||||
|
||||
}
|
93
tests/integration/Cron/Workers/StatsNotificationsTest.php
Normal file
93
tests/integration/Cron/Workers/StatsNotificationsTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\Cron\Workers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Cron\Workers\StatsNotifications;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
class StatsNotificationsTest extends \MailPoetTest {
|
||||
|
||||
/** @var StatsNotifications */
|
||||
private $stats_notifications;
|
||||
|
||||
function _before() {
|
||||
$this->stats_notifications = new StatsNotifications();
|
||||
Setting::setValue(StatsNotifications::SETTINGS_KEY, [
|
||||
'enabled' => true,
|
||||
'address' => 'email@example.com'
|
||||
]);
|
||||
}
|
||||
|
||||
function testShouldSchedule() {
|
||||
$newsletter_id = 5;
|
||||
$this->stats_notifications->schedule($newsletter_id);
|
||||
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
|
||||
expect($queue)->isInstanceOf(SendingQueue::class);
|
||||
$task = ScheduledTask::where('id', $queue->task_id)->findOne();
|
||||
expect($task)->isInstanceOf(ScheduledTask::class);
|
||||
}
|
||||
|
||||
function testShouldNotScheduleIfDisabled() {
|
||||
$newsletter_id = 6;
|
||||
Setting::setValue(StatsNotifications::SETTINGS_KEY, [
|
||||
'enabled' => false,
|
||||
'address' => 'email@example.com'
|
||||
]);
|
||||
$this->stats_notifications->schedule($newsletter_id);
|
||||
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
|
||||
expect($queue)->isEmpty();
|
||||
}
|
||||
|
||||
function testShouldNotScheduleIfSettingsMissing() {
|
||||
$newsletter_id = 7;
|
||||
Setting::setValue(StatsNotifications::SETTINGS_KEY, []);
|
||||
$this->stats_notifications->schedule($newsletter_id);
|
||||
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
|
||||
expect($queue)->isEmpty();
|
||||
}
|
||||
|
||||
function testShouldNotScheduleIfEmailIsMissing() {
|
||||
$newsletter_id = 8;
|
||||
Setting::setValue(StatsNotifications::SETTINGS_KEY, [
|
||||
'enabled' => true,
|
||||
]);
|
||||
$this->stats_notifications->schedule($newsletter_id);
|
||||
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
|
||||
expect($queue)->isEmpty();
|
||||
}
|
||||
|
||||
function testShouldNotScheduleIfEmailIsEmpty() {
|
||||
$newsletter_id = 9;
|
||||
Setting::setValue(StatsNotifications::SETTINGS_KEY, [
|
||||
'enabled' => true,
|
||||
'address' => ' '
|
||||
]);
|
||||
$this->stats_notifications->schedule($newsletter_id);
|
||||
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
|
||||
expect($queue)->isEmpty();
|
||||
}
|
||||
|
||||
function testShouldNotScheduleIfAlreadyScheduled() {
|
||||
$newsletter_id = 10;
|
||||
$existing_task = ScheduledTask::createOrUpdate([
|
||||
'type' => StatsNotifications::TASK_TYPE,
|
||||
'status' => ScheduledTask::STATUS_SCHEDULED,
|
||||
'scheduled_at' => '2017-01-02 12:13:14',
|
||||
]);
|
||||
$existing_queue = SendingQueue::createOrUpdate([
|
||||
'newsletter_id' => $newsletter_id,
|
||||
'task_id' => $existing_task->id,
|
||||
]);
|
||||
$this->stats_notifications->schedule($newsletter_id);
|
||||
$queues = SendingQueue::where('newsletter_id', $newsletter_id)->findMany();
|
||||
expect($queues)->count(1);
|
||||
$tasks = ScheduledTask::where('id', $queues[0]->task_id)->findMany();
|
||||
expect($tasks)->count(1);
|
||||
expect($existing_queue->id)->equals($queues[0]->id);
|
||||
expect($existing_task->id)->equals($tasks[0]->id);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user