Files
shlink/module/IpGeolocation/src/Resolver/ChainIpLocationResolver.php
2019-08-10 13:56:06 +02:00

39 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\IpGeolocation\Resolver;
use Shlinkio\Shlink\Common\Exception\WrongIpException;
use Shlinkio\Shlink\IpGeolocation\Model;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
class ChainIpLocationResolver implements IpLocationResolverInterface
{
/** @var IpLocationResolverInterface[] */
private $resolvers;
public function __construct(IpLocationResolverInterface ...$resolvers)
{
$this->resolvers = $resolvers;
}
/**
* @throws WrongIpException
*/
public function resolveIpLocation(string $ipAddress): Model\Location
{
$error = null;
foreach ($this->resolvers as $resolver) {
try {
return $resolver->resolveIpLocation($ipAddress);
} catch (WrongIpException $e) {
$error = $e;
}
}
// If this instruction is reached, it means no resolver was capable of resolving the address
throw WrongIpException::fromIpAddress($ipAddress, $error);
}
}