Skip to content

Commit 1d5e2d3

Browse files
committed
- (API) Added sendToQueue() method to PostmasterService class
1 parent d08fc34 commit 1d5e2d3

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

services/PostmasterService.php

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public function service($class, $settings = null)
4343
return;
4444
}
4545

46+
/*
47+
* Send a Postmaster_TransportModel object to the queue
48+
*
49+
* @param Postmaster_TransportModel
50+
* @return object
51+
*/
52+
public function sendToQueue(Postmaster_TransportModel $model)
53+
{
54+
$record = new Postmaster_QueueRecord();
55+
$record->model = $model;
56+
$record->sendDate = $model->getSendDate();
57+
$record->save();
58+
59+
return $record;
60+
}
61+
4662
/*
4763
* Send a Postmaster_TransportModel object
4864
*
@@ -59,28 +75,43 @@ public function send(Postmaster_TransportModel $model)
5975
throw new Exception(Craft::t($message));
6076
}
6177

62-
// Triger onBeforeSend method, and if return false then fail
63-
if($model->service->onBeforeSend() !== false)
64-
{
65-
// Send the Postmaster_TransportModel model to the service in
66-
// exchange for a Craft\Plugins\Postmaster\Responses\TransportResponse object
67-
$response = $model->service->send($model);
68-
69-
// Test the service response for correct class and throw an error if it fails
70-
if(!$response instanceof \Craft\Postmaster_TransportResponseModel)
71-
{
72-
throw new Exception('The '.$model->service->name.' service did not return a \Craft\Postmaster_TransportResponseModel');
73-
}
74-
75-
// Trigger the onAfterSend method
76-
$model->service->onAfterSend();
77-
78-
// Save the response to the db
79-
$response->save();
80-
81-
// Return the actual response
82-
return $response;
83-
}
78+
// If the model has a queueId remove the record from the queue
79+
if($model->queueId)
80+
{
81+
craft()->postmaster_queue->remove($model->queueId);
82+
}
83+
84+
//Validate the model to see if the send past has not past
85+
if($model->shouldSend())
86+
{
87+
// Triger onBeforeSend method, and if return false then fail
88+
if($model->service->onBeforeSend() !== false)
89+
{
90+
// Send the Postmaster_TransportModel model to the service in
91+
// exchange for a Postmaster_TransportResponseModel object
92+
$response = $model->service->send($model);
93+
94+
// Test the service response for correct class and throw an error if it fails
95+
if(!$response instanceof \Craft\Postmaster_TransportResponseModel)
96+
{
97+
throw new Exception('The '.$model->service->name.' service did not return a \Craft\Postmaster_TransportResponseModel');
98+
}
99+
100+
// Trigger the onAfterSend method
101+
$model->service->onAfterSend();
102+
103+
// Save the response to the db
104+
$response->save();
105+
106+
// Return the actual response
107+
return $response;
108+
}
109+
}
110+
else
111+
{
112+
// Send the model to the queue to be sent later
113+
return $this->sendToQueue($model);
114+
}
84115

85116
// Return false if anything fails
86117
return false;

0 commit comments

Comments
 (0)