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\Provider; |
16
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An abstract factory that makes it easier to implement new factories. A class that extend the AbstractFactory |
21
|
|
|
* should override AbstractFactory::$dependencies and AbstractFactory::configureOptionResolver(). |
22
|
|
|
* |
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractFactory implements ProviderFactoryInterface |
26
|
|
|
{ |
27
|
|
|
protected static $dependencies = []; |
28
|
|
|
|
29
|
|
|
abstract protected function getProvider(array $config): Provider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
27 |
|
public function createProvider(array $options = []): Provider |
35
|
|
|
{ |
36
|
27 |
|
$this->verifyDependencies(); |
37
|
|
|
|
38
|
27 |
|
$resolver = new OptionsResolver(); |
39
|
27 |
|
static::configureOptionResolver($resolver); |
40
|
27 |
|
$config = $resolver->resolve($options); |
41
|
|
|
|
42
|
27 |
|
return $this->getProvider($config); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
27 |
|
public static function validate(array $options, $providerName) |
49
|
|
|
{ |
50
|
27 |
|
static::verifyDependencies(); |
51
|
|
|
|
52
|
27 |
|
$resolver = new OptionsResolver(); |
53
|
27 |
|
static::configureOptionResolver($resolver); |
54
|
|
|
|
55
|
|
|
try { |
56
|
27 |
|
$resolver->resolve($options); |
57
|
|
|
} catch (\Exception $e) { |
58
|
|
|
$message = sprintf( |
59
|
|
|
'Error while configure provider "%s". Verify your configuration at "bazinga_geocoder.providers.%s.options". %s', |
60
|
|
|
$providerName, |
61
|
|
|
$providerName, |
62
|
|
|
$e->getMessage() |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
throw new InvalidConfigurationException($message, $e->getCode(), $e); |
66
|
|
|
} |
67
|
27 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Make sure that we have the required class and throw and exception if we don't. |
71
|
|
|
* |
72
|
|
|
* @throws \LogicException |
73
|
|
|
*/ |
74
|
27 |
|
protected static function verifyDependencies() |
75
|
|
|
{ |
76
|
27 |
|
foreach (static::$dependencies as $dependency) { |
77
|
27 |
|
if (!class_exists($dependency['requiredClass'])) { |
78
|
|
|
throw new \LogicException( |
79
|
|
|
sprintf( |
80
|
|
|
'You must install the "%s" package to use the "%s" factory.', |
81
|
|
|
$dependency['packageName'], |
82
|
|
|
static::class |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
27 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* By default we do not have any options to configure. A factory should override this function and confgure |
91
|
|
|
* the options resolver. |
92
|
|
|
*/ |
93
|
1 |
|
protected static function configureOptionResolver(OptionsResolver $resolver) |
94
|
|
|
{ |
95
|
1 |
|
} |
96
|
|
|
} |
97
|
|
|
|