<commit_before><?php namespace Torann\GeoIP;

use GeoIp2\Database\Reader;
use Illuminate\Config\Repository;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Session\Store as SessionStore;

class GeoIP {

	/**
	 * The session store.
	 *
	 * @var \Illuminate\Session\Store
	 */
	protected $session;

    /**
     * Illuminate config repository instance.
     *
     * @var \Illuminate\Config\Repository
     */
    protected $config;

	/**
	 * Remote Machine IP address.
	 *
	 * @var float
	 */
	protected $remote_ip = null;

	/**
	 * Location data.
	 *
	 * @var array
	 */
	protected $location = null;

	/**
	 * Reserved IP address.
	 *
	 * @var array
	 */
	protected $reserved_ips = array (
		array('0.0.0.0','192.168.3.11'),
		array('10.0.0.0','10.255.255.255'),
		array('127.0.0.0','127.255.255.255'),
		array('169.254.0.0','169.254.255.255'),
		array('172.16.0.0','172.31.255.255'),
		array('192.0.2.0','192.0.2.255'),
		array('192.168.0.0','192.168.255.255'),
		array('255.255.255.0','255.255.255.255')
	);

	/**
	 * Default Location data.
	 *
	 * @var array
	 */
	protected $default_location = array (
		"ip" 			=> "127.0.0.0",
		"isoCode" 		=> "US",
		"country" 		=> "United States",
		"city" 			=> "New Haven",
		"state" 		=> "CT",
		"postal_code" 	=> "06510",
		"lat" 			=> 41.31,
		"lon" 			=> -72.92
	);

	/**
	 * Create a new GeoIP instance.
	 *
     * @param  \Illuminate\Config\Repository  $config
	 * @param  \Illuminate\Session\Store  $session
	 * @param  \Symfony\Component\HttpFoundation\Request   $request
	 * @return void
	 */
	public function __construct(Repository $config, SessionStore $session, Request $request)
	{
		$this->config = $config;
		$this->session = $session;

		$this->remote_ip = $this->default_location['ip'] = $request->getClientIp();
	}

	/**
	 * Save location data in the session.
	 *
	 * @return void
	 */
	function saveLocation() {
		$this->session->set('geoip-location', $this->location);
	}

	/**
	 * Get location from IP.
	 *
	 * @param  string $ip Optional
	 * @return array
	 */
	function getLocation( $ip = null ) {
		// Get location data
		$this->location = $this->find( $ip );

		// Save user's location
		if( $ip === null ) {
			$this->saveLocation();
		}

		return $this->location;
	}

	private function find( $ip = null ) {
		// Check Session
		if ( $ip === null && $position = $this->session->get('geoip-location') ) {
			if($position['ip'] === $this->remote_ip) {
				return $position;
			}
		}

		// If IP not set, user remote IP
		if ( $ip === null ) {
			$ip = $this->remote_ip;
		}

		// Check if the ip is not local or empty
		if( $this->checkIp( $ip ) ) {

			// Call default service
			$service = 'locate_'.$this->config->get('geoip::service');
			return $this->$service($ip);
		}

		return $this->default_location;
	}

