Passed
Push — master ( b2988e...9f380c )
by Jonathan
33:05
created

FilesystemCache::doFetch()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 7
nop 1
dl 0
loc 30
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use const PHP_EOL;
6
use function fclose;
7
use function fgets;
8
use function fopen;
9
use function is_file;
10
use function serialize;
11
use function time;
12
use function unserialize;
13
14
/**
15
 * Filesystem cache driver.
16
 */
17
class FilesystemCache extends FileCache
18
{
19
    public const EXTENSION = '.doctrinecache.data';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
25
    {
26
        parent::__construct($directory, $extension, $umask);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function doFetch($id)
33
    {
34
        $data     = '';
35 83
        $lifetime = -1;
36
        $filename = $this->getFilename($id);
37 83
38 83
        if (! is_file($filename)) {
39
            return false;
40
        }
41
42
        $resource = fopen($filename, 'r');
43 81
        $line     = fgets($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $line     = fgets(/** @scrutinizer ignore-type */ $resource);
Loading history...
44
45 81
        if ($line !== false) {
46 81
            $lifetime = (int) $line;
47 81
        }
48
49 81
        if ($lifetime !== 0 && $lifetime < time()) {
50 81
            fclose($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            fclose(/** @scrutinizer ignore-type */ $resource);
Loading history...
51
52
            return false;
53 68
        }
54
55 68
        while (($line = fgets($resource)) !== false) {
56 68
            $data .= $line;
57
        }
58
59 68
        fclose($resource);
60
61
        return unserialize($data);
62
    }
63
64
    /**
65 68
     * {@inheritdoc}
66 68
     */
67
    protected function doContains($id)
68
    {
69 68
        $lifetime = -1;
70
        $filename = $this->getFilename($id);
71 68
72
        if (! is_file($filename)) {
73
            return false;
74
        }
75
76
        $resource = fopen($filename, 'r');
77 72
        $line     = fgets($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        $line     = fgets(/** @scrutinizer ignore-type */ $resource);
Loading history...
78
79 72
        if ($line !== false) {
80 72
            $lifetime = (int) $line;
81
        }
82 72
83 51
        fclose($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        fclose(/** @scrutinizer ignore-type */ $resource);
Loading history...
84
85
        return $lifetime === 0 || $lifetime > time();
86 67
    }
87
88 67
    /**
89 67
     * {@inheritdoc}
90
     */
91
    protected function doSave($id, $data, $lifeTime = 0)
92 67
    {
93
        if ($lifeTime > 0) {
94 67
            $lifeTime = time() + $lifeTime;
95
        }
96
97
        $data     = serialize($data);
98
        $filename = $this->getFilename($id);
99
100 79
        return $this->writeFile($filename, $lifeTime . PHP_EOL . $data);
101
    }
102
}
103