Skip to content

Commit

Permalink
Merge pull request #147 from nickvanderveeken/feature/sparkpost-campa…
Browse files Browse the repository at this point in the history
…ign-id

Add support for SparkPost "campaign_id" in Transmissions
  • Loading branch information
roelvanduijnhoven authored Sep 29, 2021
2 parents 80c2e34 + 10c9a76 commit a8a3bbe
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Mail/Message/SparkPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class SparkPost extends Message
*/
protected $variables = [];

/**
* Name of the campaign. Maximum length - 64 bytes
* @var string|null $campaignId
*/
protected $campaignId = null;

public function __construct(array $options = [])
{
$this->setOptions($options);
Expand Down Expand Up @@ -113,6 +119,35 @@ public function getTemplateId(): ?string
return $this->template;
}

/**
* Set SparkPost campaign ID to use. Maximum length is 64 bytes, and the input
* will be truncated if it exceeds that. Empty strings are nullified and hence
* ignored.
*
* @param string|null $campaignId
* @return self
*/
public function setCampaignId(?string $campaignId): SparkPost
{
$this->campaignId = (
is_string($campaignId)
? (substr($campaignId, 0, 64) ?: null)
: null
);

return $this;
}

/**
* Get SparkPost campaign ID to use
*
* @return string|null
*/
public function getCampaignId(): ?string
{
return $this->campaignId;
}

/**
* Set the global substitution variables to use with the template
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Service/SparkPostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function send(Message $message): array
$post['substitution_data'] = $message->getGlobalVariables();
}

if($message instanceof SparkPostMessage && $message->getCampaignId()) {
$post['campaign_id'] = $message->getCampaignId();
}

$response = $this->prepareHttpClient('/transmissions', $post)
->send()
;
Expand Down
42 changes: 42 additions & 0 deletions tests/SlmMailTest/Service/SparkPostServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,46 @@ public function testRemoveNonExistingAddressFromSuppressionList()
$sparkPostServiceMock->removeFromSuppressionList('[email protected]');
$this->doesNotPerformAssertions();
}

public function testSendBulkMail()
{
$message = $this->getMessageObject();
$message->addTo('[email protected]');
$message->addTo('[email protected]');

/** @var SparkPostService $sparkPostServiceMock */
$sparkPostServiceMock = $this->expectApiResponse(200);
$sparkPostServiceMock->send($message);
$this->doesNotPerformAssertions();
}

public function testCampaignId()
{
/** @var SparkPost $message */
$message = $this->getMessageObject();

// default value is null
$this->assertNull($message->getCampaignId());

// accepts null-value as a way to unset the Campaign ID
$message->setCampaignId('non-null-value');
$message->setCampaignId(null);
$this->assertNull($message->getCampaignId());

// nullify empty string
$message->setCampaignId('');
$this->assertNull($message->getCampaignId());

// regular use
$message->setCampaignId('sample-campaign');
$this->assertEquals('sample-campaign', $message->getCampaignId());

// truncation
$message->setCampaignId('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789');
$this->assertEquals('abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01', $message->getCampaignId());

// successful transmission injection
$sparkPostServiceMock = $this->expectApiResponse(200);
$sparkPostServiceMock->send($message);
}
}

0 comments on commit a8a3bbe

Please sign in to comment.