Skip to content

Instantly share code, notes, and snippets.

View dgafka's full-sized avatar

Dariusz Gafka dgafka

View GitHub Profile
@dgafka
dgafka / asynchronous-messaging-13.php
Last active May 14, 2022 12:41
asynchronous-messaging-13.php
<?php
// Configuration on Publisher Side
public function changeBillingDetails(DistributedBus $distributedCommandBus)
{
$distributedCommandBus->sendCommand(
"billing", // destination
"billing.changeDetails", // routingKey
'["personId":"123","billingDetails":"01111"]',
"application/json"
@dgafka
dgafka / asynchronous-messaging-12.php
Last active May 13, 2022 17:43
asynchronous-messaging-12.php
<?php
class PlaceOrdernHandler
{
// default behaviour, first parameter is payload, second are headers
#[CommandHandler("place_order")]
public function placeOrder(PlaceOrder $payload, array $headers) {}
}
@dgafka
dgafka / asynchronous-messaging-11.php
Last active May 13, 2022 17:34
asynchronous-messaging-11.php
<?php
class MessagingConfiguration
{
#[ServiceContext]
public function asyncChannel()
{
// this channel can be reused for different message handlers
return AmqpBackedMessageChannelBuilder::create("async_channel");
}
@dgafka
dgafka / asynchronous-messaging-10.php
Created May 13, 2022 17:24
asynchronous-messaging-10.php
<?php
class AddExecutorIdMiddleware
{
/**
* @param mixed $job
* @param callable $next
* @return mixed
*/
public function handle($job, $next)
@dgafka
dgafka / asynchronous-messaging-09.php
Created May 13, 2022 17:18
asynchronous-messaging-09.php
<?php
PlaceOrder::dispatch($order)->onQueue('async_channel');
@dgafka
dgafka / asynchronous-messaging-08.php
Created May 13, 2022 17:01
asynchronous-messaging-08.php
<?php
class PlaceOrder implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
protected Order $order;
public function __construct(Order $order)
{
@dgafka
dgafka / asynchronous-messaging-07.php
Last active May 13, 2022 06:48
asynchronous-messaging-07.php
<?php
class AddExecutorIdInterceptor
{
#[Before(pointcut: CommandHandler::class, changeHeaders: true)]
public function addExecutorId(PlaceOrder $payload, array $headers): array
{
// changeHeaders: true/false allows for changing headers or payload.
// you don't need to pass parameters, if you don't want to use them.
@dgafka
dgafka / asynchronous-messaging-06.php
Last active May 13, 2022 17:45
asynchronous-messaging-06.php
<?php
$commandBus->sendWithRouting("place_order", '{"items":["milk","coffee"]}', "application/json")
(..)
class PlaceOrdernHandler
{
#[CommandHandler("place_order")]
public function placeOrder(PlaceOrder $payload) {}
}
@dgafka
dgafka / asynchronous-messaging-05.php
Last active May 13, 2022 17:45
asynchronous-messaging-05.php
<?php
class PlaceOrdernHandler
{
#[CommandHandler]
public function placeOrder(PlaceOrder $payload) {}
}
@dgafka
dgafka / asynchronous-messaging-04.php
Last active May 13, 2022 06:40
asynchronous-messaging-04.php
<?php
$bus->dispatch(new PlaceOrder(), [new ExecutorStamp($executorId)]);
(..)
class AuthorizeExecutorMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{