Completed
Push — master ( 0f7d17...393ba8 )
by Adam
02:40
created

Singleton   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 50
c 2
b 0
f 0
ccs 12
cts 12
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getInstance() 0 10 3
A destroy() 0 4 1
A destroyInstance() 0 4 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Pattern;
4
5
/**
6
 * Class Multiton
7
 *
8
 * @package   BestServedCold\PhalueObjects\Pattern
9
 * @author    Adam Lewis <[email protected]>
10
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
11
 * @license      http://http://opensource.org/licenses/GPL-3.0 GPL License
12
 * @link      http://bestservedcold.com
13
 * @since      0.0.1-alpha
14
 * @version   0.0.2-alpha
15
 */
16
class Singleton extends NotConstructable
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
17
{
18
    /**
19
     * @var array $instances
20
     */
21
    protected static $instances = [ ];
22
23
    /**
24
     * Prevent class from being constructed.
25
     */
26
    private function __construct() {}
27
28
    /**
29
     * Get instance from $instances array.
30
     *
31
     * If the called class is present in the static $instances array, then return it.
32
     * Otherwise, create a new copy of the class and store it in the $instances
33
     * array.
34
     *
35
     * @return mixed
36
     */
37 3
    final public static function getInstance($calledClass = null)
38
    {
39 3
        $calledClass = $calledClass ?: get_called_class();
40
41 3
        if (!isset(self::$instances[ $calledClass ])) {
42 2
            self::$instances[ $calledClass ] = new $calledClass();
43 2
        }
44
45 3
        return self::$instances[ $calledClass ];
46
    }
47
48
    /**
49
     * Destroy
50
     *
51
     * @return void
52
     */
53 1
    final public static function destroy()
54
    {
55 1
        self::$instances = [ ];
56 1
    }
57
58
    /**
59
     * @param $instance
60
     */
61 1
    final public static function destroyInstance($instance)
62
    {
63 1
        unset(self::$instances[ $instance ]);
64 1
    }
65
}
66