Factory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 62
loc 62
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 3 1
A __clone() 3 3 1
A create() 12 12 3
A getClassForName() 4 4 1
A getInstance() 8 8 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
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