Skip to content

Commit

Permalink
FIX: always take the API domain value from configuration. If the API …
Browse files Browse the repository at this point in the history
…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
JamesDPC committed Nov 23, 2020
1 parent 4d02912 commit 1f39da1
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/Jobs/SendJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symbiote\QueuedJobs\Services\QueuedJobService;
use DateTime;
use DateTimeZone;
use Exception;

/**
* @author James Ellis <[email protected]>
Expand All @@ -19,6 +18,8 @@ class SendJob extends AbstractQueuedJob
{
protected $totalSteps = 1;

protected $connector;

public function getJobType()
{
$this->totalSteps = 1;
Expand All @@ -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()
{
Expand All @@ -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);
}
}

0 comments on commit 1f39da1

Please sign in to comment.