Skip to content

Commit

Permalink
Extract send message to method
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 11, 2024
1 parent a5ddf80 commit 307ec78
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/services/SendoutsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use putyourlightson\campaign\records\SendoutRecord;
use Twig\Error\Error;
use yii\db\ActiveQuery;
use yii\mail\MessageInterface;

class SendoutsService extends Component
{
Expand Down Expand Up @@ -334,19 +335,7 @@ public function sendEmail(SendoutElement $sendout, ContactElement $contact, int
return;
}

$success = false;

// Attempt to send message
for ($i = 0; $i < Campaign::$plugin->settings->maxSendAttempts; $i++) {
$success = $message->send();

if ($success) {
break;
}

// Wait a second, in case we're being throttled
sleep(1);
}
$success = $this->_sendMessage($message);

if ($success) {
// Update sent date and save
Expand Down Expand Up @@ -832,4 +821,21 @@ private function _convertLinks(string $body, ContactElement $contact, SendoutEle
// Save document element to maintain utf-8 encoding (https://gist.github.com/Xeoncross/9401853)
return $dom->saveHTML($dom->documentElement);
}

/**
* Sends a message, potentially with multiple attempts.
*/
private function _sendMessage(MessageInterface $message): bool
{
for ($i = 0; $i < Campaign::$plugin->settings->maxSendAttempts; $i++) {
if ($message->send()) {
return true;
}

// Hang on a second, in case we’re being throttled.
sleep(1);
}

return false;
}
}

0 comments on commit 307ec78

Please sign in to comment.