Completed
Push — 2.0 ( a00819...62c098 )
by Peter
07:50 queued 05:05
created

FileStorage::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service\Client\Cache\Storage;
10
11
use Symfony\Component\Filesystem\Filesystem;
12
13
class FileStorage implements StorageInterface
14
{
15
    /**
16
     * @var Filesystem
17
     */
18
    protected $fs;
19
20
    /**
21
     * @var string
22
     */
23
    protected $cache_dir = '';
24
25
    /**
26
     * @param Filesystem $fs
27
     * @param string $cache_dir
28
     */
29 5
    public function __construct(Filesystem $fs, $cache_dir)
30
    {
31 5
        $this->fs = $fs;
32 5
        $this->cache_dir = $cache_dir;
33 5
    }
34
35
    /**
36
     * @param string $key
37
     *
38
     * @return string|null
39
     */
40 5
    public function get($key)
41
    {
42 5
        $filename = $this->getFilename($key);
43 5
        if ($this->fs->exists($filename) && filemtime($filename) >= time()) {
44 2
            return file_get_contents($filename);
45
        }
46
47 3
        return null;
48
    }
49
50
    /**
51
     * @param string $key
52
     * @param string $data
53
     * @param \DateTime $expires
54
     */
55 1
    public function set($key, $data, \DateTime $expires)
56
    {
57 1
        $filename = $this->getFilename($key);
58 1
        $this->fs->dumpFile($filename, $data);
59 1
        $this->fs->touch($filename, $expires->getTimestamp());
60 1
    }
61
62
    /**
63
     * @param string $key
64
     *
65
     * @return string
66
     */
67 5
    protected function getFilename($key)
68
    {
69 5
        return $this->cache_dir.md5($key).'.xml';
70
    }
71
}
72