Passed
Push — v9 ( 467cbb...1836cb )
by Georges
03:07 queued 24s
created

Driver::getConfig()   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
 *
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 ExtendedCacheItemPoolInterface, 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 null|array
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
        $this->assertCacheItemType($item, Item::class);
87
88
        $filePath = $this->getFilePath($item->getKey());
89
        $data = $this->encode($this->driverPreWrap($item));
90
91
        try {
92
            return $this->writeFile($filePath, $data, $this->getConfig()->isSecureFileManipulation());
93
        } catch (Exception) {
94
            return false;
95
        }
96
    }
97
98
    /**
99
     * @param ExtendedCacheItemInterface $item
100
     * @return bool
101
     * @throws PhpfastcacheIOException
102
     * @throws PhpfastcacheInvalidArgumentException
103
     */
104
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
105
    {
106
        $this->assertCacheItemType($item, Item::class);
107
108
        $filePath = $this->getFilePath($item->getKey(), true);
109
        if (\file_exists($filePath) && @\unlink($filePath)) {
110
            \clearstatcache(true, $filePath);
111
            $dir = \dirname($filePath);
112
            if (!(new FilesystemIterator($dir))->valid()) {
113
                \rmdir($dir);
114
            }
115
            return true;
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * @return bool
123
     * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException
124
     * @throws PhpfastcacheInvalidArgumentException
125
     */
126
    protected function driverClear(): bool
127
    {
128
        return Directory::rrmdir($this->getPath(true));
129
    }
130
}
131