Passed
Push — v7 ( e0234b...459005 )
by Georges
02:02
created

Driver::getRequiredOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
declare(strict_types=1);
15
16
namespace Phpfastcache\Drivers\Files;
17
18
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface, IO\IOHelperTrait};
19
use Phpfastcache\Exceptions\{
20
  PhpfastcacheInvalidArgumentException
21
};
22
use Phpfastcache\Util\Directory;
23
use Psr\Cache\CacheItemInterface;
24
25
/**
26
 * Class Driver
27
 * @package phpFastCache\Drivers
28
 * @property Config $config Config object
29
 * @method Config getConfig() Return the config object
30
 */
31
class Driver implements ExtendedCacheItemPoolInterface
32
{
33
    use IOHelperTrait;
34
    use DriverBaseTrait {
35
        DriverBaseTrait::__construct as private __parentConstruct;
36
    }
37
    /**
38
     *
39
     */
40
    const FILE_DIR = 'files';
41
42
    /**
43
     * Driver constructor.
44
     * @param Config $config
45
     * @param string $instanceId
46
     */
47
    public function __construct(Config $config, string $instanceId)
48
    {
49
        $this->__parentConstruct($config, $instanceId);
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function driverCheck(): bool
56
    {
57
        return \is_writable($this->getPath()) || @\mkdir($this->getPath(), $this->getDefaultChmod(), true);
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    protected function driverConnect(): bool
64
    {
65
        return true;
66
    }
67
68
    /**
69
     * @param \Psr\Cache\CacheItemInterface $item
70
     * @return null|array
71
     */
72
    protected function driverRead(CacheItemInterface $item)
73
    {
74
        /**
75
         * Check for Cross-Driver type confusion
76
         */
77
        $file_path = $this->getFilePath($item->getKey(), true);
78
        if (!\file_exists($file_path)) {
79
            return null;
80
        }
81
82
        $content = $this->readfile($file_path);
83
84
        return $this->decode($content);
85
86
    }
87
88
    /**
89
     * @param \Psr\Cache\CacheItemInterface $item
90
     * @return bool
91
     * @throws PhpfastcacheInvalidArgumentException
92
     */
93
    protected function driverWrite(CacheItemInterface $item): bool
94
    {
95
        /**
96
         * Check for Cross-Driver type confusion
97
         */
98
        if ($item instanceof Item) {
99
            $file_path = $this->getFilePath($item->getKey());
100
            $data = $this->encode($this->driverPreWrap($item));
101
102
            /**
103
             * Force write
104
             */
105
            try {
106
                return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation());
107
            } catch (\Exception $e) {
108
                return false;
109
            }
110
        }
111
112
        throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
113
    }
114
115
    /**
116
     * @param \Psr\Cache\CacheItemInterface $item
117
     * @return bool
118
     * @throws PhpfastcacheInvalidArgumentException
119
     */
120
    protected function driverDelete(CacheItemInterface $item): bool
121
    {
122
        /**
123
         * Check for Cross-Driver type confusion
124
         */
125
        if ($item instanceof Item) {
126
            $file_path = $this->getFilePath($item->getKey(), true);
127
            if (\file_exists($file_path) && @\unlink($file_path)) {
128
                $dir = \dirname($file_path);
129
                if (!(new \FilesystemIterator($dir))->valid()) {
130
                    \rmdir($dir);
131
                }
132
                return true;
133
            }
134
135
            return false;
136
        }
137
138
        throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    protected function driverClear(): bool
145
    {
146
        return Directory::rrmdir($this->getPath(true));
147
    }
148
}
149