diff --git a/models/Postmaster_NotificationModel.php b/models/Postmaster_NotificationModel.php index 1b01983..eaffe35 100644 --- a/models/Postmaster_NotificationModel.php +++ b/models/Postmaster_NotificationModel.php @@ -81,38 +81,51 @@ public function getNotificationSchedule($class = false) public function marshal() { + // Get the notification schedule instance $notificationSchedule = $this->getNotificationSchedule(); + // Check to see if the notification should send by passing the last + // time the notification was sent as a Carbon\Carbon object if($notificationSchedule->shouldSend($this->lastSent())) { + // Get the notification type instance $notificationType = $this->getNotificationType(); - + + // Create a new Postmaster_TransportModel object $transport = new Postmaster_TransportModel(array( 'service' => $this->service, 'settings' => $notificationType->getSettings(), 'data' => array() )); - if( $notificationType->onBeforeSend($transport) !== false && - $notificationSchedule->onBeforeSend($transport) !== false) + // Call onBeforeSend methods to the notification schedule and notification type + // If either instance returns false, the message will not be sent + if( $notificationSchedule->onBeforeSend($transport) !== false && + $notificationType->onBeforeSend($transport) !== false ) { + // Send the transport object $response = $this->send($transport); // Test the service response for correct class and throw an error if it fails if(!$response instanceof \Craft\Postmaster_TransportResponseModel) { - throw new Exception('The '.$notificationSchedule->service->name.' service did not return a \Craft\Postmaster_TransportResponseModel'); + throw new Exception('The '.$notificationSchedule->getService()->name.' service did not return a \Craft\Postmaster_TransportResponseModel'); } + // Call the onAfterSend method on the notification schedule $notificationSchedule->onAfterSend($response); + // Call the onAfterSend method on the notification type $notificationType->onAfterSend($response); // Save the response to the db $response->save(); + // If the response is a success, create a notification sent record if($response->getSuccess()) { + // Pass $this object to the createSentNotification method to + // create the actual record in the db craft()->postmaster_notifications->createSentNotification($this); } } diff --git a/models/Postmaster_ParcelModel.php b/models/Postmaster_ParcelModel.php index 4ea12f6..47d4dd3 100644 --- a/models/Postmaster_ParcelModel.php +++ b/models/Postmaster_ParcelModel.php @@ -31,31 +31,41 @@ public function lastSent() public function send(Postmaster_TransportModel $transport) { - $lastSent = craft()->postmaster_parcels->lastSent($this->id); + // Get the parcel schedule instance + $parcelSchedule = $this->getParcelSchedule(); - if($this->getParcelSchedule()->shouldSend($lastSent)) + // Check to see if the notification should send by passing the last + // time the notification was sent as a Carbon\Carbon object + if($parcelSchedule->shouldSend($this->lastSent())) { + // Get the parcel type instance $parcelType = $this->getParcelType(); - $parcelSchedule = $this->getParcelSchedule(); - - if( $parcelType->onBeforeSend($transport) !== false && - $parcelSchedule->onBeforeSend($transport) !== false) + // Call onBeforeSend methods to the parcel schedule and parcel type + // If either instance returns false, the message will not be sent + if( $parcelSchedule->onBeforeSend($transport) !== false && + $parcelType->onBeforeSend($transport) !== false ) { + // Send the transport object $response = parent::send($transport); - // Test the parcel response for correct class and throw an error if it fails + // Test the service response for correct class and throw an error if it fails if(!$response instanceof \Craft\Postmaster_TransportResponseModel) { - throw new Exception('The '.$transport->service->name.' service did not return a \Craft\Postmaster_TransportResponseModel'); + throw new Exception('The '.$parcelSchedule->getService()->name.' service did not return a \Craft\Postmaster_TransportResponseModel'); } + // Call on the onAfterSend method for the parcel schedule $parcelSchedule->onAfterSend($response); + // Call on the onAfterSend method for the parcel type $parcelType->onAfterSend($response); + // If the response is a success, create a parcel sent record if($response->getSuccess()) { + // Pass $this object to the createSentParcel method to + // create the actual record in the db craft()->postmaster_parcels->createSentParcel($this); } }