Passed
Branch 5.x (508359)
by Phil
24:20
created

FileCache   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 13
c 1
b 0
f 0
dl 0
loc 56
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMultiple() 0 3 1
A __construct() 0 4 1
A delete() 0 3 1
A get() 0 3 2
A deleteMultiple() 0 3 1
A has() 0 3 2
A set() 0 3 1
A setMultiple() 0 3 1
A clear() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route\Cache;
6
7
use Psr\SimpleCache\CacheInterface;
8
9
class FileCache implements CacheInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $cacheFilePath;
15
16
    /**
17
     * @var integer
18
     */
19
    protected $ttl;
20
21
    public function __construct(string $cacheFilePath, int $ttl)
22
    {
23
        $this->cacheFilePath = $cacheFilePath;
24
        $this->ttl = $ttl;
25
    }
26
27
    public function get($key, $default = null)
28
    {
29
        return ($this->has($key)) ? file_get_contents($this->cacheFilePath) : $default;
30
    }
31
32
    public function set($key, $value, $ttl = null): bool
33
    {
34
        return (bool) file_put_contents($this->cacheFilePath, $value);
35
    }
36
37
    public function has($key): bool
38
    {
39
        return file_exists($this->cacheFilePath) && time() - filemtime($this->cacheFilePath) < $this->ttl;
40
    }
41
42
    public function delete($key): bool
43
    {
44
        return unlink($this->cacheFilePath);
45
    }
46
47
    public function clear(): bool
48
    {
49
        return $this->delete($this->cacheFilePath);
50
    }
51
52
    public function getMultiple($keys, $default = null): iterable
53
    {
54
        return [];
55
    }
56
57
    public function setMultiple($values, $ttl = null): bool
58
    {
59
        return false;
60
    }
61
62
    public function deleteMultiple($keys): bool
63
    {
64
        return false;
65
    }
66
}
67