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

HasAdaptersTrait::createFileDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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