|
| 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 | +} |
0 commit comments