Skip to content

Instantly share code, notes, and snippets.

@ismail1432
Last active October 2, 2022 12:29
Show Gist options
  • Save ismail1432/92f24c8da02f19ad2093bf61013e6a4e to your computer and use it in GitHub Desktop.
Save ismail1432/92f24c8da02f19ad2093bf61013e6a4e to your computer and use it in GitHub Desktop.
<?php
// FooPreHandler.php
class FooPreHandler implements PreHandlerInterface
{
💡 // As it's a service you can inject what you want
public function __construct(DummyService $service) {}
public function preHandle(object $message)
{
// Stuff you want to do before handle the message
dump('We are in '.__CLASS__.' in method: '.__FUNCTION__);
}
// We want to call `preHandle` only if $message
// is an instance of `MyMessage`
public function supports(object $message): bool
{
return $message instanceof MyMessage;
}
}
// SecondFooPreHandler.php
class SecondFooPreHandler implements PreHandlerInterface
{
💡 // As it's a service you can inject what you want
public function __construct(DummyService $service) {}
public function preHandle(object $message)
{
// Stuff you want to do before handle the message
dump('We are in '.__CLASS__.' in method: '.__FUNCTION__);
}
// We want to call `preHandle` only if $message
// is an instance of `MyMessage`
public function supports(object $message): bool
{
return $message instanceof MyMessage;
}
}
// FooPostHandler.php
class FooPostHandler implements PostHandlerInterface
{
💡 // As it's a service you can inject what you want
public function __construct(DummyService $service) {}
public function postHandle(object $message, $result)
{
// Stuff you want to do with the result and the message after it was handled.
dump(
'We are in '.__CLASS__.' in method: '.__FUNCTION__,
// let's make the dump more verbose with $result and $message
[
'message' => $message,
'result' => $result
]
);
}
// We want to call `postHandle` only if $message
// is an instance of `MyMessage`
public function supports(object $message): bool
{
return $message instanceof MyMessage;
}
}
// NotCalledPostHandler.php
class NotCalledPostHandler implements PostHandlerInterface
{
💡 // As it's a service you can inject what you want
public function __construct(DummyService $service) {}
public function postHandle(object $message, $result)
{
// Stuff you want to do with the result and the message after it was handled.
dump(
'We are in '.__CLASS__.' in method: '.__FUNCTION__,
[
'message' => $message,
'result' => $result
]
);
}
// We return `false` to check that `postHandle` should not be called.
public function supports(object $message): bool
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment