Completed
Push — master ( cfda18...16b364 )
by Tomas
05:21 queued 11s
created

AbstractFactory::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.3755

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 6
cts 11
cp 0.5455
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.3755
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 29
    public function createProvider(array $options = []): Provider
35
    {
36 29
        $this->verifyDependencies();
37
38 29
        $resolver = new OptionsResolver();
39 29
        static::configureOptionResolver($resolver);
40 29
        $config = $resolver->resolve($options);
41
42 29
        return $this->getProvider($config);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 29
    public static function validate(array $options, $providerName)
49
    {
50 29
        static::verifyDependencies();
51
52 29
        $resolver = new OptionsResolver();
53 29
        static::configureOptionResolver($resolver);
54
55
        try {
56 29
            $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 29
    }
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 29
    protected static function verifyDependencies()
75
    {
76 29
        foreach (static::$dependencies as $dependency) {
77 29
            if (!class_exists($dependency['requiredClass'])) {
78
                throw new \LogicException(sprintf('You must install the "%s" package to use the "%s" factory.', $dependency['packageName'], static::class));
79
            }
80
        }
81 29
    }
82
83
    /**
84
     * By default we do not have any options to configure. A factory should override this function and confgure
85
     * the options resolver.
86
     */
87 1
    protected static function configureOptionResolver(OptionsResolver $resolver)
88
    {
89 1
    }
90
}
91