Test Setup Failed
Push — master ( 562b48...254002 )
by Gabriel
04:15 queued 14s
created

HasAdaptersTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultDriver() 0 4 1
A createDriver() 0 9 2
A createFileDriver() 0 8 2
1
<?php
2
3
namespace Nip\Cache\CacheManager;
4
5
use InvalidArgumentException;
6
use Nip\Cache\Adapters\FileAdapter\FileAdapter;
7
use Symfony\Component\Cache\Adapter\AbstractAdapter;
8
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
9
10
/**
11
 * Trait HasDriversTrait
12
 * @package Nip\Cache\Traits
13
 */
14
trait HasAdaptersTrait
15
{
16
17
    /**
18
     * @return string
19
     */
20
    public function getDefaultDriver()
21
    {
22
        return 'file';
23
    }
24
25
    /**
26
     * @param $name
27
     * @param $config
28
     * @return AbstractAdapter
29
     */
30 3
    public function createDriver($name, $config = [])
31
    {
32 3
        $driverMethod = 'create' . ucfirst($name) . 'Driver';
33 3
        if (method_exists($this, $driverMethod)) {
34 3
            return $this->{$driverMethod}($config);
35
        } else {
36
            throw new InvalidArgumentException("Driver [{$name}] is not supported.");
37
        }
38
    }
39
40
    /**
41
     * Create an instance of the php file cache driver.
42
     *
43
     * @param array $config
44
     * @return FilesystemAdapter
45
     */
46 3
    protected function createFileDriver(array $config)
47
    {
48 3
        $namespace = '';
49 3
        $defaultLifetime = 0;
50 3
        $directory = isset($config['path']) ? $config['path'] : cache_path();
51
52 3
        return new FilesystemAdapter($namespace, $defaultLifetime, $directory);
53
    }
54
}
55