FilePathCache   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 113
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A has() 0 4 1
A get() 0 10 2
B set() 0 27 4
A remove() 0 14 2
A cachedFile() 0 8 2
1
<?php
2
3
namespace AssetManager\Core\Cache;
4
5
use Assetic\Cache\CacheInterface;
6
use AssetManager\Core\Exception\RuntimeException;
7
use Zend\Stdlib\ErrorHandler;
8
9
/**
10
 * A file path cache. Same as FilesystemCache, except for the fact that this will create the
11
 * directories recursively, in stead of using a hash.
12
 */
13
class FilePathCache implements CacheInterface
14
{
15
    /**
16
     * @var string Holds the cache directory.
17
     */
18
    protected $dir;
19
20
    /**
21
     * @var string The filename we'll be caching for.
22
     */
23
    protected $filename;
24
25
    /**
26
     * @var string Holds the cachedFile string
27
     */
28
    protected $cachedFile;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param string $dir       The directory to cache in
34
     * @param string $filename  The filename we'll be caching for.
35
     */
36
    public function __construct($dir, $filename)
37
    {
38
        $this->dir      = $dir;
39
        $this->filename = $filename;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function has($key)
46
    {
47
        return file_exists($this->cachedFile());
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function get($key)
54
    {
55
        $path = $this->cachedFile();
56
57
        if (!file_exists($path)) {
58
            throw new RuntimeException('There is no cached value for ' . $this->filename);
59
        }
60
61
        return file_get_contents($path);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function set($key, $value)
68
    {
69
        $pathInfo = pathInfo($this->cachedFile());
70
        $cacheDir = $pathInfo['dirname'];
71
        $fileName = $pathInfo['basename'];
72
73
        ErrorHandler::start();
74
75
        if (!is_dir($cacheDir)) {
76
            mkdir($cacheDir, 0777, true);
77
        }
78
79
        ErrorHandler::stop();
80
81
        if (!is_writable($cacheDir)) {
82
            throw new RuntimeException('Unable to write file ' . $this->cachedFile());
83
        }
84
85
        // Use "rename" to achieve atomic writes
86
        $tmpFilePath = $cacheDir . '/AssetManagerFilePathCache_' . $fileName;
87
88
        if (@file_put_contents($tmpFilePath, $value, LOCK_EX) === false) {
89
            throw new RuntimeException('Unable to write file ' . $this->cachedFile());
90
        }
91
92
        rename($tmpFilePath, $this->cachedFile());
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function remove($key)
99
    {
100
        ErrorHandler::start(\E_WARNING);
101
102
        $success = unlink($this->cachedFile());
103
104
        ErrorHandler::stop();
105
106
        if (false === $success) {
107
            throw new RuntimeException(sprintf('Could not remove key "%s"', $this->cachedFile()));
108
        }
109
110
        return $success;
111
    }
112
113
    /**
114
     * Get the path-to-file.
115
     * @return string Cache path
116
     */
117
    protected function cachedFile()
118
    {
119
        if (null === $this->cachedFile) {
120
            $this->cachedFile = rtrim($this->dir, '/') . '/' . ltrim($this->filename, '/');
121
        }
122
123
        return $this->cachedFile;
124
    }
125
}
126