Completed
Push — master ( 79d061...7993bb )
by Marco
06:18
created

Filesystem::__construct()   B

Complexity

Conditions 5
Paths 18

Size

Total Lines 29
Code Lines 14

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 5.583

Importance

Changes 0
Metric Value
dl 29
loc 29
ccs 10
cts 14
cp 0.7143
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 18
nop 2
crap 5.583
1
<?php namespace Comodojo\Cache\Providers;
2
3
use \Comodojo\Cache\Drivers\FilesystemXattr as FilesystemXattrDriver;
4
use \Comodojo\Cache\Drivers\FilesystemGhost as FilesystemGhostDriver;
5
use \Comodojo\Cache\Item;
6
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats;
7
use \Psr\Log\LoggerInterface;
8
use \Comodojo\Exception\CacheException;
9
use \Exception;
10
11
/**
12
 * Apcu provider
13
 *
14
 * @package     Comodojo Spare Parts
15
 * @author      Marco Giovinazzi <[email protected]>
16
 * @license     MIT
17
 *
18
 * LICENSE:
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
29 View Code Duplication
class Filesystem extends AbstractEnhancedProvider {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
31
    protected $default_properties = [
32
        "cache_folder" => null
33
    ];
34
35 28
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
36
37
        try {
38
39 28
            parent::__construct($properties, $logger);
40
41 28
            $cache_folder = $this->getProperties()->cache_folder;
42
43 28
            if ( empty($cache_folder) ) {
44
                throw new InvalidCacheArgumentException("Invalid or unspecified cache folder");
45
            }
46
47 28
            if ( $cache_folder[strlen($cache_folder) - 1] != "/" ) $cache_folder = "$cache_folder/";
48
49 28
            if ( self::isXattrSupported($cache_folder) ) {
50
                $this->driver = new FilesystemXattrDriver(['cache-folder'=>$cache_folder]);
51
            } else {
52 28
                $this->driver = new FilesystemGhostDriver(['cache-folder'=>$cache_folder]);
53
            }
54
55 28
            $this->test();
56
57 28
        } catch (Exception $e) {
58
59
            throw new CacheException($e->getMessage());
60
61
        }
62
63 28
    }
64
65 1
    public function getStats() {
66
67 1
        $info = $this->driver->stats();
68
69 1
        return new EnhancedCacheItemPoolStats(
70 1
            $this->getId(),
71 1
            $this->driver->getName(),
72 1
            $this->getState(),
73 1
            $info,
74 1
            []
75 1
        );
76
77
    }
78
79 28
    protected static function isXattrSupported($folder) {
80
81 28
        return function_exists("xattr_supported") && xattr_supported($folder);
82
83
    }
84
85
}
86