FileDriver::_get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
3
namespace Wechat\CacheDriver;
4
5
use Wechat\CacheDriver\BaseDriver;
6
7
/**
8
 * 文件缓存驱动.
9
 *
10
 */
11
class FileDriver extends BaseDriver
12
{
13
    /**
14
     * @param string $name
15
     *
16
     * @return bool|null|string|void
17
     */
18
    public function _get($name)
19
    {
20
        $name = $this->createFileName($name);
21
        $file = $this->cacheDir . '/' . $name;
22
        $data = @file_get_contents($file);
23
        if (!$data) {
24
            return false;
25
        }
26
27
        $data = $this->unpackData($data);
28
        if (false === $data) {
29
            @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
30
31
            $data = null;
32
        }
33
34
        return $data;
35
    }
36
37
    /**
38
     * 根据缓存名 设置缓存值和超时时间.
39
     *
40
     * @param string $name    缓存名
41
     * @param void   $value   缓存值
42
     * @param int    $expires 超时时间
43
     *
44
     * @return boolean;
0 ignored issues
show
Documentation introduced by
The doc-type boolean; could not be parsed: Expected "|" or "end of type", but got ";" at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
45
     */
46
    public function _set($name, $value, $expires)
47
    {
48
        $name = $this->createFileName($name);
49
        $data = $this->packData($value, $expires);
50
51
        return file_put_contents($this->cacheDir . '/' . $name, $data);
52
    }
53
54
    /**
55
     * 数据打包.
56
     *
57
     * @param void $data    缓存值
58
     * @param int  $expires 超时时间
59
     *
60
     * @return string
61
     */
62
    private function packData($data, $expires)
63
    {
64
        $str            = [];
65
        $str['data']    = $data;
66
        $str['expires'] = $expires === 0 ? 0 : time() + $expires;
67
        $str            = serialize($str);
68
69
        return $str;
70
    }
71
72
    /**
73
     * @param $data
74
     *
75
     * @return bool
76
     */
77
    private function unpackData($data)
78
    {
79
        $arr = unserialize($data);
80
81
        if (time() > $arr['expires'] && $arr['expires'] !== 0) {
82
            return false;
83
        }
84
85
        return $arr['data'];
86
    }
87
88
    /**
89
     * 创建缓存文件名.
90
     *
91
     * @param string $name 缓存名
92
     *
93
     * @return string
94
     */
95
    private function createFileName($name)
96
    {
97
        return md5($name);
98
    }
99
}
100