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

Singleton::getInstance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 4
nop 1
crap 3
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