Passed
Pull Request — master (#39)
by Sebastian
02:56
created

CacheFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 5
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Cache;
13
14
use Psr\SimpleCache\CacheInterface;
15
use InvalidArgumentException;
16
17
/**
18
 * Storage Factory.
19
 */
20 View Code Duplication
class CacheFactory
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...
21
{
22
    /**
23
     * @var string One of supported drivers
24
     */
25
    private $driver;
26
27
    /**
28
     * @var array Factory supported driver
29
     */
30
    private $supportedDriver = [
31
        'disk'       => DiskCache::class,
32
        'memcached'  => MemcachedCache::class,
33
    ];
34
35
    /**
36
     * @var array Options for the driver
37
     */
38
    private $options;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param string $driver
44
     * @param array  $options
45
     */
46 3
    public function __construct(string $driver, array $options)
47
    {
48 3
        $this->driver = $driver;
49 3
        $this->options = $options;
50 3
    }
51
52
    /**
53
     * Return Cache Resource.
54
     *
55
     * @throws InvalidArgumentException If required driver is not supported
56
     *
57
     * @return CacheInterface
58
     */
59 3
    public function get() : CacheInterface
60
    {
61 3
        $driver = $this->driver;
62 3
        $options = $this->options;
63
64 3
        if (isset($this->supportedDriver[$driver])) {
65 2
            $cache = $this->supportedDriver[$driver];
66
67 2
            return new $cache($options);
68
        }
69
70 1
        throw new InvalidArgumentException("[$driver] not supported.");
71
    }
72
}
73