AbstractFactory::createProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace DH\NavigationBundle\Provider;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * An abstract factory that makes it easier to implement new factories. A class that extend the AbstractFactory
10
 * should override AbstractFactory::configureOptionResolver().
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
abstract class AbstractFactory implements ProviderFactoryInterface
15
{
16
    abstract protected function getProvider(array $config): ProviderInterface;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function createProvider(array $options = []): ProviderInterface
22
    {
23
        $resolver = new OptionsResolver();
24
        static::configureOptionResolver($resolver);
25
        $config = $resolver->resolve($options);
26
27
        return $this->getProvider($config);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public static function validate(array $options, $providerName): void
34
    {
35
        $resolver = new OptionsResolver();
36
        static::configureOptionResolver($resolver);
37
38
        try {
39
            $resolver->resolve($options);
40
        } catch (\Exception $e) {
41
            $message = sprintf(
42
                'Error while configure provider "%s". Verify your configuration. %s',
43
                $providerName,
44
                $e->getMessage()
45
            );
46
47
            throw new InvalidConfigurationException($message, $e->getCode(), $e);
48
        }
49
    }
50
51
    /**
52
     * By default we do not have any options to configure.
53
     * A factory should override this function and configure the options resolver.
54
     */
55
    protected static function configureOptionResolver(OptionsResolver $resolver): void
0 ignored issues
show
Unused Code introduced by
The parameter $resolver is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    protected static function configureOptionResolver(/** @scrutinizer ignore-unused */ OptionsResolver $resolver): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
    }
58
}
59