Completed
Push — V6 ( 86d7ee...00097a )
by Georges
02:12
created

Driver::getStats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Drivers\Files;
16
17
use phpFastCache\Core\Pool\DriverBaseTrait;
18
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
19
use phpFastCache\Core\Pool\IO\IOHelperTrait;
20
use phpFastCache\Entities\driverStatistic;
21
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
22
use phpFastCache\Exceptions\phpFastCacheDriverException;
23
use phpFastCache\Exceptions\phpFastCacheIOException;
24
use phpFastCache\Util\Directory;
25
use Psr\Cache\CacheItemInterface;
26
27
/**
28
 * Class Driver
29
 * @package phpFastCache\Drivers
30
 */
31
class Driver implements ExtendedCacheItemPoolInterface
32
{
33
    use DriverBaseTrait, IOHelperTrait;
34
35
    /**
36
     *
37
     */
38
    const FILE_DIR = 'files';
39
40
    /**
41
     * Driver constructor.
42
     * @param array $config
43
     * @throws phpFastCacheDriverException
44
     */
45
    public function __construct(array $config = [])
46
    {
47
        $this->setup($config);
48
49
        if (!$this->driverCheck()) {
50
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
51
        }
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function driverCheck()
58
    {
59
        return is_writable($this->getPath()) || @mkdir($this->getPath(), $this->setChmodAuto(), true);
60
    }
61
62
    /**
63
     * @param \Psr\Cache\CacheItemInterface $item
64
     * @return mixed
65
     * @throws \InvalidArgumentException
66
     */
67
    protected function driverWrite(CacheItemInterface $item)
68
    {
69
        /**
70
         * Check for Cross-Driver type confusion
71
         */
72
        if ($item instanceof Item) {
73
            $file_path = $this->getFilePath($item->getKey());
74
            $data = $this->encode($this->driverPreWrap($item));
75
76
            /**
77
             * Force write
78
             */
79
            try {
80
                return $this->writefile($file_path, $data, $this->config['secureFileManipulation']);
81
            } catch (\Exception $e) {
82
                return false;
83
            }
84
        } else {
85
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
86
        }
87
    }
88
89
    /**
90
     * @param \Psr\Cache\CacheItemInterface $item
91
     * @return mixed
92
     */
93
    protected function driverRead(CacheItemInterface $item)
94
    {
95
        /**
96
         * Check for Cross-Driver type confusion
97
         */
98
        $file_path = $this->getFilePath($item->getKey());
99
        if (!file_exists($file_path)) {
100
            return null;
101
        }
102
103
        $content = $this->readfile($file_path);
104
105
        return $this->decode($content);
106
107
    }
108
109
    /**
110
     * @param \Psr\Cache\CacheItemInterface $item
111
     * @return bool
112
     * @throws \InvalidArgumentException
113
     */
114
    protected function driverDelete(CacheItemInterface $item)
115
    {
116
        /**
117
         * Check for Cross-Driver type confusion
118
         */
119
        if ($item instanceof Item) {
120
            $file_path = $this->getFilePath($item->getKey(), true);
121
            if (file_exists($file_path) && @unlink($file_path)) {
122
                return true;
123
            } else {
124
                return false;
125
            }
126
        } else {
127
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
128
        }
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    protected function driverClear()
135
    {
136
        return (bool) Directory::rrmdir($this->getPath(true));
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    protected function driverConnect()
143
    {
144
        return true;
145
    }
146
147
    /**
148
     * @param string $optionName
149
     * @param mixed $optionValue
150
     * @return bool
151
     * @throws \InvalidArgumentException
152
     */
153
    public static function isValidOption($optionName, $optionValue)
154
    {
155
        DriverBaseTrait::isValidOption($optionName, $optionValue);
156
        switch ($optionName) {
157
            case 'path':
158
                return is_string($optionValue);
159
                break;
160
161
            case 'default_chmod':
162
                return is_numeric($optionValue);
163
                break;
164
165
            case 'securityKey':
166
                return is_string($optionValue);
167
                break;
168
            case 'htaccess':
169
                return is_bool($optionValue);
170
                break;
171
172
            case 'secureFileManipulation':
173
                return is_bool($optionValue);
174
                break;
175
176
            default:
177
                return false;
178
                break;
179
        }
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public static function getValidOptions()
186
    {
187
        return ['path', 'default_chmod', 'securityKey', 'htaccess', 'secureFileManipulation'];
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public static function getRequiredOptions()
194
    {
195
        return ['path'];
196
    }
197
}