-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: always take the API domain value from configuration. If the API …
…domain is changed between job creation and job send, this would cause the send attempt to fail with an API credentials error. Allow empty parameters in job creation but fail if the job is processed with empty parameters. Use the API connector class created at job construction
- Loading branch information
Showing
1 changed file
with
26 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
use Symbiote\QueuedJobs\Services\QueuedJobService; | ||
use DateTime; | ||
use DateTimeZone; | ||
use Exception; | ||
|
||
/** | ||
* @author James Ellis <[email protected]> | ||
|
@@ -19,6 +18,8 @@ class SendJob extends AbstractQueuedJob | |
{ | ||
protected $totalSteps = 1; | ||
|
||
protected $connector; | ||
|
||
public function getJobType() | ||
{ | ||
$this->totalSteps = 1; | ||
|
@@ -39,20 +40,20 @@ public function getSignature() | |
return md5($this->domain . ":" . serialize($this->parameters)); | ||
} | ||
|
||
/** | ||
* Create the job | ||
* @param string domain DEPRECATED | ||
* @param array parameters for Mailgun API | ||
*/ | ||
public function __construct($domain = "", $parameters = []) | ||
{ | ||
if (!$domain) { | ||
return; | ||
} | ||
if (empty($parameters)) { | ||
return; | ||
} | ||
$this->domain = $domain; | ||
$this->connector = new MessageConnector; | ||
$this->domain = $this->connector->getApiDomain(); | ||
$this->parameters = $parameters; | ||
} | ||
|
||
/** | ||
* polls for 'failed' events in the last day and tries to resubmit them | ||
* Attempt to send the message via the Mailgun API | ||
*/ | ||
public function process() | ||
{ | ||
|
@@ -63,41 +64,44 @@ public function process() | |
|
||
$this->currentStep += 1; | ||
|
||
$connector = new MessageConnector; | ||
$client = $connector->getClient(); | ||
$client = $this->connector->getClient(); | ||
$domain = $this->connector->getApiDomain(); | ||
|
||
$domain = $this->domain; | ||
$parameters = $this->parameters; | ||
if (!$domain) { | ||
$msg = "Mailgun SendJob is missing the Mailgun API domain value"; | ||
$this->messages[] = $msg; | ||
throw new \Exception($msg); | ||
} | ||
|
||
if (!$domain || empty($parameters)) { | ||
$msg = "SendJob is missing either the domain or parameters properties"; | ||
if(empty($this->parameters)) { | ||
$msg = "Mailgun SendJob was called with empty parameters"; | ||
$this->messages[] = $msg; | ||
throw new Exception($msg); | ||
throw new \Exception($msg); | ||
} | ||
|
||
$msg = "Unknown error"; | ||
try { | ||
// if required, apply the default recipient | ||
$connector->applyDefaultRecipient($parameters); | ||
$this->connector->applyDefaultRecipient($this->parameters); | ||
// decode all attachments | ||
$connector->decodeAttachments($parameters); | ||
$this->connector->decodeAttachments($this->parameters); | ||
// send directly via the API client | ||
$response = $client->messages()->send($domain, $parameters); | ||
$response = $client->messages()->send($domain, $this->parameters); | ||
$message_id = ""; | ||
if ($response && ($response instanceof SendResponse) && ($message_id = $response->getId())) { | ||
$message_id = $connector::cleanMessageId($message_id); | ||
$message_id = MessageConnector::cleanMessageId($message_id); | ||
$this->parameters = [];//remove all params | ||
$msg = "OK {$message_id}"; | ||
$this->messages[] = $msg; | ||
$this->isComplete = true; | ||
return; | ||
} | ||
throw new Exception("SendJob invalid response or no message.id returned"); | ||
throw new \Exception("SendJob invalid response or no message.id returned"); | ||
} catch (Exception $e) { | ||
// API level errors caught here | ||
$msg = $e->getMessage(); | ||
} | ||
$this->messages[] = $msg; | ||
throw new Exception($msg); | ||
throw new \Exception($msg); | ||
} | ||
} |