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