AbstractFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 73
ccs 21
cts 27
cp 0.7778
rs 10
c 0
b 0
f 0

6 Methods

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