/** * @interitdoc */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $eventName = $request->getAttribute(self::NAME_ATTRIBUTE); if (null === $eventName) { return $next($request, $response, new RuntimeException(sprintf('Event name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE), Middleware::STATUS_CODE_BAD_REQUEST)); } try { $event = $this->eventFactory->createMessageFromArray($eventName, ['payload' => $request->getParsedBody(), 'metadata' => $this->metadataGatherer->getFromRequest($request)]); $this->eventBus->dispatch($event); return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED); } catch (\Exception $e) { return $next($request, $response, new RuntimeException(sprintf('An error occurred during dispatching of event "%s"', $eventName), Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR, $e)); } }
/** * @interitdoc */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $queryName = $request->getAttribute(self::NAME_ATTRIBUTE); if (null === $queryName) { return $next($request, $response, new RuntimeException(sprintf('Query name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE), Middleware::STATUS_CODE_BAD_REQUEST)); } $payload = $request->getQueryParams(); if ($request->getMethod() === 'POST') { $payload['data'] = $request->getParsedBody(); } try { $query = $this->queryFactory->createMessageFromArray($queryName, ['payload' => $payload, 'metadata' => $this->metadataGatherer->getFromRequest($request)]); return $this->responseStrategy->fromPromise($this->queryBus->dispatch($query)); } catch (\Exception $e) { return $next($request, $response, new RuntimeException(sprintf('An error occurred during dispatching of query "%s"', $queryName), Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR, $e)); } }