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

CacheFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 53
loc 53
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A get() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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