Completed
Push — master ( f56795...5a873a )
by Marco
03:36
created

PhpFileCache   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.44%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 104
ccs 38
cts 39
cp 0.9744
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A doFetch() 0 14 4
A doContains() 0 10 3
B doSave() 0 23 4
A includeFileForId() 0 17 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Cache;
21
22
/**
23
 * Php file cache driver.
24
 *
25
 * @since  2.3
26
 * @author Fabio B. Silva <[email protected]>
27
 */
28
class PhpFileCache extends FileCache
29
{
30
    const EXTENSION = '.doctrinecache.php';
31
32
    /**
33
     * @var callable
34
     *
35
     * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
36
     */
37
    private static $emptyErrorHandler;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 76
    public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
43
    {
44 76
        parent::__construct($directory, $extension, $umask);
45
46 76
        self::$emptyErrorHandler = function () {
47 76
        };
48 76
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 74
    protected function doFetch($id)
54
    {
55 74
        $value = $this->includeFileForId($id);
56
57 74
        if (! $value) {
58 74
            return false;
59
        }
60
61 63
        if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
62
            return false;
63
        }
64
65 63
        return $value['data'];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 65
    protected function doContains($id)
72
    {
73 65
        $value = $this->includeFileForId($id);
74
75 65
        if (! $value) {
76 46
            return false;
77
        }
78
79 62
        return $value['lifetime'] === 0 || $value['lifetime'] > time();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 72
    protected function doSave($id, $data, $lifeTime = 0)
86
    {
87 72
        if ($lifeTime > 0) {
88 3
            $lifeTime = time() + $lifeTime;
89 3
        }
90
91 72
        $filename  = $this->getFilename($id);
92
93
        $value = [
94 72
            'lifetime'  => $lifeTime,
95
            'data'      => $data
96 72
        ];
97
98 72
        if (is_object($data) && method_exists($data, '__set_state')) {
99 1
            $value  = var_export($value, true);
100 1
            $code   = sprintf('<?php return %s;', $value);
101 1
        } else {
102 71
            $value  = var_export(serialize($value), true);
103 71
            $code   = sprintf('<?php return unserialize(%s);', $value);
104
        }
105
106 72
        return $this->writeFile($filename, $code);
107
    }
108
109
    /**
110
     * @param string $id
111
     *
112
     * @return array|false
113
     */
114 74
    private function includeFileForId($id)
115
    {
116 74
        $fileName = $this->getFilename($id);
117
118
        // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
119 74
        set_error_handler(self::$emptyErrorHandler);
120
121 74
        $value = include $fileName;
122
123 74
        restore_error_handler();
124
125 74
        if (! isset($value['lifetime'])) {
126 74
            return false;
127
        }
128
129 68
        return $value;
130
    }
131
}
132