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

PhpFileCache::includeFileForId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 16
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use function is_object;
6
use function method_exists;
7
use function restore_error_handler;
8
use function serialize;
9
use function set_error_handler;
10
use function sprintf;
11
use function time;
12
use function var_export;
13
14
/**
15
 * Php file cache driver.
16
 */
17
class PhpFileCache extends FileCache
18
{
19
    public const EXTENSION = '.doctrinecache.php';
20
21
    /**
22
     * @var callable
23
     *
24
     * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
25
     */
26
    private static $emptyErrorHandler;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
32
    {
33
        parent::__construct($directory, $extension, $umask);
34
35
        self::$emptyErrorHandler = function () {
36
        };
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 80
    protected function doFetch($id)
43
    {
44 80
        $value = $this->includeFileForId($id);
45
46 80
        if ($value === null) {
47 80
            return false;
48 80
        }
49
50
        if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
51
            return false;
52
        }
53 78
54
        return $value['data'];
55 78
    }
56
57 78
    /**
58 78
     * {@inheritdoc}
59
     */
60
    protected function doContains($id)
61 66
    {
62
        $value = $this->includeFileForId($id);
63
64
        if ($value === null) {
65 66
            return false;
66
        }
67
68
        return $value['lifetime'] === 0 || $value['lifetime'] > time();
69
    }
70
71 69
    /**
72
     * {@inheritdoc}
73 69
     */
74
    protected function doSave($id, $data, $lifeTime = 0)
75 69
    {
76 48
        if ($lifeTime > 0) {
77
            $lifeTime = time() + $lifeTime;
78
        }
79 65
80
        $filename = $this->getFilename($id);
81
82
        $value = [
83
            'lifetime'  => $lifeTime,
84
            'data'      => $data,
85 76
        ];
86
87 76
        if (is_object($data) && method_exists($data, '__set_state')) {
88 3
            $value = var_export($value, true);
89
            $code  = sprintf('<?php return %s;', $value);
90
        } else {
91 76
            $value = var_export(serialize($value), true);
92
            $code  = sprintf('<?php return unserialize(%s);', $value);
93
        }
94 76
95 76
        return $this->writeFile($filename, $code);
96
    }
97
98 76
    /**
99 1
     * @return array|null
100 1
     */
101
    private function includeFileForId(string $id) : ?array
102 75
    {
103 75
        $fileName = $this->getFilename($id);
104
105
        // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
106 76
        set_error_handler(self::$emptyErrorHandler);
107
108
        $value = include $fileName;
109
110
        restore_error_handler();
111
112
        if (! isset($value['lifetime'])) {
113
            return null;
114 78
        }
115
116 78
        return $value;
117
    }
118
}
119