Refactoring NotificationService

This commit is contained in:
Antonin 2015-10-13 01:03:41 +02:00
parent fe8f779217
commit 2b38a7ddd6
3 changed files with 95 additions and 90 deletions

View File

@ -23,15 +23,11 @@ use Framadate\Services\InputService;
use Framadate\Services\LogService; use Framadate\Services\LogService;
use Framadate\Services\MailService; use Framadate\Services\MailService;
use Framadate\Services\PollService; use Framadate\Services\PollService;
use Framadate\Services\NotificationService;
use Framadate\Utils; use Framadate\Utils;
include_once __DIR__ . '/app/inc/init.php'; include_once __DIR__ . '/app/inc/init.php';
/* Constants */
/* --------- */
const UPDATE_POLL = 1;
const DELETED_POLL = 2;
/* Variables */ /* Variables */
/* --------- */ /* --------- */
@ -49,40 +45,7 @@ $pollService = new PollService($connect, $logService);
$adminPollService = new AdminPollService($connect, $pollService, $logService); $adminPollService = new AdminPollService($connect, $pollService, $logService);
$inputService = new InputService(); $inputService = new InputService();
$mailService = new MailService($config['use_smtp']); $mailService = new MailService($config['use_smtp']);
$notificationService = new NotificationService($mailService);
/* Functions */
/*-----------*/
/**
* Send a notification to the poll admin to notify him about an update.
*
* @param stdClass $poll The poll
* @param MailService $mailService The mail service
* @param int $type cf: Constants on the top of this page
*/
function sendUpdateNotification($poll, $mailService, $type) {
if (!isset($_SESSION['mail_sent'])) {
$_SESSION['mail_sent'] = [];
}
if ($poll->receiveNewVotes) {
$subject = '[' . NOMAPPLICATION . '] ' . __f('Mail', 'Notification of poll: %s', $poll->title);
$message = '';
switch ($type) {
case UPDATE_POLL:
$message = __f('Mail', 'Someone just change your poll available at the following link %s.', Utils::getUrlSondage($poll->admin_id, true)) . "\n\n";
break;
case DELETED_POLL:
$message = __f('Mail', 'Someone just delete your poll %s.', Utils::htmlEscape($poll->title)) . "\n\n";
break;
}
$messageTypeKey = $type . '-' . $poll->id;
$mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey);
}
}
/* PAGE */ /* PAGE */
/* ---- */ /* ---- */
@ -176,7 +139,7 @@ if (isset($_POST['update_poll_info'])) {
// Update poll in database // Update poll in database
if ($updated && $adminPollService->updatePoll($poll)) { if ($updated && $adminPollService->updatePoll($poll)) {
$message = new Message('success', __('adminstuds', 'Poll saved')); $message = new Message('success', __('adminstuds', 'Poll saved'));
sendUpdateNotification($poll, $mailService, UPDATE_POLL); $notificationService->sendUpdateNotification($poll, $notificationService::UPDATE_POLL);
} else { } else {
$message = new Message('danger', __('Error', 'Failed to save poll')); $message = new Message('danger', __('Error', 'Failed to save poll'));
$poll = $pollService->findById($poll_id); $poll = $pollService->findById($poll_id);
@ -341,7 +304,7 @@ if (isset($_POST['delete_poll'])) {
if (isset($_POST['confirm_delete_poll'])) { if (isset($_POST['confirm_delete_poll'])) {
if ($adminPollService->deleteEntirePoll($poll_id)) { if ($adminPollService->deleteEntirePoll($poll_id)) {
$message = new Message('success', __('adminstuds', 'Poll fully deleted')); $message = new Message('success', __('adminstuds', 'Poll fully deleted'));
sendUpdateNotification($poll, $mailService, DELETED_POLL); $notificationService->sendUpdateNotification($poll, $notificationService::DELETED_POLL);
} else { } else {
$message = new Message('danger', __('Error', 'Failed to delete the poll')); $message = new Message('danger', __('Error', 'Failed to delete the poll'));
} }

View File

@ -0,0 +1,86 @@
<?php
namespace Framadate\Services;
use Framadate\Services\MailService;
use Framadate\Utils;
use \stdClass;
class NotificationService {
const UPDATE_VOTE = 1;
const ADD_VOTE = 2;
const ADD_COMMENT = 3;
const UPDATE_POLL = 10;
const DELETED_POLL = 11;
private $mailService;
function __construct(MailService $mailService) {
$this->mailService = $mailService;
}
/**
* Send a notification to the poll admin to notify him about an update.
*
* @param $poll stdClass The poll
* @param $name string The name user who triggered the notification
* @param $type int cf: Constants on the top of this page
*/
function sendUpdateNotification(stdClass $poll, $type, $name='') {
if (!isset($_SESSION['mail_sent'])) {
$_SESSION['mail_sent'] = [];
}
if ($poll->receiveNewVotes) {
if (self::isParticipation($type))
$translationString = 'Poll\'s participation: %s';
else
$translationString = 'Notification of poll: %s';
$subject = '[' . NOMAPPLICATION . '] ' . __f('Mail', $translationString, $poll->title);
$message = '';
if (self::isParticipation($type))
$message .= $name . ' ';
$urlSondage = Utils::getUrlSondage($poll->admin_id, true);
$link = '<a href="' . $urlSondage . '">' . $urlSondage . '</a>' . "\n\n";
switch ($type) {
case self::UPDATE_VOTE:
$message .= __('Mail', "updated a vote.\nYou can find your poll at the link") . " :\n\n";
$message .= $link;
break;
case self::ADD_VOTE:
$message .= __('Mail', "filled a vote.\nYou can find your poll at the link") . " :\n\n";
$message .= $link;
break;
case self::ADD_COMMENT:
$message .= __('Mail', "wrote a comment.\nYou can find your poll at the link") . " :\n\n";
$message .= $link;
break;
case self::UPDATE_POLL:
$message = __f('Mail', 'Someone just change your poll available at the following link %s.', Utils::getUrlSondage($poll->admin_id, true)) . "\n\n";
break;
case self::DELETED_POLL:
$message = __f('Mail', 'Someone just delete your poll %s.', Utils::htmlEscape($poll->title)) . "\n\n";
break;
}
$messageTypeKey = $type . '-' . $poll->id;
$this->mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey);
}
}
function isParticipation($type)
{
return $type >= self::UPDATE_POLL;
}
}

View File

@ -20,18 +20,13 @@ use Framadate\Services\LogService;
use Framadate\Services\PollService; use Framadate\Services\PollService;
use Framadate\Services\InputService; use Framadate\Services\InputService;
use Framadate\Services\MailService; use Framadate\Services\MailService;
use Framadate\Services\NotificationService;
use Framadate\Message; use Framadate\Message;
use Framadate\Utils; use Framadate\Utils;
use Framadate\Editable; use Framadate\Editable;
include_once __DIR__ . '/app/inc/init.php'; include_once __DIR__ . '/app/inc/init.php';
/* Constants */
/* --------- */
const UPDATE_VOTE = 1;
const ADD_VOTE = 2;
const ADD_COMMENT = 3;
/* Variables */ /* Variables */
/* --------- */ /* --------- */
@ -47,46 +42,8 @@ $logService = new LogService();
$pollService = new PollService($connect, $logService); $pollService = new PollService($connect, $logService);
$inputService = new InputService(); $inputService = new InputService();
$mailService = new MailService($config['use_smtp']); $mailService = new MailService($config['use_smtp']);
$notificationService = new NotificationService($mailService);
/* Functions */
/*-----------*/
/**
* Send a notification to the poll admin to notify him about an update.
*
* @param $poll stdClass The poll
* @param $mailService MailService The mail service
* @param $name string The name user who triggered the notification
* @param $type int cf: Constants on the top of this page
*/
function sendUpdateNotification($poll, $mailService, $name, $type) {
if (!isset($_SESSION['mail_sent'])) {
$_SESSION['mail_sent'] = [];
}
if ($poll->receiveNewVotes) {
$subject = '[' . NOMAPPLICATION . '] ' . __f('Mail', 'Poll\'s participation: %s', $poll->title);
$message = $name . ' ';
switch ($type) {
case UPDATE_VOTE:
$message .= __('Mail', "updated a vote.\nYou can find your poll at the link") . " :\n\n";
break;
case ADD_VOTE:
$message .= __('Mail', "filled a vote.\nYou can find your poll at the link") . " :\n\n";
break;
case ADD_COMMENT:
$message .= __('Mail', "wrote a comment.\nYou can find your poll at the link") . " :\n\n";
break;
}
$urlSondage = Utils::getUrlSondage($poll->admin_id, true);
$message .= '<a href="' . $urlSondage . '">' . $urlSondage . '</a>' . "\n\n";
$messageTypeKey = $type . '-' . $poll->id;
$mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey);
}
}
/* PAGE */ /* PAGE */
/* ---- */ /* ---- */
@ -139,7 +96,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote
} else { } else {
$message = new Message('success', __('studs', 'Update vote succeeded')); $message = new Message('success', __('studs', 'Update vote succeeded'));
} }
sendUpdateNotification($poll, $mailService, $name, UPDATE_VOTE); $notificationService->sendUpdateNotification($poll, $notificationService::UPDATE_VOTE, $name);
} else { } else {
$message = new Message('danger', __('Error', 'Update vote failed')); $message = new Message('danger', __('Error', 'Update vote failed'));
} }
@ -165,7 +122,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote
} else { } else {
$message = new Message('success', __('studs', 'Adding the vote succeeded')); $message = new Message('success', __('studs', 'Adding the vote succeeded'));
} }
sendUpdateNotification($poll, $mailService, $name, ADD_VOTE); $notificationService->sendUpdateNotification($poll, $notificationService::ADD_VOTE, $name);
} else { } else {
$message = new Message('danger', __('Error', 'Adding vote failed')); $message = new Message('danger', __('Error', 'Adding vote failed'));
} }
@ -189,12 +146,11 @@ if (isset($_POST['add_comment'])) {
$result = $pollService->addComment($poll_id, $name, $comment); $result = $pollService->addComment($poll_id, $name, $comment);
if ($result) { if ($result) {
$message = new Message('success', __('Comments', 'Comment added')); $message = new Message('success', __('Comments', 'Comment added'));
sendUpdateNotification($poll, $mailService, $name, ADD_COMMENT); $notificationService->sendUpdateNotification($poll, $notificationService::ADD_COMMENT, $name);
} else { } else {
$message = new Message('danger', __('Error', 'Comment failed')); $message = new Message('danger', __('Error', 'Comment failed'));
} }
} }
} }
// Retrieve data // Retrieve data