FileCache   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 123
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A loadMetadataFromCache() 0 8 2
A readFile() 0 4 1
A putMetadataInCache() 0 5 1
A writeFile() 0 8 1
A evictMetadataFromCache() 0 8 2
A getCacheFile() 0 4 1
A renameFile() 0 6 2
1
<?php
2
3
namespace As3\Modlr\Metadata\Cache;
4
5
use As3\Modlr\Metadata\EntityMetadata;
6
use As3\Modlr\Exception\RuntimeException;
7
use As3\Modlr\Exception\InvalidArgumentException;
8
9
/**
10
 * Caches and retrieves EntityMetadata objects from the file system.
11
 *
12
 * @author Jacob Bare <jacob.baregmail.com>
13
 */
14
class FileCache implements CacheInterface
15
{
16
    /**
17
     * The cache directory.
18
     *
19
     * @var string
20
     */
21
    protected $dir;
22
23
    /**
24
     * The cache type file prefix.
25
     *
26
     * @var string
27
     */
28
    protected $cachePrefix = 'FileCache';
29
30
    /**
31
     * The file extension.
32
     *
33
     * @var string
34
     */
35
    protected $extension = 'php';
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param   string          $dir
41
     */
42
    public function __construct($dir)
43
    {
44
        if (!is_dir($dir)) {
45
            throw new InvalidArgumentException(sprintf('The cache directory "%s" does not exist.', $dir));
46
        }
47
        if (!is_writable($dir)) {
48
            throw new InvalidArgumentException(sprintf('The cache directory "%s" is not writable.', $dir));
49
        }
50
        $this->dir = rtrim($dir, '\\/');
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function loadMetadataFromCache($type)
57
    {
58
        $file = $this->getCacheFile($type);
59
        if (!file_exists($file)) {
60
            return null;
61
        }
62
        return $this->readFile($file);
63
    }
64
65
    /**
66
     * Reads the cache file and returns as an EntityMetadata object.
67
     *
68
     * @param   string  $file
69
     * @return  EntityMetadata
70
     */
71
    protected function readFile($file)
72
    {
73
        return include $file;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function putMetadataInCache(EntityMetadata $metadata)
80
    {
81
        $this->writeFile($metadata, '<?php return unserialize('.var_export(serialize($metadata), true).');');
82
        return $this;
83
    }
84
85
    /**
86
     * Writes the cache file.
87
     *
88
     * @param   EntityMetadata  $metadata
89
     * @param   string          $contents
90
     */
91
    protected function writeFile(EntityMetadata $metadata, $contents)
92
    {
93
        $file = $this->getCacheFile($metadata->type);
94
        $tmpFile = tempnam($this->dir, 'metadata-cache');
95
        file_put_contents($tmpFile, $contents);
96
        chmod($tmpFile, 0666 & ~umask());
97
        $this->renameFile($tmpFile, $file);
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public function evictMetadataFromCache(EntityMetadata $metadata)
104
    {
105
        $file = $this->getCacheFile($metadata->type);
106
        if (file_exists($file)) {
107
            return unlink($file);
108
        }
109
        return false;
110
    }
111
112
    /**
113
     * Gets the cache file from the entity type.
114
     *
115
     * @param   string  $type
116
     * @return  string
117
     */
118
    private function getCacheFile($type)
119
    {
120
        return $this->dir.'/ModlrData.'.$this->cachePrefix.'.'.$type.'.'.$this->extension;
121
    }
122
123
    /**
124
     * Renames a file
125
     *
126
     * @param  string $source
127
     * @param  string $target
128
     * @throws \RuntimeException
129
     */
130
    private function renameFile($source, $target)
131
    {
132
        if (false === @rename($source, $target)) {
133
            throw new RuntimeException(sprintf('Could not write new cache file to %s.', $target));
134
        }
135
    }
136
}
137