Skip to content

Commit

Permalink
- (API) Refactored the order in which the onBeforeSend methods are se…
Browse files Browse the repository at this point in the history
…nt. The api schedule is now triggered before the api type.
  • Loading branch information
objectivehtml committed Dec 16, 2014
1 parent 2bb775f commit 7506718
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
21 changes: 17 additions & 4 deletions models/Postmaster_NotificationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
26 changes: 18 additions & 8 deletions models/Postmaster_ParcelModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 7506718

Please sign in to comment.