AssetViewHelperFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 53.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 48
ccs 8
cts 15
cp 0.5333
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 1
B loadCache() 0 23 6
1
<?php
2
3
namespace AssetManager\Service;
4
5
use AssetManager\Core\Exception\InvalidArgumentException;
6
use AssetManager\Core\Resolver\ResolverInterface;
7
use AssetManager\Core\Service\AssetManager;
8
use AssetManager\View\Helper\Asset;
9
use Interop\Container\ContainerInterface;
10
use Zend\Cache\Storage\Adapter\AbstractAdapter as AbstractCacheAdapter;
11
12
class AssetViewHelperFactory
13
{
14
    /**
15
     * @inheritDoc
16
     */
17 1
    public function __invoke(ContainerInterface $container)
18
    {
19 1
        $config = $container->get('config')['asset_manager'];
20
21
        /** @var ResolverInterface $assetManagerResolver */
22 1
        $assetManagerResolver = $container->get(AssetManager::class)->getResolver();
23
24
        /** @var AbstractCacheAdapter|null $cache */
25 1
        $cache = $this->loadCache($container, $config);
26
27 1
        return new Asset($assetManagerResolver, $cache, $config);
28
    }
29
30
    /**
31
     * @param ContainerInterface $container
32
     * @param array                   $config
33
     *
34
     * @return null
35
     */
36 1
    private function loadCache(ContainerInterface $container, $config)
37
    {
38
        // check if the cache is configured
39 1
        if (!isset($config['view_helper']['cache']) || $config['view_helper']['cache'] === null) {
40 1
            return null;
41
        }
42
43
        // get the cache, if it's a string, search it among services
44
        $cache = $config['view_helper']['cache'];
45
        if (is_string($cache)) {
46
            $cache = $container->get($cache);
47
        }
48
49
        // exception in case cache is not an Adapter that extend the AbstractAdapter of Zend\Cache\Storage
50
        if ($cache !== null && !($cache instanceof AbstractCacheAdapter)) {
51
            throw new InvalidArgumentException(
52
                'Invalid cache provided, you must pass a Cache Adapter that extend 
53
                Zend\Cache\Storage\Adapter\AbstractAdapter'
54
            );
55
        }
56
57
        return $cache;
58
    }
59
}
60