Completed
Push — master ( 18312a...f64dc4 )
by Lars
03:45
created

AdapterFile::deleteFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * AdapterFile: File-adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterFile implements iAdapter
11
{
12
  const CACHE_FILE_PREFIX = '__';
13
  const CACHE_FILE_SUBFIX = '.php.cache';
14
15
  /**
16
   * @var bool
17
   */
18
  public $installed = false;
19
20
  /**
21
   * @var string
22
   */
23
  protected $cacheDir;
24
25
  /**
26
   * @var iSerializer
27
   */
28
  protected $serializer;
29
30
  /**
31
   * @param string $cacheDir
32
   */
33 6
  public function __construct($cacheDir = null)
34
  {
35 6
    $this->serializer = new SerializerIgbinary();
36
37 6
    if (!$cacheDir) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cacheDir of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
38 6
      $cacheDir = realpath(sys_get_temp_dir()) . '/cache';
39 6
    }
40
41 6
    $this->cacheDir = (string)$cacheDir;
42
43 6
    if ($this->createCacheDirectory($cacheDir) === true) {
44 1
      $this->installed = true;
45 1
    }
46 6
  }
47
48
  /**
49
   * @param string $key
50
   *
51
   * @param        $key
52
   *
53
   * @return bool
54
   */
55
  public function remove($key)
56
  {
57
    $cacheFile = $this->getFileName($key);
58
59
    return $this->deleteFile($cacheFile);
60
  }
61
62
  /**
63
   * @param string $key
64
   *
65
   * @return mixed
66
   */
67 4
  public function get($key)
68
  {
69 4
    $path = $this->getFileName($key);
70
71 4
    if (!file_exists($path)) {
72
      return null;
73
    }
74
75 4
    $data = $this->serializer->unserialize(file_get_contents($path));
76
77 4
    if (!$data || !$this->validateDataFromCache($data)) {
78
      return null;
79
    }
80
81 4
    if ($this->ttlHasExpired($data['ttl']) === true) {
82
      $this->remove($key);
83
      
84
      return null;
85
    }
86
87 4
    return $data['value'];
88
  }
89
90
  /**
91
   * @param $data
92
   *
93
   * @return bool
94
   */
95 4
  protected function validateDataFromCache($data)
96
  {
97 4
    if (!is_array($data)) {
98
      return false;
99
    }
100
101 4
    foreach (array('value', 'ttl') as $missing) {
102 4
      if (!array_key_exists($missing, $data)) {
103
        return false;
104
      }
105 4
    }
106
107 4
    return true;
108
  }
109
110
  /**
111
   * @param string $key
112
   *
113
   * @return bool
114
   */
115 1
  public function exists($key)
116
  {
117 1
    return null !== $this->get($key);
118
  }
119
120
  /**
121
   * @param string $key
122
   * @param mixed  $value
123
   *
124
   * @return bool
125
   */
126 2
  public function set($key, $value)
127
  {
128 2
    return $this->setExpired($key, $value);
129
  }
130
131
  /**
132
   * @param string $key
133
   * @param mixed  $value
134
   * @param int    $ttl
135
   *
136
   * @return bool
137
   */
138 3
  public function setExpired($key, $value, $ttl = 0)
139
  {
140 3
    $item = $this->serializer->serialize(
141
        array(
142 3
            'value' => $value,
143 3
            'ttl'   => $ttl ? (int)$ttl + time() : 0,
144
        )
145 3
    );
146
147 3
    if (!file_put_contents($this->getFileName($key), $item)) {
148
      return false;
149
    }
150
151 3
    return true;
152
  }
153
154 6
  protected function createCacheDirectory($path)
155
  {
156 6
    if (!@mkdir($path, 0777, true) || !is_dir($path)) {
157 5
      return false;
158
    }
159
160 1
    if (!is_writable($path)) {
161
      return false;
162
    }
163
164 1
    return true;
165
  }
166
167
  /**
168
   * @param $cacheFile
169
   *
170
   * @return bool
171
   */
172
  protected function deleteFile($cacheFile)
173
  {
174
    if (is_file($cacheFile)) {
175
      return unlink($cacheFile);
176
    }
177
178
    return false;
179
  }
180
181
  /**
182
   * @param string $key
183
   *
184
   * @return string
185
   */
186 5
  protected function getFileName($key)
187
  {
188 5
    return $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . self::CACHE_FILE_SUBFIX;
189
  }
190
191
  /**
192
   * @param $ttl
193
   *
194
   * @return bool
195
   */
196 4
  protected function ttlHasExpired($ttl)
197
  {
198 4
    if ($ttl === 0) {
199 3
      return false;
200
    }
201
202 1
    return (time() > $ttl);
203
  }
204
205
  /**
206
   * check if cache is installed
207
   *
208
   * @return boolean
209
   */
210
  public function installed()
211
  {
212
    return $this->installed;
213
  }
214
}