Completed
Push — master ( 8c9a02...d7d88b )
by Tobias
11:28
created

AbstractFactory::getProvider()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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
    /**
30
     * @param array $config
31
     *
32
     * @return Provider
33
     */
34
    abstract protected function getProvider(array $config): Provider;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 22
    public function createProvider(array $options = []): Provider
40
    {
41 22
        $this->verifyDependencies();
42
43 22
        $resolver = new OptionsResolver();
44 22
        static::configureOptionResolver($resolver);
45 22
        $config = $resolver->resolve($options);
46
47 22
        return $this->getProvider($config);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 22
    public static function validate(array $options, $providerName)
54
    {
55 22
        static::verifyDependencies();
56
57 22
        $resolver = new OptionsResolver();
58 22
        static::configureOptionResolver($resolver);
59
60
        try {
61 22
            $resolver->resolve($options);
62
        } catch (\Exception $e) {
63
            $message = sprintf(
64
                'Error while configure provider "%s". Verify your configuration at "bazinga_geocoder.providers.%s.options". %s',
65
                $providerName,
66
                $providerName,
67
                $e->getMessage()
68
            );
69
70
            throw new InvalidConfigurationException($message, $e->getCode(), $e);
71
        }
72 22
    }
73
74
    /**
75
     * Make sure that we have the required class and throw and exception if we don't.
76
     *
77
     * @throws \LogicException
78
     */
79 22
    protected static function verifyDependencies()
80
    {
81 22
        foreach (static::$dependencies as $dependency) {
82 22
            if (!class_exists($dependency['requiredClass'])) {
83
                throw new \LogicException(
84
                    sprintf(
85
                        'You must install the "%s" package to use the "%s" factory.',
86
                        $dependency['packageName'],
87 22
                        static::class
88
                    )
89
                );
90
            }
91
        }
92 22
    }
93
94
    /**
95
     * By default we do not have any options to configure. A factory should override this function and confgure
96
     * the options resolver.
97
     *
98
     * @param OptionsResolver $resolver
99
     */
100 1
    protected static function configureOptionResolver(OptionsResolver $resolver)
101
    {
102 1
    }
103
}
104