-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
491 additions
and
358 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client; | ||
|
||
trait EmptyExtensionTrait | ||
{ | ||
public function onPreSendEvent(PreSend $context): void | ||
{ | ||
} | ||
|
||
public function onPreSendCommand(PreSend $context): void | ||
{ | ||
} | ||
|
||
public function onPreDriverSend(PreDriverSend $context): void | ||
{ | ||
} | ||
|
||
public function onPostSend(PostSend $context): void | ||
{ | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client\Extension; | ||
|
||
use Enqueue\Client\EmptyExtensionTrait; | ||
use Enqueue\Client\ExtensionInterface; | ||
use Enqueue\Client\Message; | ||
use Enqueue\Client\PreSend; | ||
use Enqueue\Util\JSON; | ||
|
||
class PrepareBodyExtension implements ExtensionInterface | ||
{ | ||
use EmptyExtensionTrait; | ||
|
||
public function onPreSendEvent(PreSend $context): void | ||
{ | ||
$this->prepareBody($context->getMessage()); | ||
} | ||
|
||
public function onPreSendCommand(PreSend $context): void | ||
{ | ||
$this->prepareBody($context->getMessage()); | ||
} | ||
|
||
private function prepareBody(Message $message): void | ||
{ | ||
$body = $message->getBody(); | ||
$contentType = $message->getContentType(); | ||
|
||
if (is_scalar($body) || null === $body) { | ||
$contentType = $contentType ?: 'text/plain'; | ||
$body = (string) $body; | ||
} elseif (is_array($body)) { | ||
// only array of scalars is allowed. | ||
array_walk_recursive($body, function ($value) { | ||
if (!is_scalar($value) && null !== $value) { | ||
throw new \LogicException(sprintf( | ||
'The message\'s body must be an array of scalars. Found not scalar in the array: %s', | ||
is_object($value) ? get_class($value) : gettype($value) | ||
)); | ||
} | ||
}); | ||
|
||
$contentType = $contentType ?: 'application/json'; | ||
$body = JSON::encode($body); | ||
} elseif ($body instanceof \JsonSerializable) { | ||
$contentType = $contentType ?: 'application/json'; | ||
$body = JSON::encode($body); | ||
} else { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The message\'s body must be either null, scalar, array or object (implements \JsonSerializable). Got: %s', | ||
is_object($body) ? get_class($body) : gettype($body) | ||
)); | ||
} | ||
|
||
$message->setContentType($contentType); | ||
$message->setBody($body); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client; | ||
|
||
class PostSend | ||
{ | ||
private $message; | ||
|
||
private $producer; | ||
|
||
private $driver; | ||
|
||
public function __construct(Message $message, ProducerInterface $producer, DriverInterface $driver) | ||
{ | ||
$this->message = $message; | ||
$this->producer = $producer; | ||
$this->driver = $driver; | ||
} | ||
|
||
public function getMessage(): Message | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getProducer(): ProducerInterface | ||
{ | ||
return $this->producer; | ||
} | ||
|
||
public function getDriver(): DriverInterface | ||
{ | ||
return $this->driver; | ||
} | ||
|
||
public function isEvent(): bool | ||
{ | ||
return Config::COMMAND_TOPIC !== $this->message->getProperty(Config::PARAMETER_TOPIC_NAME); | ||
} | ||
|
||
public function getCommand(): string | ||
{ | ||
return $this->message->getProperty(Config::PARAMETER_COMMAND_NAME); | ||
} | ||
|
||
public function getTopic(): string | ||
{ | ||
return $this->message->getProperty(Config::PARAMETER_TOPIC_NAME); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client; | ||
|
||
class PreDriverSend | ||
{ | ||
private $message; | ||
|
||
private $producer; | ||
|
||
private $driver; | ||
|
||
public function __construct(Message $message, ProducerInterface $producer, DriverInterface $driver) | ||
{ | ||
$this->message = $message; | ||
$this->producer = $producer; | ||
$this->driver = $driver; | ||
} | ||
|
||
public function getMessage(): Message | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getProducer(): ProducerInterface | ||
{ | ||
return $this->producer; | ||
} | ||
|
||
public function getDriver(): DriverInterface | ||
{ | ||
return $this->driver; | ||
} | ||
|
||
public function isEvent(): bool | ||
{ | ||
return Config::COMMAND_TOPIC !== $this->message->getProperty(Config::PARAMETER_TOPIC_NAME); | ||
} | ||
|
||
public function getCommand(): string | ||
{ | ||
return $this->message->getProperty(Config::PARAMETER_COMMAND_NAME); | ||
} | ||
|
||
public function getTopic(): string | ||
{ | ||
return $this->message->getProperty(Config::PARAMETER_TOPIC_NAME); | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
namespace Enqueue\Client; | ||
|
||
class PreSend | ||
{ | ||
private $message; | ||
|
||
private $originalMessage; | ||
|
||
private $commandOrTopic; | ||
|
||
private $producer; | ||
|
||
private $driver; | ||
|
||
public function __construct( | ||
string $commandOrTopic, | ||
Message $message, | ||
ProducerInterface $producer, | ||
DriverInterface $driver | ||
) { | ||
$this->message = $message; | ||
$this->commandOrTopic = $commandOrTopic; | ||
$this->producer = $producer; | ||
$this->driver = $driver; | ||
|
||
$this->originalMessage = clone $message; | ||
} | ||
|
||
public function getCommandOrTopic(): string | ||
{ | ||
return $this->commandOrTopic; | ||
} | ||
|
||
public function changeCommandOrTopic(string $commandOrTopic): void | ||
{ | ||
$this->commandOrTopic = $commandOrTopic; | ||
} | ||
|
||
public function changeBody($body, string $contentType = null): void | ||
{ | ||
$this->message->setBody($body); | ||
|
||
if (null !== $contentType) { | ||
$this->message->setContentType($contentType); | ||
} | ||
} | ||
|
||
public function getMessage(): Message | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getOriginalMessage(): Message | ||
{ | ||
return $this->originalMessage; | ||
} | ||
|
||
public function getProducer(): ProducerInterface | ||
{ | ||
return $this->producer; | ||
} | ||
|
||
public function getDriver(): DriverInterface | ||
{ | ||
return $this->driver; | ||
} | ||
} |
Oops, something went wrong.