Completed
Pull Request — develop (#273)
by Samuel
25:26 queued 13:47
created

TimeCacheStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 10 2
A save() 0 4 1
A isExpired() 0 12 3
1
<?php
2
/**
3
 * TimeCacheStrategy
4
 */
5
6
namespace Graviton\ProxyBundle\Definition\Loader\CacheStrategy;
7
8
use Gaufrette\Exception\FileNotFound;
9
use Gaufrette\Filesystem;
10
11
/**
12
 * cache content with a defined length of time
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class TimeCacheStrategy implements CacheStrategyInterface
19
{
20
    /**
21
     * @var Filesystem
22
     */
23
    private $gaufrette;
24
25
    /**
26
     * @var int
27
     */
28
    private $cacheTime;
29
30
    /**
31
     * constructor
32
     *
33
     * @param Filesystem $gaufrette     gaufrette filesystem
34
     * @param string     $cacheDuration how long is the cached file valid
35
     */
36
    public function __construct(Filesystem $gaufrette, $cacheDuration)
37
    {
38
        $this->gaufrette = $gaufrette;
39
        $this->cacheTime = (int) $cacheDuration;
40
    }
41
42
    /**
43
     * return the cached content
44
     *
45
     * @param string $key key
46
     *
47
     * @return string content of the cached file
48
     */
49
    public function get($key)
50
    {
51
        try {
52
            $content = $this->gaufrette->read($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->gaufrette->read($key); of type string|boolean adds the type boolean to the return on line 57 which is incompatible with the return type declared by the interface Graviton\ProxyBundle\Def...eStrategyInterface::get of type string.
Loading history...
53
        } catch (FileNotFound $e) {
54
            $content = '';
55
        }
56
57
        return $content;
58
    }
59
60
    /**
61
     * cache content
62
     *
63
     * @param string $key     key
64
     * @param string $content content to cache
65
     *
66
     * @return void
67
     */
68
    public function save($key, $content)
69
    {
70
        $this->gaufrette->write($key, $content, true);
71
    }
72
73
    /**
74
     * check whether cached content is expired
75
     *
76
     * @param string $key key
77
     *
78
     * @return bool
79
     */
80
    public function isExpired($key)
81
    {
82
        $retVal = true;
83
        if ($this->gaufrette->has($key)) {
84
            $mTime = $this->gaufrette->mtime($key);
85
            if (($mTime + $this->cacheTime) >= time()) {
86
                $retVal = false;
87
            }
88
        }
89
90
        return $retVal;
91
    }
92
}
93