diff --git a/src/Exception/MessageException.php b/src/Exception/MessageException.php new file mode 100644 index 000000000..5f0903ecc --- /dev/null +++ b/src/Exception/MessageException.php @@ -0,0 +1,8 @@ +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 ), @@ -108,8 +117,6 @@ public function sendMessage(): bool { $this->SendNotificationMail(); remove_action( 'commonsbooking_mail_sent',array($this,'updateEmailSent'), 5 ); - - return true; } /** diff --git a/src/Messages/BookingMessage.php b/src/Messages/BookingMessage.php index b7f8de5cb..aef848ca5 100644 --- a/src/Messages/BookingMessage.php +++ b/src/Messages/BookingMessage.php @@ -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() ); diff --git a/src/Messages/BookingReminderMessage.php b/src/Messages/BookingReminderMessage.php index 2285c6882..3062f3ba7 100644 --- a/src/Messages/BookingReminderMessage.php +++ b/src/Messages/BookingReminderMessage.php @@ -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 ); diff --git a/src/Messages/Message.php b/src/Messages/Message.php index deaa0c541..e762e4668 100644 --- a/src/Messages/Message.php +++ b/src/Messages/Message.php @@ -2,6 +2,7 @@ namespace CommonsBooking\Messages; +use CommonsBooking\Exception\MessageException; use WP_Error; use function commonsbooking_parse_template; @@ -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() ) ) { diff --git a/src/Messages/RestrictionMessage.php b/src/Messages/RestrictionMessage.php index 7d2d7261a..525d7e579 100644 --- a/src/Messages/RestrictionMessage.php +++ b/src/Messages/RestrictionMessage.php @@ -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 diff --git a/src/Service/Booking.php b/src/Service/Booking.php index 44d215c72..f8b495ec9 100644 --- a/src/Service/Booking.php +++ b/src/Service/Booking.php @@ -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(); } } } @@ -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(); } } } diff --git a/src/Service/BookingCodes.php b/src/Service/BookingCodes.php index 2726e5b57..4e79acdcc 100644 --- a/src/Service/BookingCodes.php +++ b/src/Service/BookingCodes.php @@ -2,6 +2,7 @@ namespace CommonsBooking\Service; +use CommonsBooking\Exception\MessageException; use CommonsBooking\Messages\BookingCodesMessage; use CommonsBooking\Model\BookingCode; use CommonsBooking\Wordpress\CustomPostType\Timeframe; @@ -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'] ); - } - - } + } } } diff --git a/src/View/BookingCodes.php b/src/View/BookingCodes.php index 71ee73bd4..e6dda7d94 100644 --- a/src/View/BookingCodes.php +++ b/src/View/BookingCodes.php @@ -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( diff --git a/tests/php/Messages/BookingMessageTest.php b/tests/php/Messages/BookingMessageTest.php index 4cface2c5..ba3f517e7 100644 --- a/tests/php/Messages/BookingMessageTest.php +++ b/tests/php/Messages/BookingMessageTest.php @@ -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); diff --git a/tests/php/Messages/BookingReminderMessageTest.php b/tests/php/Messages/BookingReminderMessageTest.php index 503110768..452e7463c 100644 --- a/tests/php/Messages/BookingReminderMessageTest.php +++ b/tests/php/Messages/BookingReminderMessageTest.php @@ -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); diff --git a/tests/php/Messages/RestrictionMessageTest.php b/tests/php/Messages/RestrictionMessageTest.php index fb30d24fa..e1cf19cce 100644 --- a/tests/php/Messages/RestrictionMessageTest.php +++ b/tests/php/Messages/RestrictionMessageTest.php @@ -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; @@ -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 ); @@ -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 ); @@ -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 );