Skip to content

Commit e761a57

Browse files
authored
Added provider factories (#145)
* Adding provider factories * Starting to add tests * Adding more tests * Adding more tests * Added all tests and providers * Added docs * Applied changes from StyleCI * Moved services to a separate file
1 parent 0575516 commit e761a57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1154
-28
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\ArcGISOnline\ArcGISOnline;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class ArcGISOnlineFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => ArcGISOnline::class, 'packageName' => 'geocoder-php/arcgis-online-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new ArcGISOnline($httplug, $config['source_country']);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
'source_country' => null,
36+
]);
37+
38+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
39+
$resolver->setAllowedTypes('source_country', ['string', 'null']);
40+
}
41+
}

ProviderFactory/BingMapsFactory.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\BingMaps\BingMaps;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class BingMapsFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => BingMaps::class, 'packageName' => 'geocoder-php/bing-maps-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new BingMaps($httplug, $config['api_key']);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
]);
36+
37+
$resolver->setRequired('api_key');
38+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
39+
$resolver->setAllowedTypes('api_key', ['string']);
40+
}
41+
}

ProviderFactory/FreeGeoIpFactory.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\FreeGeoIp\FreeGeoIp;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class FreeGeoIpFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => FreeGeoIp::class, 'packageName' => 'geocoder-php/free-geoip-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new FreeGeoIp($httplug, $config['base_url']);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
'base_url' => 'https://freegeoip.net/json/%s',
36+
]);
37+
38+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
39+
$resolver->setAllowedTypes('base_url', ['string']);
40+
}
41+
}

ProviderFactory/GeoIP2Factory.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\GeoIP2\GeoIP2;
14+
use Geocoder\Provider\GeoIP2\GeoIP2Adapter;
15+
use GeoIp2\Database\Reader;
16+
use GeoIp2\ProviderInterface;
17+
use GeoIp2\WebService\Client;
18+
use Symfony\Component\OptionsResolver\OptionsResolver;
19+
20+
final class GeoIP2Factory extends AbstractFactory
21+
{
22+
protected static $dependencies = [
23+
['requiredClass' => GeoIP2::class, 'packageName' => 'geocoder-php/geoip2-provider'],
24+
];
25+
26+
protected function getProvider(array $config)
27+
{
28+
$provider = $config['provider'];
29+
if ($provider === 'webservice') {
30+
$provider = new Client($config['user_id'], $config['license_key'], $config['locales'], $config['webservice_options']);
31+
} elseif ($provider === 'database') {
32+
$provider = new Reader($config['database_filename'], $config['locales']);
33+
} else {
34+
$provider = $config['provider_service'];
35+
}
36+
37+
$adapter = new GeoIP2Adapter($provider, $config['model']);
38+
39+
return new GeoIP2($adapter);
40+
}
41+
42+
protected static function configureOptionResolver(OptionsResolver $resolver)
43+
{
44+
$resolver->setDefaults([
45+
'model' => GeoIP2Adapter::GEOIP2_MODEL_CITY,
46+
'database_filename' => null,
47+
'user_id' => null,
48+
'license_key' => null,
49+
'webservice_options' => [],
50+
'locales' => ['en'],
51+
'provider_service' => null,
52+
]);
53+
54+
$resolver->setRequired('provider');
55+
$resolver->setAllowedTypes('provider', ['string']);
56+
$resolver->setAllowedTypes('provider_service', [ProviderInterface::class, 'null']);
57+
$resolver->setAllowedTypes('model', ['string']);
58+
$resolver->setAllowedTypes('user_id', ['string', 'null']);
59+
$resolver->setAllowedTypes('license_key', ['string', 'null']);
60+
$resolver->setAllowedTypes('locales', ['array']);
61+
$resolver->setAllowedTypes('webservice_options', ['array']);
62+
$resolver->setAllowedTypes('database_filename', ['string', 'null']);
63+
64+
$resolver->setAllowedValues('model', [GeoIP2Adapter::GEOIP2_MODEL_CITY, GeoIP2Adapter::GEOIP2_MODEL_COUNTRY]);
65+
$resolver->setAllowedValues('provider', ['webservice', 'database', 'service']);
66+
}
67+
}

ProviderFactory/GeoIPsFactory.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\GeoIPs\GeoIPs;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class GeoIPsFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => GeoIPs::class, 'packageName' => 'geocoder-php/geoips-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new GeoIPs($httplug, $config['api_key']);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
]);
36+
37+
$resolver->setRequired('api_key');
38+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
39+
$resolver->setAllowedTypes('api_key', ['string']);
40+
}
41+
}

ProviderFactory/GeoPluginFactory.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\GeoPlugin\GeoPlugin;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class GeoPluginFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => GeoPlugin::class, 'packageName' => 'geocoder-php/geo-plugin-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new GeoPlugin($httplug);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
]);
36+
37+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
38+
}
39+
}

ProviderFactory/GeoipFactory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\Geoip\Geoip;
14+
use Symfony\Component\OptionsResolver\OptionsResolver;
15+
16+
final class GeoipFactory extends AbstractFactory
17+
{
18+
protected static $dependencies = [
19+
['requiredClass' => Geoip::class, 'packageName' => 'geocoder-php/geoip-provider'],
20+
];
21+
22+
protected function getProvider(array $config)
23+
{
24+
return new Geoip();
25+
}
26+
27+
protected static function configureOptionResolver(OptionsResolver $resolver)
28+
{
29+
}
30+
}

ProviderFactory/GeonamesFactory.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\Geonames\Geonames;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class GeonamesFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => Geonames::class, 'packageName' => 'geocoder-php/geonames-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new Geonames($httplug, $config['username']);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
]);
36+
37+
$resolver->setRequired('username');
38+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
39+
$resolver->setAllowedTypes('username', ['string']);
40+
}
41+
}

ProviderFactory/HostIpFactory.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the BazingaGeocoderBundle package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Bazinga\GeocoderBundle\ProviderFactory;
12+
13+
use Geocoder\Provider\HostIp\HostIp;
14+
use Http\Client\HttpClient;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
final class HostIpFactory extends AbstractFactory
19+
{
20+
protected static $dependencies = [
21+
['requiredClass' => HostIp::class, 'packageName' => 'geocoder-php/host-ip-provider'],
22+
];
23+
24+
protected function getProvider(array $config)
25+
{
26+
$httplug = $config['httplug_client'] ?: HttpClientDiscovery::find();
27+
28+
return new HostIp($httplug);
29+
}
30+
31+
protected static function configureOptionResolver(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'httplug_client' => null,
35+
]);
36+
37+
$resolver->setAllowedTypes('httplug_client', [HttpClient::class, 'null']);
38+
}
39+
}

0 commit comments

Comments
 (0)