geoIPServiceURL = $geoIPServiceURL; $this->httpRequestFactory = $httpRequestFactory; } /** * {@inheritdoc} * @param string $ip The IP address to lookup * @return string|null */ public function locate( $ip ) { if ( isset( $this->locatedIPs[$ip] ) ) { return $this->locatedIPs[$ip]; } if ( !IPUtils::isValid( $ip ) ) { throw new InvalidArgumentException( "$ip is not a valid IP address." ); } if ( substr( $this->geoIPServiceURL, -1 ) !== '/' ) { $this->geoIPServiceURL .= '/'; } $json = $this->httpRequestFactory->get( $this->geoIPServiceURL . $ip, [ 'timeout' => '2', ] ); if ( !$json ) { return null; } $returnObject = json_decode( $json ); if ( $returnObject === null || !property_exists( $returnObject, 'country_code' ) ) { return null; } $this->locatedIPs[$ip] = $returnObject->country_code; return $this->locatedIPs[$ip]; } }