/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
throw new RuntimeException('Geolocate middleware needs ClientIp executed before');
}
$ip = ClientIp::getIp($request);
if ($ip !== null) {
$request = Middleware::setAttribute($request, self::KEY, $this->geocoder->geocode($ip));
}
return $next($request, $response);
}