-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DE-111852 Add support for custom SQS message attributes - part 02 (#103)
* DE-111852 Add support for custom SQS message attributes - part 02 * DE-111852 Leverage nullsafe operator when dispatching events * DE-111852 Refactor SqsMessageAttribute into a class
- Loading branch information
1 parent
761730b
commit 34910c9
Showing
16 changed files
with
609 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BE\QueueManagement\Observability; | ||
|
||
use BE\QueueManagement\Jobs\JobInterface; | ||
|
||
class BeforeMessageSentEvent | ||
{ | ||
public function __construct( | ||
public readonly JobInterface $job, | ||
public readonly int $delayInSeconds, | ||
public readonly string $prefixedQueueName, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BE\QueueManagement\Queue\AWSSQS; | ||
|
||
use function is_numeric; | ||
use function str_contains; | ||
use function strlen; | ||
|
||
/** | ||
* Represent SQS Message Attribute | ||
* | ||
* @final | ||
*/ | ||
class SqsMessageAttribute | ||
{ | ||
public function __construct( | ||
private readonly string $name, | ||
private readonly string $value, | ||
private readonly SqsMessageAttributeDataType $dataType, | ||
) { | ||
} | ||
|
||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
|
||
public function getValue(): string|int|float | ||
{ | ||
if ($this->dataType === SqsMessageAttributeDataType::NUMBER) { | ||
if (is_numeric($this->value)) { | ||
if (str_contains($this->value, '.')) { | ||
return (float)$this->value; | ||
} | ||
|
||
return (int)$this->value; | ||
} | ||
} | ||
|
||
return $this->value; | ||
} | ||
|
||
|
||
public function getDataType(): SqsMessageAttributeDataType | ||
{ | ||
return $this->dataType; | ||
} | ||
|
||
|
||
public function getSizeInBytes(): int | ||
{ | ||
return strlen($this->dataType->value) + strlen($this->value); | ||
} | ||
|
||
|
||
/** | ||
* @return array{DataType: 'String'|'Number', StringValue: string}|array{DataType: 'Binary', BinaryValue: string} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
if ($this->dataType === SqsMessageAttributeDataType::BINARY) { | ||
return [ | ||
SqsMessageAttributeFields::DATA_TYPE->value => $this->dataType->value, | ||
SqsMessageAttributeFields::BINARY_VALUE->value => $this->value, | ||
]; | ||
} | ||
|
||
return [ | ||
SqsMessageAttributeFields::DATA_TYPE->value => $this->dataType->value, | ||
SqsMessageAttributeFields::STRING_VALUE->value => $this->value, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BE\QueueManagement\Queue\AWSSQS; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class SqsMessageAttributeFactory | ||
{ | ||
/** | ||
* @param array{DataType: 'String'|'Number', StringValue: string}|array{DataType: 'Binary', BinaryValue: string} $value | ||
*/ | ||
public function createFromArray(string $name, array $value): SqsMessageAttribute | ||
{ | ||
$dataType = SqsMessageAttributeDataType::from($value[SqsMessageAttributeFields::DATA_TYPE->value]); | ||
$valueKey = $dataType === SqsMessageAttributeDataType::BINARY | ||
? SqsMessageAttributeFields::BINARY_VALUE->value | ||
: SqsMessageAttributeFields::STRING_VALUE->value; | ||
|
||
return new SqsMessageAttribute( | ||
$name, | ||
$value[$valueKey], | ||
$dataType, | ||
); | ||
} | ||
} |
Oops, something went wrong.