$eventStore = new \Broadway\EventStore\InMemoryEventStore(); $eventBus = new \Broadway\EventHandling\SimpleEventBus(); // Subscribe your things here $eventListener = new MyEventListener(); $eventBus->subscribe($eventListener); // Setup the command handler $marriageRepository = MarriageRepository::parent($eventStore, $eventBus); $commandHandler = new MarriageCommandHandler($marriageRepository); // Create a command bus and subscribe the command handler at the command bus $commandBus = new \Broadway\CommandHandling\SimpleCommandBus(); $commandBus->subscribe($commandHandler); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Data for the Command & Marriage $generator = new \Broadway\UuidGenerator\Rfc4122\Version4Generator(); // @TODO: FLUSH OUT THESE EXAMPLES $partnerId_1 = $generator->generate(); $partnerId_2 = $generator->generate(); $partnerId_3 = $generator->generate(); $partnerId_4 = $generator->generate(); $partnerId_5 = $generator->generate(); $partnerId_6 = $generator->generate(); $partnerId_7 = $generator->generate(); $marriageId = $generator->generate(); // would think more like `execute` // Create and dispatch the command! $occurredOn = time(); // or as new DateTime() // 2 $command1 = new CreateMarriageCommand($marriageId, [$partnerId_1, $partnerId_2], $occurredOn); // 2 different ones $command2 = new CreateMarriageCommand($marriageId, [$partnerId_3, $partnerId_4], $occurredOn);
/** * @param Application $app * @return Response */ public function connect(Application $app) { $app['entry_api.command_handler'] = $app->share(function (Application $app) { return new SecurityDecoratedCommandHandler(new EntryAPIEventCommandHandler($app['event_repository']), $app['event.security']); }); /** @var ControllerCollection $controllers */ $controllers = $app['controllers_factory']; $app['entryapi_event_controller'] = $app->share(function (Application $app) { $controller = new EventController($app['event_repository'], $app['entryapi.link_base_url']); return $controller; }); $controllers->get('event/checkpermission', function (Request $request, Application $app) { /** @var String[] $eventIds */ $eventIds = []; if (!empty($request->query->get('ids'))) { $eventIds = explode(",", $request->query->get('ids')); $eventIds = array_filter($eventIds, function ($item) { return trim($item) !== ''; }); $eventIds = array_map(function ($cdbid) { return new String($cdbid); }, $eventIds); } $uitId = $request->query->get('user'); /** @var PermissionQueryInterface $repository */ $repository = $app['event_permission.repository']; $editableEvents = $repository->getEditableEvents(new String($uitId)); if (empty($eventIds)) { $eventIds = $editableEvents; } /** @var EventPermission[] $eventPermissions */ $eventPermissions = array_map(function (string $cdbid) use($editableEvents) { $isEditable = in_array($cdbid, $editableEvents); return new EventPermission($cdbid->toNative(), $isEditable); }, $eventIds); return $this->createPermissionResponse(new EventPermissionCollection($eventPermissions)); }); $controllers->post('/event', function (Request $request, Application $app) { $callback = function () use($request, $app) { if ($request->getContentType() !== 'xml') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not XML.'); return $this->createResponse($rsp); } $xml = new SizeLimitedEventXmlString($request->getContent()); /** @var \DOMElement $eventElement */ $eventElement = $xml->eventElement(); $id = $eventElement->getAttribute('cdbid'); if ($id) { // First try to retrieve the event from the JSON-LD read model. // This will result in a EventNotFoundException if the event // does not exist. /** @var \CultuurNet\UDB3\EventServiceInterface $service */ $service = $app['event_service']; $service->getEvent($id); $command = new UpdateEventFromCdbXml(new String($id), $xml); } else { $uuidGenerator = new \Broadway\UuidGenerator\Rfc4122\Version4Generator(); $id = $uuidGenerator->generate(); $command = new AddEventFromCdbXml(new String($id), $xml); } $commandHandler = $app['entry_api.command_handler']; $commandHandler->handle($command); $link = $app['entryapi.link_base_url'] . $id; $status = $command instanceof UpdateEventFromCdbXml ? 'ItemModified' : 'ItemCreated'; $rsp = new Rsp('0.1', 'INFO', $status, $link, null); return $rsp; }; return $this->processEventRequest($callback); }); $controllers->post('/event/{cdbid}/keywords', function (Request $request, Application $app, $cdbid) { $callback = function () use($request, $app, $cdbid) { $repository = $app['event_repository']; if ($request->getContentType() !== 'form') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not x-www-form-urlencoded.'); return $rsp; } $keywordsString = new String($request->request->get('keywords', '')); $visiblesString = new String($request->request->get('visibles', '')); $keywordsVisiblesPair = new KeywordsVisiblesPair($keywordsString, $visiblesString); $eventId = new String($cdbid); $command = new MergeLabels($eventId, $keywordsVisiblesPair->getLabels()); $commandHandler = new EntryAPIEventCommandHandler($repository); $commandHandler->handle($command); $link = $app['entryapi.link_base_url'] . $cdbid; $rsp = new Rsp('0.1', 'INFO', 'KeywordsCreated', $link, null); return $rsp; }; return $this->processEventRequest($callback); }); $controllers->delete('/event/{cdbid}/keywords', 'entryapi_event_controller:deleteKeyword'); $controllers->post('/event/{cdbid}/translations', 'entryapi_event_controller:translate'); $controllers->delete('/event/{cdbid}/translations', 'entryapi_event_controller:deleteTranslation'); $controllers->post('/event/{cdbid}/links', 'entryapi_event_controller:addLink'); $putCallback = function (Request $request, Application $app, $cdbid) { $callback = function () use($request, $app, $cdbid) { // First try to retrieve the event from the JSON-LD read model. // This will result in a EventNotFoundException if the event // does not exist. /** @var \CultuurNet\UDB3\EventServiceInterface $service */ $service = $app['event_service']; $service->getEvent($cdbid); if ($request->getContentType() !== 'xml') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not XML.'); return $this->createResponse($rsp); } $xml = new SizeLimitedEventXmlString($request->getContent()); $eventId = new String($cdbid); $command = new UpdateEventFromCdbXml($eventId, $xml); $commandHandler = $app['entry_api.command_handler']; $commandHandler->handle($command); $link = $app['entryapi.link_base_url'] . $eventId; $rsp = new Rsp('0.1', 'INFO', 'ItemModified', $link, null); return $rsp; }; return $this->processEventRequest($callback); }; $controllers->put('/event/{cdbid}', $putCallback); // Culturefeed Entry UI Drupal module seems to use POST instead of PUT. $controllers->post('/event/{cdbid}', $putCallback); return $controllers; }