Skip to content

Commit

Permalink
Merge branch 'MDL-76915-master' of https://github.com/meirzamoodle/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov authored and sarjona committed Mar 17, 2023
2 parents cc35edd + 6d2e7a5 commit 9caa246
Show file tree
Hide file tree
Showing 27 changed files with 360 additions and 113 deletions.
78 changes: 52 additions & 26 deletions lib/maxmind/GeoIp2/Database/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
namespace GeoIp2\Database;

use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Model\AbstractModel;
use GeoIp2\Model\AnonymousIp;
use GeoIp2\Model\Asn;
use GeoIp2\Model\City;
use GeoIp2\Model\ConnectionType;
use GeoIp2\Model\Country;
use GeoIp2\Model\Domain;
use GeoIp2\Model\Enterprise;
use GeoIp2\Model\Isp;
use GeoIp2\ProviderInterface;
use MaxMind\Db\Reader as DbReader;
use MaxMind\Db\Reader\InvalidDatabaseException;
Expand Down Expand Up @@ -35,8 +44,19 @@
*/
class Reader implements ProviderInterface
{
/**
* @var DbReader
*/
private $dbReader;

/**
* @var string
*/
private $dbType;

/**
* @var array<string>
*/
private $locales;

/**
Expand Down Expand Up @@ -68,9 +88,10 @@ public function __construct(
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function city(string $ipAddress): \GeoIp2\Model\City
public function city(string $ipAddress): City
{
return $this->modelFor('City', 'City', $ipAddress);
// @phpstan-ignore-next-line
return $this->modelFor(City::class, 'City', $ipAddress);
}

/**
Expand All @@ -83,9 +104,10 @@ public function city(string $ipAddress): \GeoIp2\Model\City
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function country(string $ipAddress): \GeoIp2\Model\Country
public function country(string $ipAddress): Country
{
return $this->modelFor('Country', 'Country', $ipAddress);
// @phpstan-ignore-next-line
return $this->modelFor(Country::class, 'Country', $ipAddress);
}

/**
Expand All @@ -98,10 +120,11 @@ public function country(string $ipAddress): \GeoIp2\Model\Country
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function anonymousIp(string $ipAddress): \GeoIp2\Model\AnonymousIp
public function anonymousIp(string $ipAddress): AnonymousIp
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'AnonymousIp',
AnonymousIp::class,
'GeoIP2-Anonymous-IP',
$ipAddress
);
Expand All @@ -117,10 +140,11 @@ public function anonymousIp(string $ipAddress): \GeoIp2\Model\AnonymousIp
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function asn(string $ipAddress): \GeoIp2\Model\Asn
public function asn(string $ipAddress): Asn
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'Asn',
Asn::class,
'GeoLite2-ASN',
$ipAddress
);
Expand All @@ -136,10 +160,11 @@ public function asn(string $ipAddress): \GeoIp2\Model\Asn
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function connectionType(string $ipAddress): \GeoIp2\Model\ConnectionType
public function connectionType(string $ipAddress): ConnectionType
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'ConnectionType',
ConnectionType::class,
'GeoIP2-Connection-Type',
$ipAddress
);
Expand All @@ -155,10 +180,11 @@ public function connectionType(string $ipAddress): \GeoIp2\Model\ConnectionType
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function domain(string $ipAddress): \GeoIp2\Model\Domain
public function domain(string $ipAddress): Domain
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'Domain',
Domain::class,
'GeoIP2-Domain',
$ipAddress
);
Expand All @@ -174,9 +200,10 @@ public function domain(string $ipAddress): \GeoIp2\Model\Domain
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function enterprise(string $ipAddress): \GeoIp2\Model\Enterprise
public function enterprise(string $ipAddress): Enterprise
{
return $this->modelFor('Enterprise', 'Enterprise', $ipAddress);
// @phpstan-ignore-next-line
return $this->modelFor(Enterprise::class, 'Enterprise', $ipAddress);
}

/**
Expand All @@ -189,47 +216,46 @@ public function enterprise(string $ipAddress): \GeoIp2\Model\Enterprise
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
* is corrupt or invalid
*/
public function isp(string $ipAddress): \GeoIp2\Model\Isp
public function isp(string $ipAddress): Isp
{
// @phpstan-ignore-next-line
return $this->flatModelFor(
'Isp',
Isp::class,
'GeoIP2-ISP',
$ipAddress
);
}

private function modelFor(string $class, string $type, string $ipAddress)
private function modelFor(string $class, string $type, string $ipAddress): AbstractModel
{
list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
[$record, $prefixLen] = $this->getRecord($class, $type, $ipAddress);

$record['traits']['ip_address'] = $ipAddress;
$record['traits']['prefix_len'] = $prefixLen;

$class = 'GeoIp2\\Model\\' . $class;

return new $class($record, $this->locales);
}

private function flatModelFor(string $class, string $type, string $ipAddress)
private function flatModelFor(string $class, string $type, string $ipAddress): AbstractModel
{
list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
[$record, $prefixLen] = $this->getRecord($class, $type, $ipAddress);

$record['ip_address'] = $ipAddress;
$record['prefix_len'] = $prefixLen;
$class = 'GeoIp2\\Model\\' . $class;

return new $class($record);
}

private function getRecord(string $class, string $type, string $ipAddress): array
{
if (strpos($this->dbType, $type) === false) {
$method = lcfirst($class);
$method = lcfirst((new \ReflectionClass($class))->getShortName());

throw new \BadMethodCallException(
"The $method method cannot be used to open a {$this->dbType} database"
);
}
list($record, $prefixLen) = $this->dbReader->getWithPrefixLen($ipAddress);
[$record, $prefixLen] = $this->dbReader->getWithPrefixLen($ipAddress);
if ($record === null) {
throw new AddressNotFoundException(
"The address $ipAddress is not in the database."
Expand Down Expand Up @@ -258,7 +284,7 @@ private function getRecord(string $class, string $type, string $ipAddress): arra
*
* @return \MaxMind\Db\Reader\Metadata object for the database
*/
public function metadata(): \MaxMind\Db\Reader\Metadata
public function metadata(): DbReader\Metadata
{
return $this->dbReader->metadata();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/maxmind/GeoIp2/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class HttpException extends GeoIp2Exception
{
/**
* The URI queried.
*
* @var string
*/
public $uri;

Expand Down
2 changes: 2 additions & 0 deletions lib/maxmind/GeoIp2/Exception/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class InvalidRequestException extends HttpException
{
/**
* The code returned by the MaxMind web service.
*
* @var string
*/
public $error;

Expand Down
11 changes: 9 additions & 2 deletions lib/maxmind/GeoIp2/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
abstract class AbstractModel implements \JsonSerializable
{
/**
* @var array<string, mixed>
*/
protected $raw;

/**
Expand All @@ -21,6 +24,8 @@ public function __construct(array $raw)

/**
* @ignore
*
* @return mixed
*/
protected function get(string $field)
{
Expand All @@ -36,11 +41,13 @@ protected function get(string $field)

/**
* @ignore
*
* @return mixed
*/
public function __get(string $attr)
{
if ($attr !== 'instance' && property_exists($this, $attr)) {
return $this->$attr;
return $this->{$attr};
}

throw new \RuntimeException("Unknown attribute: $attr");
Expand All @@ -51,7 +58,7 @@ public function __get(string $attr)
*/
public function __isset(string $attr): bool
{
return $attr !== 'instance' && isset($this->$attr);
return $attr !== 'instance' && isset($this->{$attr});
}

public function jsonSerialize(): array
Expand Down
31 changes: 31 additions & 0 deletions lib/maxmind/GeoIp2/Model/AnonymousIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,44 @@
*/
class AnonymousIp extends AbstractModel
{
/**
* @var bool
*/
protected $isAnonymous;

/**
* @var bool
*/
protected $isAnonymousVpn;

/**
* @var bool
*/
protected $isHostingProvider;

/**
* @var bool
*/
protected $isPublicProxy;

/**
* @var bool
*/
protected $isResidentialProxy;

/**
* @var bool
*/
protected $isTorExitNode;

/**
* @var string
*/
protected $ipAddress;

/**
* @var string
*/
protected $network;

/**
Expand Down
15 changes: 15 additions & 0 deletions lib/maxmind/GeoIp2/Model/Asn.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,24 @@
*/
class Asn extends AbstractModel
{
/**
* @var int|null
*/
protected $autonomousSystemNumber;

/**
* @var string|null
*/
protected $autonomousSystemOrganization;

/**
* @var string
*/
protected $ipAddress;

/**
* @var string
*/
protected $network;

/**
Expand Down
Loading

0 comments on commit 9caa246

Please sign in to comment.