Factory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 12
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Cache;
3
4 View Code Duplication
class Factory
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...
5
{
6
    const DRIVER_RAM = 'Ram';
7
    const DRIVER_FILE = 'File';
8
    const DRIVER_SHM = 'Shm';
9
    private static $instance;
10
11
    /**
12
     * @codeCoverageIgnore
13
     */
14
    private function __construct()
15
    {
16
    }
17
18
    /**
19
     * @codeCoverageIgnore
20
     */
21
    private function __clone()
22
    {
23
    }
24
25
    /**
26
     * @return self
27
     */
28 4
    final public static function getInstance()
29
    {
30 4
        if (!self::$instance) {
31 1
            $class = get_called_class();
32 1
            self::$instance = new $class();
33
        }
34 4
        return self::$instance;
35
    }
36
    
37
    /**
38
     * @param string $driver
39
     * @param array $params
40
     * @return CacheInterface
41
     * @throws Exception
42
     */
43 3
    final public function create($driver = self::DRIVER_RAM, $params = array())
44
    {
45 3
        $class = $this->getClassForName($driver);
46 3
        if (!class_exists($class)) {
47 2
            throw new Exception('Unable to create ' . $class);
48
        }
49 2
        $instance = new $class($params);
50 2
        if (!$instance instanceof CacheInterface) {
51 1
            throw new Exception('Unable to create ' . $class);
52
        }
53 2
        return $instance;
54
    }
55
56
    /**
57
     * Must return full class name for specified driver name
58
     * @param string $driver
59
     * @return string
60
     */
61 3
    protected function getClassForName($driver)
62
    {
63 3
        return __NAMESPACE__ . '\Driver\\' . ucfirst($driver);
64
    }
65
}
66