Skip to content

Commit

Permalink
Fixed issue with message decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sinemacula-ben committed Aug 12, 2024
1 parent 0490e7c commit ca1f9c5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Entities/Messages/Contracts/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aws\Sns\Message as BaseMessage;
use Carbon\Carbon;
use stdClass;

/**
* Message interface.
Expand Down Expand Up @@ -51,9 +52,9 @@ public function getTimestamp(): Carbon;
/**
* Return the message.
*
* @return array|string|null
* @return \stdClass
*/
public function getMessage(): array|string|null;
public function getMessage(): stdClass;

/**
* Return the signature version.
Expand Down
10 changes: 7 additions & 3 deletions src/Entities/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aws\Sns\Message as BaseMessage;
use Carbon\Carbon;
use SineMacula\Aws\Sns\Entities\Entity;
use stdClass;

/**
* The base AWS SNS message instance.
Expand All @@ -25,7 +26,10 @@ public function __construct(
protected BaseMessage $message

) {
parent::__construct($message->toArray());
parent::__construct([
...$message->toArray(),
'Message' => json_decode($message['Message'], true)
]);
}

/**
Expand Down Expand Up @@ -81,9 +85,9 @@ public function getTimestamp(): Carbon
/**
* Return the message.
*
* @return array|string|null
* @return \stdClass
*/
public function getMessage(): array|string|null
public function getMessage(): stdClass
{
return $this->attributes->Message;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Entities/Messages/S3/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SineMacula\Aws\Sns\Entities\Messages\Contracts\S3NotificationInterface;
use SineMacula\Aws\Sns\Entities\Messages\Notification as BaseNotification;
use stdClass;

/**
* AWS SNS S3 notification instance.
Expand All @@ -23,6 +24,6 @@ class Notification extends BaseNotification implements S3NotificationInterface
*/
public function getRecords(): array
{
return $this->records ??= array_map(fn (array $record) => new Record($record), $this->attributes->Message->Records ?? []);
return $this->records ??= array_map(fn (stdClass $record) => new Record($record), $this->getMessage()->Records ?? []);
}
}
20 changes: 12 additions & 8 deletions src/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ class MessageFactory
*/
public static function make(Message $message): MessageInterface
{
$message['Message'] = json_decode($message['Message'], true);

return match (true) {
self::isSubscriptionConfirmation($message) => new SubscriptionConfirmation($message),
self::isS3Notification($message) => new S3Notification($message),
self::isSesNotification($message) => new SesNotification($message),
self::isCloudWatchNotification($message) => new CloudWatchNotification($message),
default => throw new UnsupportedMessageException('Unsupported SNS message type.'),
default => throw new UnsupportedMessageException('Unsupported SNS message type: ' . $message['Type'] ?? 'Undefined')
};
}

Expand All @@ -58,7 +56,9 @@ private static function isSubscriptionConfirmation(Message $message): bool
*/
private static function isS3Notification(Message $message): bool
{
return isset($message['Message']['Records'][0]['s3']);
$message = json_decode($message['Message'], true);

return isset($message['Records'][0]['s3']);
}

/**
Expand All @@ -69,8 +69,10 @@ private static function isS3Notification(Message $message): bool
*/
private static function isSesNotification(Message $message): bool
{
return isset($message['Message']['notificationType'])
&& in_array($message['Message']['notificationType'], [
$message = json_decode($message['Message'], true);

return isset($message['notificationType'])
&& in_array($message['notificationType'], [
'Bounce',
'Complaint',
'Delivery'
Expand All @@ -85,7 +87,9 @@ private static function isSesNotification(Message $message): bool
*/
private static function isCloudWatchNotification(Message $message): bool
{
return isset($message['Message']['AlarmName'])
&& isset($message['Message']['NewStateValue']);
$message = json_decode($message['Message'], true);

return isset($message['AlarmName'])
&& isset($message['NewStateValue']);
}
}

0 comments on commit ca1f9c5

Please sign in to comment.