Skip to content

Commit

Permalink
Add a factory method to Message to create from PSR-7 Requests (#20)
Browse files Browse the repository at this point in the history
* Add a factory method to Message to create from PSR-7 Requests

* Direct Travis to use Trusty instead of Precise

* Do not require that PSR-7 requests have an SNS type header

* Revert X_AMZ_SNS_MESSAGE_TYPE header usage to its previous state
  • Loading branch information
jeskew authored Jul 12, 2017
1 parent 239d80e commit 5135ef7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

sudo: false
dist: trusty

install:
- travis_retry composer update --no-interaction --prefer-dist
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sns-message-validator/issues"
},
"suggest": {
"aws/aws-sdk-php": "The official Amazon Web Services PHP SDK."
},
"require": {
"php": ">=5.4",
"ext-openssl": "*"
"ext-openssl": "*",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"squizlabs/php_codesniffer": "^2.3"
"squizlabs/php_codesniffer": "^2.3",
"guzzlehttp/psr7": "^1.4"
},
"autoload": {
"psr-4": { "Aws\\Sns\\": "src/" }
Expand Down
30 changes: 27 additions & 3 deletions src/Message.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Aws\Sns;

use Psr\Http\Message\RequestInterface;

/**
* Represents an SNS message received over http(s).
*/
Expand All @@ -21,7 +23,7 @@ class Message implements \ArrayAccess, \IteratorAggregate
private $data;

/**
* Creates a message object from the raw POST data
* Creates a Message object from the raw POST data
*
* @return Message
* @throws \RuntimeException If the POST data is absent, or not a valid JSON document
Expand All @@ -33,8 +35,30 @@ public static function fromRawPostData()
throw new \RuntimeException('SNS message type header not provided.');
}

// Read the raw POST data and JSON-decode it.
$data = json_decode(file_get_contents('php://input'), true);
// Read the raw POST data and JSON-decode it into a message.
return self::fromJsonString(file_get_contents('php://input'));
}

/**
* Creates a Message object from a PSR-7 Request or ServerRequest object.
*
* @param RequestInterface $request
* @return Message
*/
public static function fromPsrRequest(RequestInterface $request)
{
return self::fromJsonString($request->getBody());
}

/**
* Creates a Message object from a JSON-decodable string.
*
* @param string $requestBody
* @return Message
*/
private static function fromJsonString($requestBody)
{
$data = json_decode($requestBody, true);
if (JSON_ERROR_NONE !== json_last_error() || !is_array($data)) {
throw new \RuntimeException('Invalid POST data.');
}
Expand Down
30 changes: 29 additions & 1 deletion tests/MessageTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace Aws\Sns;

use GuzzleHttp\Psr7\Request;

/**
* @covers Aws\Sns\Message
* @covers \Aws\Sns\Message
*/
class MessageTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -141,6 +143,32 @@ public function testCreateFromRawPostFailsWithMissingData()
unset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE']);
}

public function testCanCreateFromPsr7Request()
{
$request = new Request(
'POST',
'/',
[],
json_encode($this->messageData)
);
$message = Message::fromPsrRequest($request);
$this->assertInstanceOf('Aws\Sns\Message', $message);
}

/**
* @expectedException \RuntimeException
*/
public function testCreateFromPsr7RequestFailsWithMissingData()
{
$request = new Request(
'POST',
'/',
[],
'Not valid JSON'
);
Message::fromPsrRequest($request);
}

public function testArrayAccess()
{
$message = new Message($this->messageData);
Expand Down

0 comments on commit 5135ef7

Please sign in to comment.