FileDriver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _get() 0 18 3
A _set() 0 7 1
A packData() 0 9 2
A unpackData() 0 10 3
A createFileName() 0 4 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