Completed
Push — master ( c11d86...d758ed )
by Gianluca
13:50
created

ZendStorageCacheFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 45
ccs 0
cts 23
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 25 4
A createService() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModule\Service;
21
22
use Interop\Container\ContainerInterface;
23
use RuntimeException;
24
use Zend\ServiceManager\ServiceLocatorInterface;
25
use Zend\Cache\Storage\StorageInterface;
26
use DoctrineModule\Cache\ZendStorageCache;
27
28
/**
29
 * ZendStorageCache ServiceManager factory
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Marco Pivetta <[email protected]>
34
 */
35
class ZendStorageCacheFactory extends CacheFactory
36
{
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * @return \Doctrine\Common\Cache\Cache
41
     *
42
     * @throws RuntimeException
43
     */
44
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
45
    {
46
        /** @var $options \DoctrineModule\Options\Cache */
47
        $options  = $this->getOptions($container, 'cache');
48
        $instance = $options->getInstance();
49
50
        if (!$instance) {
51
            // @todo move this validation to the options class
52
            throw new RuntimeException('ZendStorageCache must have a referenced cache instance');
53
        }
54
55
        $cache = $container->get($instance);
56
57
        if (!$cache instanceof StorageInterface) {
58
            throw new RuntimeException(
59
                sprintf(
60
                    'Retrieved storage "%s" is not a Zend\Cache\Storage\StorageInterface instance, %s found',
61
                    $instance,
62
                    is_object($cache) ? get_class($cache) : getType($cache)
63
                )
64
            );
65
        }
66
67
        return new ZendStorageCache($cache);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     * @return ZendStorageCache
73
     * @throws RuntimeException
74
     */
75
    public function createService(ServiceLocatorInterface $container)
76
    {
77
        return $this($container, ZendStorageCache::class);
78
    }
79
}
80