	// Maxmind
	private function locate_maxmind( $ip ) {

		$settings = $this->config->get('geoip::maxmind');

		if($settings['type'] === 'web_service') {
			$maxmind = new Client($settings['user_id'], $settings['license_key']);
		}
		else {
			$maxmind = new Reader(app_path().'/database/maxmind/GeoLite2-City.mmdb');
		}

		$record = $maxmind->city($ip);

		$location = array(
			"ip"			=> $ip,
			"isoCode" 		=> $record->country->isoCode,
			"country" 		=> $record->country->name,
			"city" 			=> $record->city->name,
			"state" 		=> $record->mostSpecificSubdivision->isoCode,
			"postal_code" 	=> $record->postal->code,
			"lat" 			=> $record->location->latitude,
			"lon" 			=> $record->location->longitude
		);

		unset($record);
<commit_msg>Add default to return array. This will tell the developer that the location was not found<commit_after><?php namespace Torann\GeoIP;

use GeoIp2\Database\Reader;
use Illuminate\Config\Repository;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Session\Store as SessionStore;

class GeoIP {

	/**
	 * The session store.
	 *
	 * @var \Illuminate\Session\Store
	 */
	protected $session;

    /**
     * Illuminate config repository instance.
     *
     * @var \Illuminate\Config\Repository
     */
    protected $config;

	/**
	 * Remote Machine IP address.
	 *
	 * @var float
	 */
	protected $remote_ip = null;

	/**
	 * Location data.
	 *
	 * @var array
	 */
	protected $location = null;

	/**
	 * Reserved IP address.
	 *
	 * @var array
	 */
	protected $reserved_ips = array (
		array('0.0.0.0','192.168.3.11'),
		array('10.0.0.0','10.255.255.255'),
		array('127.0.0.0','127.255.255.255'),
		array('169.254.0.0','169.254.255.255'),
		array('172.16.0.0','172.31.255.255'),
		array('192.0.2.0','192.0.2.255'),
		array('192.168.0.0','192.168.255.255'),
		array('255.255.255.0','255.255.255.255')
	);

	/**
	 * Default Location data.
	 *
	 * @var array
	 */
	protected $default_location = array (
		"ip" 			=> "127.0.0.0",
		"isoCode" 		=> "US",
		"country" 		=> "United States",
		"city" 			=> "New Haven",
		"state" 		=> "CT",
		"postal_code" 	=> "06510",
		"lat" 			=> 41.31,
		"lon" 			=> -72.92,
		"deafult"       => true
	);

	/**
	 * Create a new GeoIP instance.
	 *
     * @param  \Illuminate\Config\Repository  $config
	 * @param  \Illuminate\Session\Store  $session
	 * @param  \Symfony\Component\HttpFoundation\Request   $request
	 * @return void
	 */
	public function __construct(Repository $config, SessionStore $session, Request $request)
	{
		$this->config = $config;
		$this->session = $session;

		$this->remote_ip = $this->default_location['ip'] = $request->getClientIp();
	}

	/**
	 * Save location data in the session.
	 *
	 * @return void
	 */
	function saveLocation() {
		$this->session->set('geoip-location', $this->location);
	}

	/**
	 * Get location from IP.
	 *
	 * @param  string $ip Optional
	 * @return array
	 */
	function getLocation( $ip = null ) {
		// Get location data
		$this->location = $this->find( $ip );

		// Save user's location
		if( $ip === null ) {
			$this->saveLocation();
		}

		return $this->location;
	}

	private function find( $ip = null ) {
		// Check Session
		if ( $ip === null && $position = $this->session->get('geoip-location') ) {
			if($position['ip'] === $this->remote_ip) {
				return $position;
			}
		}

		// If IP not set, user remote IP
		if ( $ip === null ) {
			$ip = $this->remote_ip;
		}

		// Check if the ip is not local or empty
		if( $this->checkIp( $ip ) ) {

			// Call default service
			$service = 'locate_'.$this->config->get('geoip::service');

			return $this->$service($ip);
		}

		return $this->default_location;
	}

	// Maxmind
	private function locate_maxmind( $ip ) {

		$settings = $this->config->get('geoip::maxmind');

		if($settings['type'] === 'web_service') {
			$maxmind = new Client($settings['user_id'], $settings['license_key']);
		}
		else {
			$maxmind = new Reader(app_path().'/database/maxmind/GeoLite2-City.mmdb');
		}

		$record = $maxmind->city($ip);

		$location = array(
			"ip"			=> $ip,
			"isoCode" 		=> $record->country->isoCode,
			"country" 		=> $record->country->name,
			"city" 			=> $record->city->name,
			"state" 		=> $record->mostSpecificSubdivision->isoCode,
			"postal_code" 	=> $record->postal->code,
			"lat" 			=> $record->location->latitude,
			"lon" 			=> $record->location->longitude,
			"deafult"       => false
		);

		unset($record);

{'fraction_non_alphanumeric': 0.19340080410370164, 'fraction_numerical': 0.04491889643698877, 'mean_word_length': 2.9356246590289143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 34, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2330503', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2596, 'n_words': 513}