Completed
Push — master ( a814e0...392de1 )
by Tobias
24:09
created

AbstractFactory::getProvider()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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