/** * Execute the request * * @param MvcEvent $e * @return mixed * @throws Exception\DomainException */ public function onDispatch(MvcEvent $e) { $routeMatch = $e->getRouteMatch(); if (!$routeMatch) { /** * @todo Determine requirements for when route match is missing. * Potentially allow pulling directly from request metadata? */ throw new \Zend\Mvc\Exception\DomainException('Missing route matches; unsure how to retrieve action'); } $action = $routeMatch->getParam('action', null); if ($action) { $action = strtolower($action); } switch ($action) { case 'process': $message = $routeMatch->getParam('message', null); if ($message instanceof MessageInterface) { $result = $this->process($message); } else { throw new \InvalidArgumentException('Missing or invalid message type: must be an instace of ' . MessageInterface::class); } break; case 'send': case 'receive': case 'await': $queue = $routeMatch->getParam('queue', null); if (is_string($queue)) { $queue = $this->getServiceLocator()->get($queue); } if (!$queue instanceof QueueClientInterface) { throw new \InvalidArgumentException('Invalid queue param type: must be a string or an instace of ' . QueueClientInterface::class); } if ($action === 'send') { $message = $routeMatch->getParam('message', null); $parameters = $routeMatch->getParam('sendParameters', null); if ($parameters === null) { $params = null; } else { if ($parameters instanceof SendParametersInterface) { $params = $parameters; } else { if (is_string($parameters)) { $params = new SendParameters(); $params->fromString($parameters); } else { if (is_array($parameters)) { $params = new SendParameters(); $params->fromArray($parameters); } else { throw new \InvalidArgumentException('Invalid sendParameters param type: must be null, an array, a string or an instace of ' . SendParametersInterface::class); } } } } $result = $this->send($queue, $message, $params); } else { $parameters = $routeMatch->getParam('receiveParameters', null); if ($parameters === null) { $params = null; } else { if ($parameters instanceof ReceiveParametersInterface) { $params = $parameters; } else { if (is_string($parameters)) { $params = new ReceiveParameters(); $params->fromString($parameters); } else { if (is_array($parameters)) { $params = new ReceiveParameters(); $params->fromArray($parameters); } else { throw new \InvalidArgumentException('Invalid receiveParameters param type: must be null, an array, a string or an instace of ' . QueueClientInterface::class); } } } } if ($action === 'receive') { $maxMessages = (int) $routeMatch->getParam('maxMessages', 1); $result = $this->receive($queue, $maxMessages, $params); } else { $result = $this->await($queue, $params); } } break; default: throw new \InvalidArgumentException(sprintf('Invalid action "%s". Only "process", "send", "receive", "await" are allowed', $action)); } $e->setResult($result); return $result; }