Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-28] Allow setting message streams transport-wide or per message #29

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Postmark/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Transport implements Swift_Transport {
*/
protected $serverToken;

/**
* The Postmark Message Stream Identifier
*
* @var string|null
*/
protected $messageStream;

/**
* @var \Swift_Events_EventDispatcher
*/
Expand All @@ -28,11 +35,13 @@ class Transport implements Swift_Transport {
/**
* Create a new Postmark transport instance.
*
* @param string $serverToken The API token for the server from which you will send mail.
* @param string $serverToken The API token for the server from which you will send mail.
* @param string $messageStream The name of the broadcast stream or the default if not provided
* @return void
*/
public function __construct($serverToken) {
public function __construct($serverToken, $messageStream = null) {
$this->serverToken = $serverToken;
$this->messageStream = $messageStream;
$this->version = phpversion();
$this->os = PHP_OS;
$this->_eventDispatcher = \Swift_DependencyContainer::getInstance()->lookup('transport.eventdispatcher');
Expand Down Expand Up @@ -175,6 +184,8 @@ private function getMessagePayload(Swift_Mime_SimpleMessage $message) {
$this->processHeaders($payload, $message);
}

$this->processMessageStream($payload, $message);

return $payload;
}

Expand Down Expand Up @@ -297,6 +308,22 @@ private function processHeaders(&$payload, $message) {
$payload['Headers'] = $headers;
}

/**
* Applies the message stream into the API payload.
*
* @param array $payload
* @param Swift_Mime_SimpleMessage $message
* @return void
*/
private function processMessageStream(&$payload, $message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't make anything private, you don't know in what ways other users/customers need to customize things.

Suggested change
private function processMessageStream(&$payload, $message)
protected function processMessageStream(&$payload, $message)

See also #33

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods in this code are all mostly private, i keep to the existing style. No need to combine #33 into this here.

{
if ($message->getHeaders() && $message->getHeaders()->has('X-PM-Message-Stream')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the discussion in #20 about not adding special handling and detecting this in the API instead. However in this csae its necessary, if you have a message stream set as default, then it would be present in the API request, and probably not overwritten by the header that is sent as well.

$payload['MessageStream'] = $message->getHeaders()->get('X-PM-Message-Stream', 0)->getFieldBody();
} else if ($this->messageStream !== NULL) {
$payload['MessageStream'] = $this->messageStream;
}
}

/**
* {@inheritdoc}
*/
Expand Down
41 changes: 39 additions & 2 deletions tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,43 @@ public function testToAndCcCanBeNullableEvents()
$this->assertEquals('"A. Friend" <you@example.com>,"B. Friend" <other@example.com>', $body['Bcc']);
}

public function testMessageStreamViaConstructorOption()
{
$message = new Swift_Message();
$message->setFrom('johnny5@example.com', 'Johnny #5');
$message->setSubject('Some Subject');
$message->addBcc('you@example.com', 'A. Friend');
$message->addBcc('other@example.com', 'B. Friend');

$transport = new PostmarkTransportStub([new Response(200)], 'broadcasts');
$transport->send($message);

$request = $transport->getHistory()[0]['request'];
$body = json_decode($request->getBody()->getContents(), true);

$this->assertArrayHasKey('MessageStream', $body);
$this->assertEquals('broadcasts', $body['MessageStream']);
}

public function testMessageStreamViaHeader()
{
$message = new Swift_Message();
$message->setFrom('johnny5@example.com', 'Johnny #5');
$message->setSubject('Some Subject');
$message->addBcc('you@example.com', 'A. Friend');
$message->addBcc('other@example.com', 'B. Friend');
$message->getHeaders()->addTextHeader('X-PM-Message-Stream', 'broadcasts');

$transport = new PostmarkTransportStub([new Response(200)]);
$transport->send($message);

$request = $transport->getHistory()[0]['request'];
$body = json_decode($request->getBody()->getContents(), true);

$this->assertArrayHasKey('MessageStream', $body);
$this->assertEquals('broadcasts', $body['MessageStream']);
}

public function testServerTokenReturnedFromPublicMethod()
{
$transport = new PostmarkTransportStub();
Expand All @@ -166,9 +203,9 @@ public function testFailedResponse()
class PostmarkTransportStub extends Postmark\Transport {
protected $client;

public function __construct(array $responses = [])
public function __construct(array $responses = [], $messageStream = null)
{
parent::__construct('TESTING_SERVER');
parent::__construct('TESTING_SERVER', $messageStream);

$this->client = $this->mockGuzzle($responses);
}
Expand Down