SingletonTrait::__wakeup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cse\base;
6
7
use cse\base\Exceptions\CSESingletonException;
8
9
/**
10
 * Class SingletonTrait
11
 *
12
 * @package cse\based
13
 */
14
trait SingletonTrait
15
{
16
    protected static $instance;
17
18
    /**
19
     * @param string $instanceKey
20
     *
21
     * @return SingletonTrait
22
     *
23
     * @throws CSESingletonException
24
     */
25
    public static function getInstance(string $instanceKey = ''): self
26
    {
27
        $instanceKey = empty($instanceKey) ? __CLASS__ : $instanceKey;
28
29
        // instance exist
30
        if (empty(self::$instance[$instanceKey])) {
31
            $reflection = new \ReflectionClass(__CLASS__);
32
            $arg = func_get_args();
33
            array_shift($arg);
34
            self::$instance[$instanceKey] = $reflection->newInstanceArgs($arg);
35
        }
36
37
        return self::$instance[$instanceKey];
38
    }
39
40
    /**
41
     * @throws CSESingletonException
42
     */
43
    final public function __clone()
44
    {
45
        CSESingletonException::throwException(CSESingletonException::ERROR_SINGLETON_CLONE);
46
    }
47
48
    /**
49
     * @throws CSESingletonException
50
     */
51
    final public function __sleep()
52
    {
53
        CSESingletonException::throwException(CSESingletonException::ERROR_SINGLETON_SLEEP);
54
    }
55
56
    /**
57
     * @throws CSESingletonException
58
     */
59
    final public function __wakeup()
60
    {
61
        CSESingletonException::throwException(CSESingletonException::ERROR_SINGLETON_WAKEUP);
62
    }
63
}