Completed
Pull Request — master (#24)
by Adam
02:31
created

Singleton::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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 1
    private function __construct()
27
    {
28 1
    }
29
30
    /**
31
     * Get instance from $instances array.
32
     *
33
     * If the called class is present in the static $instances array, then return it.
34
     * Otherwise, create a new copy of the class and store it in the $instances
35
     * array.
36
     *
37
     * @param  string|null $calledClass
38
     * @return Object
39
     */
40 2
    final public static function getInstance($calledClass = null)
41
    {
42 2
        $calledClass = $calledClass ?: get_called_class();
43
44 2
        if (!isset(self::$instances[ $calledClass ])) {
45 1
            self::$instances[ $calledClass ] = new $calledClass();
46 1
        }
47
48 2
        return self::$instances[ $calledClass ];
49
    }
50
51
    /**
52
     * Destroy
53
     *
54
     * @return void
55
     */
56 1
    final public static function destroy()
57
    {
58 1
        self::$instances = [ ];
59 1
    }
60
61
    /**
62
     * @param $instance
63
     */
64 1
    final public static function destroyInstance($instance)
65
    {
66 1
        unset(self::$instances[ $instance ]);
67 1
    }
68
}
69