Completed
Pull Request — master (#357)
by Anton
03:23
created

PhpFile::doSet()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.3256
Metric Value
dl 0
loc 31
ccs 13
cts 17
cp 0.7647
rs 8.439
cc 5
eloc 17
nc 6
nop 3
crap 5.3256
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Cache\Adapter;
13
14
use Bluz\Cache\Cache;
15
use Bluz\Cache\CacheException;
16
17
/**
18
 * Adapter that caches data into php array
19
 *
20
 * It can cache data that support var_export.
21
 * This adapter very fast and cacheable by opcode cachers but it have some limitations related to var_export.
22
 * It's best to use for scalar data caching
23
 *
24
 * @package Bluz\Cache\Adapter
25
 * @author  murzik
26
 * @link    http://php.net/manual/en/function.var-export.php
27
 * @link    http://php.net/manual/en/language.oop5.magic.php#object.set-state
28
 */
29
class PhpFile extends FileBase
30
{
31
    /**
32
     * @var array cache data
33
     */
34
    protected $data = array();
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @param  string $id
40
     * @return bool
41
     */
42 2
    protected function doContains($id)
43
    {
44 2
        $filename = $this->getFilename($id);
45
46 2
        if (!is_file($filename)) {
47 1
            return false;
48
        }
49
50 2
        $cacheEntry = include $filename;
51
52 2
        return $cacheEntry['ttl'] === Cache::TTL_NO_EXPIRY || $cacheEntry['ttl'] > time();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @param string $id
59
     * @return bool|mixed
60
     */
61 3
    protected function doGet($id)
62
    {
63 3
        $filename = $this->getFilename($id);
64
65 3
        if (!is_file($filename)) {
66 2
            return false;
67
        }
68
69 2
        if (defined('HHVM_VERSION')) {
70
            // XXX: workaround for https://github.com/facebook/hhvm/issues/1447
71
            $cacheEntry = eval(str_replace('<?php', '', file_get_contents($filename)));
72
        } else {
73 2
            $cacheEntry = include $filename;
74
        }
75
76 2
        if ($cacheEntry['ttl'] !== Cache::TTL_NO_EXPIRY && $cacheEntry['ttl'] < time()) {
77
            return false;
78
        }
79
80 2
        return $cacheEntry['data'];
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @param string $id
87
     * @param mixed $data
88
     * @param integer $ttl
89
     * @return integer The number of bytes that were written to the file, or false on failure.
90
     * @throws CacheException
91
     */
92 3
    protected function doSet($id, $data, $ttl = Cache::TTL_NO_EXPIRY)
93
    {
94 3
        if ($ttl > 0) {
95
            $ttl = time() + $ttl;
96
        }
97
98
        // if we have an array containing objects - we will have a problem.
99 3
        if (is_object($data) && !method_exists($data, '__set_state')) {
100
            throw new CacheException(
101
                "Invalid argument given, PhpFileAdapter only allows objects that implement __set_state() " .
102
                "and fully support var_export()."
103
            );
104
        }
105
106 3
        $fileName = $this->getFilename($id);
107 3
        $filePath = pathinfo($fileName, PATHINFO_DIRNAME);
108
109 3
        if (!is_dir($filePath)) {
110 2
            mkdir($filePath, 0777, true);
111 2
        }
112
113
        $cacheEntry = array(
114 3
            'ttl' => $ttl,
115
            'data' => $data
116 3
        );
117
118 3
        $cacheEntry = var_export($cacheEntry, true);
119 3
        $code = sprintf('<?php return %s;', $cacheEntry);
120
121 3
        return $this->writeFile($fileName, $code);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     *
127
     * @param string $id
128
     * @param mixed $data
129
     * @param integer $ttl
130
     * @return bool|int
131
     * @throws CacheException
132
     */
133 1
    protected function doAdd($id, $data, $ttl = Cache::TTL_NO_EXPIRY)
134
    {
135 1
        if (!$this->doContains($id)) {
136 1
            return $this->doSet($id, $data, $ttl);
137
        } else {
138
            return false;
139
        }
140
    }
141
}
142