AssetCacheManager::getProvider()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 5
nop 1
1
<?php
2
3
namespace AssetManager\Core\Service;
4
5
use Assetic\Asset\AssetCache;
6
use Assetic\Asset\AssetInterface;
7
use Assetic\Cache\CacheInterface;
8
use Psr\Container\ContainerInterface;
9
10
/**
11
 * Asset Cache Manager.  Sets asset cache based on configuration.
12
 */
13
class AssetCacheManager
14
{
15
    /**
16
     * @var \Psr\Container\ContainerInterface;
17
     */
18
    protected $container;
19
20
    /**
21
     * @var array Cache configuration.
22
     */
23
    protected $config = array();
24
25
    /**
26
     * Construct the AssetCacheManager
27
     *
28
     * @param   ContainerInterface $container
29
     * @param   array              $config
30
     */
31
    public function __construct(
32
        ContainerInterface $container,
33
        $config
34
    ) {
35
        $this->container = $container;
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * Set the cache (if any) on the asset, and return the new AssetCache.
41
     *
42
     * @param string         $path  Path to asset
43
     * @param AssetInterface $asset Assetic Asset Interface
44
     *
45
     * @return  AssetInterface|CacheInterface
46
     */
47
    public function setCache($path, AssetInterface $asset)
48
    {
49
        $provider = $this->getProvider($path);
50
51
        if (!$provider instanceof CacheInterface) {
52
            return $asset;
53
        }
54
55
        $assetCache             = new AssetCache($asset, $provider);
56
        $assetCache->mimetype   = $asset->mimetype;
0 ignored issues
show
Bug introduced by
The property mimetype does not seem to exist in Assetic\Asset\AssetCache.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
Accessing mimetype on the interface Assetic\Asset\AssetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
57
58
        return $assetCache;
59
    }
60
61
    /**
62
     * Get the cache provider.  First checks to see if the provider is callable,
63
     * then will attempt to get it from the service locator, finally will fallback
64
     * to a class mapper.
65
     *
66
     * @param $path
67
     *
68
     * @return array
69
     */
70
    private function getProvider($path)
71
    {
72
        $cacheProvider = $this->getCacheProviderConfig($path);
73
74
        if (!$cacheProvider) {
75
            return null;
76
        }
77
78
        if (is_string($cacheProvider['cache']) &&
79
            $this->container->has($cacheProvider['cache'])
80
        ) {
81
            return $this->container->get($cacheProvider['cache']);
82
        }
83
84
        // Left here for BC.  Please consider defining a ZF2 service instead.
85
        if (is_callable($cacheProvider['cache'])) {
86
            return call_user_func($cacheProvider['cache'], $path);
87
        }
88
89
        $dir = '';
90
        $class = $cacheProvider['cache'];
91
92
        if (!empty($cacheProvider['options']['dir'])) {
93
            $dir = $cacheProvider['options']['dir'];
94
        }
95
96
        $class = $this->classMapper($class);
97
        return new $class($dir, $path);
98
    }
99
100
    /**
101
     * Get the cache provider config.  Use default values if defined.
102
     *
103
     * @param $path
104
     *
105
     * @return null|array Cache config definition.  Returns null if not found in
106
     *                    config.
107
     */
108
    private function getCacheProviderConfig($path)
109
    {
110
        $cacheProvider = null;
111
112
        if (!empty($this->config[$path]) && !empty($this->config[$path]['cache'])) {
113
            $cacheProvider = $this->config[$path];
114
        }
115
116
        if (!$cacheProvider
117
            && !empty($this->config['default'])
118
            && !empty($this->config['default']['cache'])
119
        ) {
120
            $cacheProvider = $this->config['default'];
121
        }
122
123
        return $cacheProvider;
124
    }
125
126
    /**
127
     * Class mapper to provide backwards compatibility
128
     *
129
     * @param $class
130
     *
131
     * @return string
132
     */
133
    private function classMapper($class)
134
    {
135
        $classToCheck = $class;
136
        $classToCheck .= (substr($class, -5) === 'Cache') ? '' : 'Cache';
137
138
        switch ($classToCheck) {
139
            case 'ApcCache':
140
                $class = 'Assetic\Cache\ApcCache';
141
                break;
142
            case 'FilesystemCache':
143
                $class = 'Assetic\Cache\FilesystemCache';
144
                break;
145
            case 'FilePathCache':
146
                $class = 'AssetManager\Core\Cache\FilePathCache';
147
                break;
148
        }
149
150
        return $class;
151
    }
152
}
153