getHandler($cmd); $resp = $handler->handle($cmd); return $resp; } /** * Method that retrieves the handler for a given command/query object. * * @param object $cmd The command or query to retrieve a handler for. * @return object The handler for the given command/query. * @throws NoHandlerFoundException If no appropriate handler is found. */ private function getHandler(object $cmd): object { $reflection = new ReflectionObject($cmd); while ($reflection) { $attributes = $reflection->getAttributes(Handler::class); foreach ($attributes as $attribute) { $ins = $attribute->newInstance(); $className = $ins->className; // Retrieves the handler for the given command/query // from the container. return $this->container->get($className); } $reflection = $reflection->getParentClass(); } throw new NoHandlerFoundException($cmd); } }