Driver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 91
rs 10
c 3
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A driverClear() 0 3 1
A driverConnect() 0 3 1
A driverCheck() 0 3 3
A driverRead() 0 11 2
A driverWrite() 0 10 2
A driverDelete() 0 14 4
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Files;
18
19
use Exception;
20
use FilesystemIterator;
21
use Phpfastcache\Cluster\AggregatablePoolInterface;
22
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
23
use Phpfastcache\Core\Pool\IO\IOHelperTrait;
24
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
25
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
26
use Phpfastcache\Exceptions\PhpfastcacheIOException;
27
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
28
use Phpfastcache\Util\Directory;
29
30
/**
31
 * @method Config getConfig()
32
 *
33
 * Important NOTE:
34
 * We are using getKey instead of getEncodedKey since this backend create filename that are
35
 * managed by defaultFileNameHashFunction and not defaultKeyHashFunction
36
 */
37
class Driver implements AggregatablePoolInterface
38
{
39
    use IOHelperTrait;
40
41
    /**
42
     * @return bool
43
     * @throws PhpfastcacheIOException
44
     * @throws PhpfastcacheInvalidArgumentException
45
     */
46
    public function driverCheck(): bool
47
    {
48
        return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory);
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    protected function driverConnect(): bool
55
    {
56
        return true;
57
    }
58
59
    /**
60
     * @param ExtendedCacheItemInterface $item
61
     * @return ?array<string, mixed>
62
     * @throws PhpfastcacheIOException
63
     */
64
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
65
    {
66
        $filePath = $this->getFilePath($item->getKey(), true);
67
68
        try {
69
            $content = $this->readFile($filePath);
70
        } catch (PhpfastcacheIOException) {
71
            return null;
72
        }
73
74
        return $this->decode($content);
75
    }
76
77
    /**
78
     * @param ExtendedCacheItemInterface $item
79
     * @return bool
80
     * @throws PhpfastcacheIOException
81
     * @throws PhpfastcacheInvalidArgumentException
82
     * @throws PhpfastcacheLogicException
83
     */
84
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
85
    {
86
87
        $filePath = $this->getFilePath($item->getKey());
88
        $data = $this->encode($this->driverPreWrap($item));
89
90
        try {
91
            return $this->writeFile($filePath, $data, $this->getConfig()->isSecureFileManipulation());
92
        } catch (Exception) {
93
            return false;
94
        }
95
    }
96
97
    /**
98
     * @param string $key
99
     * @param string $encodedKey
100
     * @return bool
101
     * @throws PhpfastcacheIOException
102
     * @throws PhpfastcacheInvalidArgumentException
103
     */
104
    protected function driverDelete(string $key, string $encodedKey): bool
105
    {
106
107
        $filePath = $this->getFilePath($key, true);
108
        if (\file_exists($filePath) && @\unlink($filePath)) {
109
            \clearstatcache(true, $filePath);
110
            $dir = \dirname($filePath);
111
            if (!(new FilesystemIterator($dir))->valid()) {
112
                \rmdir($dir);
113
            }
114
            return true;
115
        }
116
117
        return false;
118
    }
119
120
    /**
121
     * @return bool
122
     * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException
123
     * @throws PhpfastcacheInvalidArgumentException
124
     */
125
    protected function driverClear(): bool
126
    {
127
        return Directory::rrmdir($this->getPath(true));
128
    }
129
}
130