Completed
Push — master ( b8f1e2...d7fcc6 )
by Tobias
08:06
created

AbstractAdapterFactory::verifyDependencies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.5435

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 9
cp 0.4444
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 4.5435
1
<?php
2
3
/*
4
 * This file is part of php-cache organization.
5
 *
6
 * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\AdapterBundle\Factory;
13
14
use Cache\AdapterBundle\Exception\ConfigurationException;
15
use Psr\Cache\CacheItemPoolInterface;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
/**
19
 * An abstract factory that makes it easier to implement new factories. A class that extend the AbstractAdapterFactory
20
 * should override AbstractAdapterFactory::$dependencies and AbstractAdapterFactory::configureOptionResolver().
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
abstract class AbstractAdapterFactory implements AdapterFactoryInterface
25
{
26
    protected static $dependencies = [];
27
28
    /**
29
     * @param array $config
30
     *
31
     * @return CacheItemPoolInterface
32
     */
33
    abstract protected function getAdapter(array $config);
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function createAdapter(array $options = [])
39
    {
40 3
        $this->verifyDependencies();
41
42 3
        $resolver = new OptionsResolver();
43 3
        static::configureOptionResolver($resolver);
44 3
        $config = $resolver->resolve($options);
45
46 3
        return $this->getAdapter($config);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 6
    public static function validate(array $options, $adapterName)
53
    {
54 6
        static::verifyDependencies();
55
56 6
        $resolver = new OptionsResolver();
57 6
        static::configureOptionResolver($resolver);
58
59
        try {
60 6
            $resolver->resolve($options);
61
        } catch (\Exception $e) {
62
            $message = sprintf(
63
                'Error while configure adapter %s. Verify your configuration at "cache_adapter.providers.%s.options". %s',
64
                $adapterName,
65
                $adapterName,
66
                $e->getMessage()
67
            );
68
69
            throw new ConfigurationException($message, $e->getCode(), $e);
70
        }
71 6
    }
72
73
    /**
74
     * Make sure that we have the required class and throw and exception if we don't.
75
     *
76
     * @throws \LogicException
77
     */
78 6
    protected static function verifyDependencies()
79
    {
80 6
        foreach (static::$dependencies as $dependency) {
81 6
            if (!class_exists($dependency['requiredClass'])) {
82
                throw new \LogicException(
83
                    sprintf(
84
                        'You must install the "%s" package to use the "%s" factory.',
85
                        $dependency['packageName'],
86
                        static::class
87
                    )
88
                );
89
            }
90
        }
91 6
    }
92
93
    /**
94
     * By default we do not have any options to configure. A factory should override this function and confgure
95
     * the options resolver.
96
     *
97
     * @param OptionsResolver $resolver
98
     */
99 6
    protected static function configureOptionResolver(OptionsResolver $resolver)
100
    {
101 6
    }
102
}
103