FileCache::__destruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Innmind\RestBundle\Client\Server\Cache;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class FileCache extends InMemoryCache
8
{
9
    protected $filePath;
10
    protected $filesystem;
11
    protected $isFresh = true;
12
13 10
    public function __construct($filePath)
14
    {
15 10
        $this->filePath = (string) $filePath;
16 10
        $this->filesystem = new Filesystem;
17
18 10
        if ($this->filesystem->exists($this->filePath)) {
19 4
            $this->data = require $this->filePath;
20 4
            $this->isFresh = false;
21 4
        }
22 10
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function isFresh()
28
    {
29 2
        return $this->isFresh;
30
    }
31
32
    /**
33
     * Dump the couples to the file
34
     *
35
     * @return void
36
     */
37 6
    public function __destruct()
38
    {
39 6
        $dump = var_export($this->data, true);
40
        $code = <<<PHP
41
<?php
42
43
return $dump;
44
45 6
PHP;
46
47 6
        $this->filesystem->dumpFile($this->filePath, $code);
48 6
    }
49
}
50