Test Failed
Push — master ( 21e806...ed25dd )
by Fran
05:25 queued 02:04
created

Singleton::init()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 9
nop 0
dl 0
loc 26
ccs 0
cts 17
cp 0
crap 72
rs 8.4444
c 0
b 0
f 0
1
<?php
2
namespace PSFS\base;
3
4
use PSFS\base\config\Config;
5
use PSFS\base\exception\ConfigException;
6
use PSFS\base\types\helpers\InjectorHelper;
7
use PSFS\base\types\traits\SingletonTrait;
8
9
/**
10
 * Class Singleton
11
 * @package PSFS\base
12
 */
13
class Singleton
14
{
15
    use SingletonTrait;
16
17
    /**
18
     * Singleton constructor.
19
     * @throws \Exception
20
     * @throws exception\GeneratorException
21
     * @throws ConfigException
22
     */
23 12
    public function __construct()
24
    {
25 12
        Logger::log(static::class . ' constructor invoked');
26
        $this->init();
27
    }
28
29
    /**
30
     * @param string $variable
31
     * @param mixed $value
32
     */
33
    public function __set($variable, $value)
34
    {
35
        if ($this->__isset($variable)) {
36
            $this->$variable = $value;
37
        }
38
    }
39
40
    /**
41
     * @param string $name
42
     * @return bool
43
     */
44
    public function __isset($name)
45
    {
46
        return property_exists(get_class($this), $name);
47
    }
48
49
    /**
50
     * @param string $variable
51
     * @return mixed
52
     */
53
    public function __get($variable)
54
    {
55
        return $this->__isset($variable) ? $this->$variable : null;
56
    }
57
58
    /**
59
     * HELPERS
60
     */
61
62
    /**
63
     * @return string
64
     * @throws \ReflectionException
65
     */
66
    public function getShortName()
67
    {
68
        $reflector = new \ReflectionClass(get_class($this));
69
        return $reflector->getShortName();
70
    }
71
72
    /**
73
     * @param string $variable
74
     * @param bool $singleton
75
     * @param string $classNameSpace
0 ignored issues
show
Documentation introduced by
Should the type for parameter $classNameSpace not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
76
     * @return $this
77
     * @throws \Exception
78
     */
79
    public function load($variable, $singleton = true, $classNameSpace = null)
80
    {
81
        $calledClass = static::class;
82
        try {
83
            $instance = InjectorHelper::constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass);
84
            $setter = 'set' . ucfirst($variable);
85
            if (method_exists($calledClass, $setter)) {
86
                $this->$setter($instance);
87
            } else {
88
                $this->$variable = $instance;
89
            }
90
        } catch (\Exception $e) {
91
            Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR);
92
            throw $e;
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * @throws \Exception
99
     * @throws exception\GeneratorException
100
     * @throws ConfigException
101
     */
102
    public function init()
103
    {
104
        if (!$this->isLoaded()) {
105
            $filename = sha1(get_class($this));
106
            $cacheFilename = 'reflections' . DIRECTORY_SEPARATOR . substr($filename, 0, 2) . DIRECTORY_SEPARATOR . substr($filename, 2, 2) . DIRECTORY_SEPARATOR . $filename . ".json";
107
            /** @var \PSFS\base\Cache $cacheService */
108
            $cacheService = Cache::getInstance();
109
            /** @var \PSFS\base\config\Config $configService */
110
            $configService = Config::getInstance();
111
            $cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON;
112
            $properties = $cacheService->getDataFromFile($cacheFilename, $cache);
113
            if (true === $configService->getDebugMode() || !$properties) {
114
                $properties = InjectorHelper::getClassProperties(get_class($this));
115
                $cacheService->storeData($cacheFilename, $properties, $cache);
116
            }
117
            /** @var \ReflectionProperty $property */
118
            if (!empty($properties) && is_array($properties)) {
119
                foreach ($properties as $property => $class) {
120
                    $this->load($property, true, $class);
121
                }
122
            }
123
            $this->setLoaded();
124
        } else {
125
            Logger::log(get_class($this) . ' already loaded', LOG_INFO);
126
        }
127
    }
128
}
129