/** * @param ActionEvent $e */ public function __invoke(ActionEvent $e) { $message = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE); $handler = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER); if ($this->canInvoke($handler, $message)) { $this->invoke($handler, $message); } }
/** * @param ActionEvent $actionEvent */ public function onRoute(ActionEvent $actionEvent) { if ($this->authorizationService->isGranted($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME), $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE))) { return; } $actionEvent->stopPropagation(true); throw new UnauthorizedException(); }
/** * @param ActionEvent $event */ public function onErrorCommand(ActionEvent $event) { $exception = $event->getParam(MessageBus::EVENT_PARAM_EXCEPTION); $command = $event->getParam(MessageBus::EVENT_PARAM_MESSAGE); $data = $this->messageSerializer->serializeCommand($command); $data['success'] = false; if ($exception instanceof \Exception) { $data['exception'] = $this->messageSerializer->serializeException($exception); } $this->logger->error(json_encode($data)); }
/** * @param ActionEvent $actionEvent */ public function __invoke(ActionEvent $actionEvent) { $finder = $actionEvent->getParam(QueryBus::EVENT_PARAM_MESSAGE_HANDLER); $query = $actionEvent->getParam(QueryBus::EVENT_PARAM_MESSAGE); $deferred = $actionEvent->getParam(QueryBus::EVENT_PARAM_DEFERRED); if (is_object($finder)) { $queryName = $this->determineQueryName($query); if (method_exists($finder, $queryName)) { $finder->{$queryName}($query, $deferred); } } }
/** * @param ActionEvent $actionEvent */ public function onRoute(ActionEvent $actionEvent) { $messageName = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); if ($this->authorizationService->isGranted($messageName, $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE))) { return; } $actionEvent->stopPropagation(true); if (!$this->exposeEventMessageName) { $messageName = ''; } throw new UnauthorizedException($messageName); }
/** * @param ActionEvent $actionEvent */ public function onFinalize(ActionEvent $actionEvent) { $deferred = $actionEvent->getParam(self::EVENT_PARAM_DEFERRED); if ($deferred instanceof Deferred) { $deferred->promise()->done(function ($result) use($actionEvent, $deferred) { if (!$this->authorizationService->isGranted($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME), $result)) { $actionEvent->stopPropagation(true); throw new UnauthorizedException(); } }); } elseif (!$this->authorizationService->isGranted($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME))) { $actionEvent->stopPropagation(true); throw new UnauthorizedException(); } }
public function onLocateMessageHandler(ActionEvent $actionEvent) { $messageHandlerAlias = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER); if (is_string($messageHandlerAlias) && $this->serviceLocator->has($messageHandlerAlias)) { $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $this->serviceLocator->get($messageHandlerAlias)); } }
/** * @param ActionEvent $actionEvent * @throws UnauthorizedException */ public function onFinalize(ActionEvent $actionEvent) { $promise = $actionEvent->getParam(QueryBus::EVENT_PARAM_PROMISE); if ($promise instanceof Promise) { $newPromise = $promise->then(function ($result) use($actionEvent) { if (!$this->authorizationService->isGranted($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME), $result)) { $actionEvent->stopPropagation(true); throw new UnauthorizedException(); } }); $actionEvent->setParam(QueryBus::EVENT_PARAM_PROMISE, $newPromise); } elseif (!$this->authorizationService->isGranted($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME))) { $actionEvent->stopPropagation(true); throw new UnauthorizedException(); } }
/** * @param ActionEvent $actionEvent */ public function onInitializeEvent(ActionEvent $actionEvent) { $message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); if (is_string($message)) { $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_NAME, $message); } }
/** * Publish recorded events on the event bus * * @param ActionEvent $actionEvent */ public function onEventStoreCommitPost(ActionEvent $actionEvent) { $recordedEvents = $actionEvent->getParam('recordedEvents', new \ArrayIterator()); foreach ($recordedEvents as $recordedEvent) { $this->eventBus->dispatch($recordedEvent); } }
/** * Add event metadata on event store appendToStream. * * @param ActionEvent $appendToStreamEvent */ public function onEventStoreAppendToStream(ActionEvent $appendToStreamEvent) { $streamEvents = $appendToStreamEvent->getParam('streamEvents'); if (!$streamEvents instanceof \Iterator) { return; } $streamEvents = $this->handleRecordedEvents($streamEvents); $appendToStreamEvent->setParam('streamEvents', $streamEvents); }
/** * @param ActionEvent $event */ public function onDispatchInitialize(ActionEvent $event) { $bus = $event->getTarget(); if ($bus instanceof EventBus) { $listeners = $event->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []); $listeners[] = $this->messageProducer; $event->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, $listeners); } else { $event->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $this->messageProducer); } }
/** * @param ActionEvent $actionEvent */ public function __invoke(ActionEvent $actionEvent) { $message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); if (!is_array($message)) { return; } if (!array_key_exists('message_name', $message)) { return; } $messageName = $message['message_name']; unset($message['message_name']); $message = $this->messageFactory->createMessageFromArray($messageName, $message); $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE, $message); $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_NAME, $messageName); }
/** * @param ActionEvent $actionEvent */ public function onRouteMessage(ActionEvent $actionEvent) { $messageName = (string) $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); if (empty($messageName)) { return; } $message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); //if the message is marked with AsyncMessage, but had not yet been sent via async then sent to async producer if ($message instanceof AsyncMessage && !(isset($message->metadata()['handled-async']) && $message->metadata()['handled-async'] === true)) { //apply meta data, this is need to we can identify that the message has already been send via the async producer $message = $message->withAddedMetadata('handled-async', true); // update ActionEvent $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE, $message); if ($actionEvent->getTarget() instanceof CommandBus || $actionEvent->getTarget() instanceof QueryBus) { $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $this->asyncMessageProducer); } else { //Target is an event bus so we set message producer as the only listener of the message $actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, [$this->asyncMessageProducer]); } return; } // pass ActionEvent to decorated router return $this->router->onRouteMessage($actionEvent); }
/** * Take snapshots on event-store::commit.post * * @param ActionEvent $actionEvent */ public function onEventStoreCommitPost(ActionEvent $actionEvent) { $recordedEvents = $actionEvent->getParam('recordedEvents', []); $snapshots = []; foreach ($recordedEvents as $recordedEvent) { if ($recordedEvent->version() % $this->versionStep !== 0) { continue; } $metadata = $recordedEvent->metadata(); if (!isset($metadata['aggregate_type']) || !isset($metadata['aggregate_id'])) { continue; } $snapshots[$metadata['aggregate_type']][] = $metadata['aggregate_id']; } foreach ($snapshots as $aggregateType => $aggregateIds) { foreach ($aggregateIds as $aggregateId) { $command = TakeSnapshot::withData($aggregateType, $aggregateId); $this->commandBus->dispatch($command); } } }
/** * Take snapshots on event-store::commit.post * * @param ActionEvent $actionEvent */ public function onEventStoreCommitPost(ActionEvent $actionEvent) { $recordedEvents = $actionEvent->getParam('recordedEvents', new \ArrayIterator()); $snapshots = []; /* @var $recordedEvent \Prooph\Common\Messaging\Message */ foreach ($recordedEvents as $recordedEvent) { $doSnapshot = $recordedEvent->version() % $this->versionStep === 0; if (false === $doSnapshot && false === $this->hasEventNames) { continue; } $metadata = $recordedEvent->metadata(); if (!isset($metadata['aggregate_type'], $metadata['aggregate_id']) || false === $doSnapshot && !in_array($recordedEvent->messageName(), $this->eventNames, true)) { continue; } $snapshots[$metadata['aggregate_type']][] = $metadata['aggregate_id']; } foreach ($snapshots as $aggregateType => $aggregateIds) { foreach ($aggregateIds as $aggregateId) { $command = TakeSnapshot::withData($aggregateType, $aggregateId); $this->commandBus->dispatch($command); } } }
/** * @param ActionEvent $e */ public function onPostCommit(ActionEvent $e) { $this->loggedStreamEvents = $e->getParam('recordedEvents', []); }
/** * @param ActionEvent $event */ public function onDeleteTableRow(ActionEvent $event) { if ($event->getParam('item_type') === TestUser::class) { /** @var $query QueryBuilder */ $query = $event->getParam('query'); $data = $event->getParam('item_db_data'); $query->where($query->expr()->eq('name', ':name'))->setParameter('name', $data['name']); } }
/** * @param ActionEvent $actionEvent */ public function onRouteEvent(ActionEvent $actionEvent) { $messageName = (string) $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); if (empty($messageName)) { return; } if (!isset($this->eventMap[$messageName])) { return; } $listeners = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []); $listeners = array_merge($listeners, $this->eventMap[$messageName]); $actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, $listeners); }
/** * @param ActionEvent $actionEvent */ private function onRouteEvent(ActionEvent $actionEvent) { $messageName = (string) $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); if (empty($messageName)) { return; } foreach ($this->patternMap as $map) { list($pattern, $handler) = each($map); if (preg_match($pattern, $messageName)) { $listeners = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []); $listeners[] = $handler; $actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, $listeners); } } }
/** * @param ActionEvent $e */ public function onProcessDidFinish(ActionEvent $e) { if ($e->getParam('succeed')) { $this->processLogger->logProcessSucceed(ProcessId::fromString($e->getParam('process_id')), \DateTimeImmutable::createFromFormat(\DateTime::ISO8601, $e->getParam('finished_at'))); } else { $this->processLogger->logProcessFailed(ProcessId::fromString($e->getParam('process_id')), \DateTimeImmutable::createFromFormat(\DateTime::ISO8601, $e->getParam('finished_at'))); } }
/** * @param ActionEvent $event */ public function onProcessWasStartedByMessage(ActionEvent $event) { $this->messageLogger->logProcessStartedByMessage(ProcessId::fromString($event->getParam('process_id')), Uuid::fromString($event->getParam('message_id'))); }
/** * @param ActionEvent $actionEvent */ public function onRouteMessage(ActionEvent $actionEvent) { $messageName = (string) $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); if (empty($messageName)) { return; } if (!isset($this->messageMap[$messageName])) { return; } $handler = $this->messageMap[$messageName]; $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $handler); }
/** * Add event metadata on event store appendToStream * * @param ActionEvent $appendToStreamEvent */ public function onEventStoreAppendToStream(ActionEvent $appendToStreamEvent) { $streamEvents = $appendToStreamEvent->getParam('streamEvents'); $streamEvents = $this->handleRecordedEvents($streamEvents); $appendToStreamEvent->setParam('streamEvents', $streamEvents); }