Filesystem   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 8
eloc 23
dl 0
loc 53
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 5
A isXattrSupported() 0 3 2
A getStats() 0 10 1
1
<?php namespace Comodojo\SimpleCache\Providers;
2
3
use \Comodojo\Cache\Drivers\FilesystemXattr as FilesystemXattrDriver;
4
use \Comodojo\Cache\Drivers\FilesystemGhost as FilesystemGhostDriver;
5
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats;
6
use \Psr\Log\LoggerInterface;
7
use \Comodojo\Exception\SimpleCacheException;
8
use \Comodojo\Exception\InvalidSimpleCacheArgumentException;
9
use \Exception;
10
11
/**
12
 * @package     Comodojo Cache
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @license     MIT
15
 *
16
 * LICENSE:
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
class Filesystem extends AbstractEnhancedProvider {
28
29
    protected $default_properties = [
30
        "cache_folder" => null
31
    ];
32
33 19
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
34
35
        try {
36
37 19
            parent::__construct($properties, $logger);
38
39 19
            $cache_folder = $this->getProperties()->cache_folder;
40
41 19
            if ( empty($cache_folder) ) {
42
                throw new InvalidSimpleCacheArgumentException("Invalid or unspecified cache folder");
43
            }
44
45 19
            if ( $cache_folder[strlen($cache_folder) - 1] != "/" ) $cache_folder = "$cache_folder/";
46
47 19
            if ( self::isXattrSupported($cache_folder) ) {
48
                $this->driver = new FilesystemXattrDriver(['cache-folder'=>$cache_folder]);
49
            } else {
50 19
                $this->driver = new FilesystemGhostDriver(['cache-folder'=>$cache_folder]);
51
            }
52
53 19
            $this->test();
54
55
        } catch (Exception $e) {
56
57
            throw new SimpleCacheException($e->getMessage());
58
59
        }
60
61 19
    }
62
63 1
    public function getStats() {
64
65 1
        $info = $this->driver->stats();
66
67 1
        return new EnhancedCacheItemPoolStats(
68 1
            $this->getId(),
69 1
            $this->driver->getName(),
70 1
            $this->getState(),
71 1
            $info['objects'],
72 1
            []
73
        );
74
75
    }
76
77 19
    protected static function isXattrSupported($folder) {
78
79 19
        return function_exists("xattr_supported") && xattr_supported($folder);
80
81
    }
82
83
}
84