sudiptpa / ipstack

A minimal implementation of IP to Location solution for small to large scale applications.
https://sujipthapa.co/blog/a-simple-ip-to-geo-location-solution-for-php
MIT License
4 stars 4 forks source link

Separatation of ip-lookup and ipstack-client #3

Open christoph-kluge opened 4 years ago

christoph-kluge commented 4 years ago

First of all, thanks for creating this little nice package. Currently there is a hard dependency between the ipstack client and the ip lookup call itself.

I would suggest and would love to see this seperated so you could auto-resolve the client with the api key w/o calling the configuration repository all the time. Here is a possible example:

$client = new IpStackClient($apiKey);
$ipstack = $client->lookup($ip); // will return the IpStack object which holds all relevant information
christoph-kluge commented 4 years ago

For now I use a small proxy/factory to ensure this behavior:

class IpStackFactory
{

    private $apiKey;

    public function __construct(string $apiKey)
    {
        $this->apiKey = $apiKey;
    }

    public function resolve(string $ip): IpStack
    {
        $ipStack = new Ipstack($ip, $this->apiKey);
        $ipStack->resolve('country'); // calling this so I can ensure that I have an resolved call
        return $ipStack;
    }
}