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