Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active May 18, 2016 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/f3519ad7a1df19ac38dc3649ff54bb79 to your computer and use it in GitHub Desktop.
Save webdevilopers/f3519ad7a1df19ac38dc3649ff54bb79 to your computer and use it in GitHub Desktop.
How to handle events from recorder with SimpleBus in Symfony
<?php
class AddDefectHandler
implements MessageHandler
{
/**
* @var RecordsMessages $eventRecorder
*/
private $eventRecorder;
public function handle(Message $command)
{
$this->defectRepository->save($defect);
$defectAdded = new DefectAdded(new DefectTypeId($defect->id()));
$this->eventRecorder->record($defectAdded);
}
}
<?php
class DefectController extends Controller
{
public function addDefectAction(Request $request, Contract $order)
{
$command = AddOrderDefectCommand::fromOrder($order);
$handler->handle($command);
$defectAdded = new DefectAdded(new DefectTypeId(1));
$eventBus = $this->get('event_bus');
$eventBus->handle($defectAdded); // works fine
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="qms.subscriber.defect_added"
class="Acme\Application\Services\DefectAddedSubscriber">
<tag name="event_subscriber" subscribes_to="Acme\DomainModel\Defect\DefectAdded" />
</service>
</services>
</container>
@Axxon
Copy link

Axxon commented May 18, 2016

@webdevilopers Hello, into my example that you quoted, after time between write and reading, i think i made a mistake, the end message should be "i'm a asynchronous command"!, so good question. Commands are dealed asynchronously then related events are synchronous ... So i will check that this week-end.

@webdevilopers
Copy link
Author

Thanks for the feedback @Axxon. Looking forward to your answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment