Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor messages #1561

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Exception/MessageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CommonsBooking\Exception;

class MessageException extends \Exception
{

}
29 changes: 18 additions & 11 deletions src/Messages/BookingCodesMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CommonsBooking\Messages;

use CommonsBooking\Exception\BookingCodeException;
use CommonsBooking\Exception\MessageException;
use CommonsBooking\Model\BookingCode;
use CommonsBooking\Settings\Settings;
use CommonsBooking\Repository\BookingCodes;
Expand Down Expand Up @@ -36,20 +38,27 @@ public function __construct( $postId, string $action, int $tsFrom=null, int $tsT
$this->tsTo=$tsTo;
}

/**
* prepares Message and sends by E-mail
*
* @return bool true if message was sent, false otherwise. If the message is not sent, an error is raised.
*/
public function sendMessage(): bool {
/**
* prepares Message and sends by E-mail
*
* @return void - Exception is thrown on error
* @throws MessageException|BookingCodeException
*/
protected function sendMessage(): void {
$timeframeId=(int)$this->getPostId();
$timeframe=new Timeframe($timeframeId);

if(!$this->prepareReceivers($timeframe)) return $this->raiseError(
__( "Unable to send Emails. No location email(s) configured, check location", "commonsbooking" ));
if(!$this->prepareReceivers($timeframe)) {
$this->raiseError(
__( "Unable to send Emails. No location email(s) configured, check location", "commonsbooking" ));
throw new MessageException( "Unable to send Emails. No location email(s) configured, check location" );
}

$bookingCodes = BookingCodes::getCodes($timeframeId, $this->tsFrom,$this->tsTo);
if(empty($bookingCodes)) return $this->raiseError( __( "Could not find booking codes for this timeframe/period", "commonsbooking" ));
if(empty($bookingCodes)) {
$this->raiseError( __( "Could not find booking codes for this timeframe/period", "commonsbooking" ));
throw new MessageException( "Could not find booking codes for this timeframe/period" );
}

$bookingTable=apply_filters('commonsbooking_emailcodes_rendertable',
\CommonsBooking\View\BookingCodes::renderBookingCodesTable( $bookingCodes ),
Expand Down Expand Up @@ -108,8 +117,6 @@ public function sendMessage(): bool {
$this->SendNotificationMail();

remove_action( 'commonsbooking_mail_sent',array($this,'updateEmailSent'), 5 );

return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/BookingMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BookingMessage extends Message {
*/
protected $validActions = [ "confirmed", "canceled" ];

public function sendMessage() {
protected function sendMessage(): void {
/** @var \CommonsBooking\Model\Booking $booking */
$booking = Booking::getPostById( $this->getPostId() );

Expand Down
3 changes: 1 addition & 2 deletions src/Messages/BookingReminderMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class BookingReminderMessage extends Message {

/**
* Sends reminder message.
* @throws \Exception
*/
public function sendMessage() {
protected function sendMessage(): void {
/** @var \CommonsBooking\Model\Booking $booking */
$booking = Booking::getPostById( $this->getPostId() );
$booking_user = get_userdata( $this->getPost()->post_author );
Expand Down
9 changes: 8 additions & 1 deletion src/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CommonsBooking\Messages;

use CommonsBooking\Exception\MessageException;
use WP_Error;
use function commonsbooking_parse_template;

Expand Down Expand Up @@ -183,11 +184,17 @@ public function SendNotificationMail() {
do_action( 'commonsbooking_mail_sent', $this->getAction(), $result );
}

abstract public function sendMessage();
/**
* Will send the message, should only be called through @see self::triggerMail() to check if action is valid
* @return void
* @throws MessageException
*/
abstract protected function sendMessage(): void;

/**
* Only send mail if action is valid
* @return void
* @throws MessageException
*/
public function triggerMail(): void {
if ( in_array( $this->getAction(), $this->getValidActions() ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/RestrictionMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct( $restriction, $user, Booking $booking, $action, boo
/**
* Sends mails related to restriction type and state.
*/
public function sendMessage() {
protected function sendMessage(): void {
if ( $this->getRestriction()->isActive() ) {
if ( $this->getRestriction()->getType() == Restriction::TYPE_HINT ) {
// send hint mail
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function sendReminderMessage() {
if ( count( $bookings ) ) {
foreach ( $bookings as $booking ) {
$reminderMessage = new BookingReminderMessage( $booking->getPost()->ID, 'pre-booking-reminder' );
$reminderMessage->sendMessage();
$reminderMessage->triggerMail();
}
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public static function sendFeedbackMessage() {
if ( count( $bookings ) ) {
foreach ( $bookings as $booking ) {
$reminderMessage = new BookingReminderMessage( $booking->getPost()->ID, 'post-booking-notice' );
$reminderMessage->sendMessage();
$reminderMessage->triggerMail();
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/Service/BookingCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CommonsBooking\Service;

use CommonsBooking\Exception\MessageException;
use CommonsBooking\Messages\BookingCodesMessage;
use CommonsBooking\Model\BookingCode;
use CommonsBooking\Wordpress\CustomPostType\Timeframe;
Expand Down Expand Up @@ -35,20 +36,21 @@ public static function sendBookingCodesMessage(): void {
if($params === false) continue;

$booking_msg = new BookingCodesMessage($post->ID, "codes",$params['from'],$params['to'] );
if(!$booking_msg->sendMessage()) {
try {
$booking_msg->triggerMail();
update_post_meta( $post->ID, \CommonsBooking\View\BookingCodes::NEXT_CRON_EMAIL, $params['nextCronEventTs'] );
}
catch( MessageException $e) {
set_transient(
BookingCode::ERROR_TYPE,
commonsbooking_sanitizeHTML(
__( "Error sending booking codes by E-mail for Timeframe ", 'commonsbooking' ) . get_the_title($post) . " ({$post->ID})"
. " " . $e->getMessage()
),
0
);
}
else {
update_post_meta( $post->ID, \CommonsBooking\View\BookingCodes::NEXT_CRON_EMAIL, $params['nextCronEventTs'] );
}

}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/View/BookingCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public static function emailCodes($timeframeId = null, $tsFrom=null, $tsTo=null)
, 10, 2 );

$booking_msg = new BookingCodesMessage($timeframeId, "codes",$tsFrom,$tsTo );
$booking_msg->sendMessage();
$booking_msg->triggerMail();

//this should never happen
wp_die(
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Messages/BookingMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testSendMessage() {
Settings::updateOption('commonsbooking_options_templates', 'emailtemplates_mail-booking_ics_attach', 'on');

$bookingMessage = new BookingMessage($this->bookingId, 'confirmed');
$bookingMessage->sendMessage();
$bookingMessage->triggerMail();
$mailer = $this->getMockMailer();
$this->assertEmpty($mailer->ErrorInfo);
$this->assertEquals(self::FROM_MAIL, $mailer->From);
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Messages/BookingReminderMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testSendMessage()
Settings::updateOption('commonsbooking_options_reminder', 'pre-booking-reminder-body', $body);

$bookingMessage = new BookingReminderMessage($this->bookingId, 'pre-booking-reminder');
$bookingMessage->sendMessage();
$bookingMessage->triggerMail();
$mailer = $this->getMockMailer();
$this->assertEmpty($mailer->ErrorInfo);
$this->assertEquals(self::FROM_MAIL, $mailer->From);
Expand Down
8 changes: 4 additions & 4 deletions tests/php/Messages/RestrictionMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RestrictionMessageTest extends Email_Test_Case {
const RESTRICION_REPAIR_NAME = 'Test Restriction (Repair)';

private $hintMessageText;
private $hintMessage;
private RestrictionMessage $hintMessage;
private $hintId;

private $repairMessageText;
Expand All @@ -35,7 +35,7 @@ public function testGetBooking() {
}

public function testSendMessage() {
$this->hintMessage->sendMessage();
$this->hintMessage->triggerMail();
$mailer = $this->getMockMailer();
$this->assertEmpty( $mailer->ErrorInfo );
$this->assertEquals( self::FROM_MAIL, $mailer->From );
Expand All @@ -58,7 +58,7 @@ public function testSendMessage() {
\CommonsBooking\Model\Restriction::TYPE_HINT,
true
);
$this->hintMessage->sendMessage();
$this->hintMessage->triggerMail();
$mailer = $this->getMockMailer();
$this->assertEmpty( $mailer->ErrorInfo );
$this->assertEquals( self::FROM_MAIL, $mailer->From );
Expand All @@ -73,7 +73,7 @@ public function testSendMessage() {
$this->resetMailer();

//now test repair message
$this->repairMessage->sendMessage();
$this->repairMessage->triggerMail();
$mailer = $this->getMockMailer();
$this->assertEmpty( $mailer->ErrorInfo );
$this->assertEquals( self::FROM_MAIL, $mailer->From );
Expand Down
Loading