Completed
Push — master ( 00fae0...ee941a )
by Dan
23:33 queued 15:03
created

FileStorage::has()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Src/Cache/Storage/FileStorage.php
4
 *
5
 * @package     Ds\Cache\Storage
6
 * @subpackage  Cache
7
 * @author      Dan Smith <[email protected]>
8
 * @version     v.1 (20/03/2017)
9
 * @copyright   Copyright (c) 2017, Dan Smith
10
 */
11
namespace Ds\Cache\Storage;
12
13
use \DateInterval;
14
15
/**
16
 * Class NullStorage
17
 *
18
 * @package Ds\Cache
19
 */
20
class FileStorage extends AbstractStorage
21
{
22
    /**
23
     * @var string
24
     */
25
    private $fs;
26
27
    private $ext;
28
29
    /**
30
     * File Storage constructor.
31
     *
32
     * @param string $file
33
     * @param DateInterval $ttl
34
     * @throws \Exception
35
     */
36 7
    public function __construct(string $file, DateInterval $ttl)
37
    {
38 7
        $this->fs = $file;
39 7
        $this->ext = 'cache';
40
41 7
        if(!@mkdir($this->fs, 0755) && !is_dir($this->fs)){
42 1
            throw new \Exception('unable to create directory');
43
        }
44
45 7
        parent::__construct($ttl);
46 7
    }
47
48
    /**
49
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
50
     *
51
     * @param string   $key   The key of the item to store.
52
     * @param mixed    $value The value of the item to store, must be serializable.
53
     * @param null|int $ttl   Optional. The TTL value of this item. If no value is sent and
54
     *                                     the driver supports TTL then the library may set a default value
55
     *                                     for it or let the driver take care of that.
56
     *
57
     * @return bool True on success and false on failure.
58
     */
59 4
    public function set($key, $value, $ttl = null)
60
    {
61 4
        $ttl = time() + $this->getTtlTimestamp($ttl);
62
63 4
        $cacheItem = $this->_createCacheItem($key, $value, $ttl);
64
65 4
        \file_put_contents($cacheItem['file'],$cacheItem['data']);
66
67 4
        if (!file_exists($cacheItem['file'])){
68
            return false;
69
        }
70
71 4
        return true;
72
    }
73
74 4
    private function _createCacheItem(string $key, $value, int $ttl){
75
76 4
        $filename = implode('.',[$key,$this->ext]);
77
78
        return [
79 4
            'file' => $this->fs . DIRECTORY_SEPARATOR . $filename,
80 4
            'data' => json_encode([$ttl, $value])
81
        ];
82
    }
83
84
    /**
85
     * Determines whether an item is present in the cache.
86
     *
87
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
88
     * and not to be used within your live applications operations for get/set, as this method
89
     * is subject to a race condition where your has() will return true and immediately after,
90
     * another script can remove it making the state of your app out of date.
91
     *
92
     * @param string $key The cache item key.
93
     *
94
     * @return bool
95
     */
96 4
    public function has($key){
97
98 4
       $cacheFile = implode('.',[$key,$this->ext]);
99 4
       $cacheLocation = $this->fs . DIRECTORY_SEPARATOR . $cacheFile;
100
101 4
       if (!file_exists($cacheLocation)){
102 1
           return false;
103
       }
104
105 3
       $data = \file_get_contents($cacheLocation);
106 3
       $cacheData = json_decode($data, TRUE);
107
108
       //cache has expired
109 3
       if ($cacheData[0] < time() ){
110 1
           \unlink($cacheLocation);
111 1
           return false;
112
       }
113
114 2
       return true;
115
    }
116
117
    /**
118
     * Fetches a value from the cache.
119
     *
120
     * @param string $key     The unique key of this item in the cache.
121
     *
122
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
123
     *
124
     */
125 1
    public function get($key){
126
127 1
        $cacheFile = implode('.',[$key,$this->ext]);
128 1
        $cacheLocation = $this->fs . DIRECTORY_SEPARATOR . $cacheFile;
129
130 1
        if ($this->has($key)){
131 1
            $data = \file_get_contents($cacheLocation);
132 1
            $cacheData = json_decode($data, TRUE);
133 1
            return $cacheData[1];
134
        }
135
136
        return null;
137
    }
138
139
140
141
    /**
142
     * Delete an item from the cache by its unique key.
143
     *
144
     * @param string $key The unique cache key of the item to delete.
145
     *
146
     * @return bool True if the item was successfully removed. False if there was an error.
147
     */
148
    public function delete($key)
149
    {
150
        return true;
151
    }
152
153
    /**
154
     * Wipes clean the entire cache's keys.
155
     *
156
     * @return bool True on success and false on failure.
157
     */
158 1
    public function clear(){
159 1
        if (is_dir( $this->fs )){
160 1
            array_map('unlink', glob("$this->fs/*.*"));
161
        }
162
163 1
        $data = glob("$this->fs/*.*");
164
165 1
        if (!empty($data)){
166
            return false;
167
        }
168
169 1
        return true;
170
    }
171
}
172