<commit_before><?php

/**
 * Core
 *
 * The core of any web-project by Lowie Huyghe
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */

namespace Core\Models;
use Core\Bases\Models\BaseModel;
use Core\Http\Request;
use Core\Models\User;
use Core\Registry\Admin\Pm;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Model\City;

/**
 * The helper for the browser
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 *
 * @property User $user
 * @property string $ip
 * @property string $continent_code
 * @property string $continent_name
 * @property string $country_code
 * @property string $country_name
 * @property string $region_code
 * @property string $region_name
 * @property string $city_name
 * @property string $postal_code
 * @property float $latitude
 * @property float $longitude
 * @property string $timezone
 */
class Location extends BaseModel
{
	const	GEOIP_DIR	= 'geoip',
			GEOIP_CITY	= 'GeoLite2-City.mmdb';

	/**
	 * The table associated with the model.
	 * @var string
	 */
	protected $table = 'user_locations';

	/**
	 * The primary key for the model.
	 * @var string
	 */
	protected $primaryKey = 'user_id';

	/**
	 * The user
	 * @return BelongsTo
	 */
	public function user()
	{
		return $this->belongsTo(User::class);
	}

	/**
	 * Initialize
	 * @param string $ip
	 */
	public function initialize($ip)
	{
		$cityDetails = $this->getDetailsCity($ip);

		if ($cityDetails)
		{
			$this->ip = $ip;

			$this->continent_code = $cityDetails->continent->code;
			$this->continent_name = $cityDetails->continent->name;

			$this->country_code = $cityDetails->country->isoCode;
			$this->country_name = $cityDetails->country->name;

			$this->region_code = $cityDetails->mostSpecificSubdivision->isoCode;
			$this->region_name = $cityDetails->mostSpecificSubdivision->name;

			$this->city_name = $cityDetails->city->name;
			$this->postal_code = $cityDetails->postal->code;

			$this->latitude = $cityDetails->location->latitude;
			$this->longitude = $cityDetails->location->longitude;
			$this->timezone = $cityDetails->location->timeZone;
		}
	}

	/**
	 * Return the GeoIP of browser
	 * @return Reader
	 */
	private function getGeoIPCity()
	{
		return new Reader(storage_path(self::GEOIP_DIR . '/' . self::GEOIP_CITY));
	}

	/**
	 * Return the details of city db
	 * @param string $ip
	 * @return City
	 */
	private function getDetailsCity($ip)
	{
		try
		{
			$reader = $this->getGeoIPCity();
			return $reader->city($ip);
		}
		catch(AddressNotFoundException $e)
		{
			return false;
		}
		catch(\InvalidArgumentException $e)
		{
			Registry::pm()->error('GeoIP Error!', 'There is no city-geo-db.');
			return false;
		}
	}

	/**
	 * Add the validation of the model
	 */
	protected function addValidationRules()
	{<commit_msg>#75: Removed unnecessary reporting<commit_after><?php

/**
 * Core
 *
 * The core of any web-project by <NAME>
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */

namespace Core\Models;
use Core\Bases\Models\BaseModel;
use Core\Http\Request;
use Core\Models\User;
use Core\Registry\Admin\Pm;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Model\City;

/**
 * The helper for the browser
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 *
 * @property User $user
 * @property string $ip
 * @property string $continent_code
 * @property string $continent_name
 * @property string $country_code
 * @property string $country_name
 * @property string $region_code
 * @property string $region_name
 * @property string $city_name
 * @property string $postal_code
 * @property float $latitude
 * @property float $longitude
 * @property string $timezone
 */
class Location extends BaseModel
{
	const	GEOIP_DIR	= 'geoip',
			GEOIP_CITY	= 'GeoLite2-City.mmdb';

	/**
	 * The table associated with the model.
	 * @var string
	 */
	protected $table = 'user_locations';

	/**
	 * The primary key for the model.
	 * @var string
	 */
	protected $primaryKey = 'user_id';

	/**
	 * The user
	 * @return BelongsTo
	 */
	public function user()
	{
		return $this->belongsTo(User::class);
	}

	/**
	 * Initialize
	 * @param string $ip
	 */
	public function initialize($ip)
	{
		$cityDetails = $this->getDetailsCity($ip);

		if ($cityDetails)
		{
			$this->ip = $ip;

			$this->continent_code = $cityDetails->continent->code;
			$this->continent_name = $cityDetails->continent->name;

			$this->country_code = $cityDetails->country->isoCode;
			$this->country_name = $cityDetails->country->name;

			$this->region_code = $cityDetails->mostSpecificSubdivision->isoCode;
			$this->region_name = $cityDetails->mostSpecificSubdivision->name;

			$this->city_name = $cityDetails->city->name;
			$this->postal_code = $cityDetails->postal->code;

			$this->latitude = $cityDetails->location->latitude;
			$this->longitude = $cityDetails->location->longitude;
			$this->timezone = $cityDetails->location->timeZone;
		}
	}

	/**
	 * Return the GeoIP of browser
	 * @return Reader
	 */
	private function getGeoIPCity()
	{
		return new Reader(storage_path(self::GEOIP_DIR . '/' . self::GEOIP_CITY));
	}

	/**
	 * Return the details of city db
	 * @param string $ip
	 * @return City
	 */
	private function getDetailsCity($ip)
	{
		try
		{
			$reader = $this->getGeoIPCity();
			return $reader->city($ip);
		}
		catch(AddressNotFoundException $e)
		{
			return false;
		}
		catch(\InvalidArgumentException $e)
		{
			return false;
		}
	}

	/**
	 * Add the validation of the model
	 */
	protected function addValidationRules()
	{
{'fraction_non_alphanumeric': 0.13823575824522644, 'fraction_numerical': 0.0053653148177371, 'mean_word_length': 3.830792682926829, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6585586', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2038, 'n_words': 518}