Passed
Push — master ( 227247...0d14a6 )
by Fran
03:24
created

Singleton::load()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 6
nop 3
dl 0
loc 16
ccs 8
cts 10
cp 0.8
crap 3.072
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace PSFS\base;
3
4
use PSFS\base\config\Config;
5
use PSFS\base\types\helpers\InjectorHelper;
6
use PSFS\base\types\traits\SingletonTrait;
7
8
/**
9
 * Class Singleton
10
 * @package PSFS\base
11
 */
12
class Singleton
13
{
14
    use SingletonTrait;
15
    /**
16
     * @var bool Flag that indicated if the class is already loaded
17
     */
18
    protected $loaded = false;
19
20 2
    public function __construct()
21
    {
22 2
        Logger::log(get_class($this) . ' constructor invoked');
23 2
        $this->init();
24 2
    }
25
26
    /**
27
     * Magic setter
28
     * @param $variable
29
     * @param $value
30
     */
31 7
    public function __set($variable, $value)
32
    {
33 7
        if (property_exists(get_class($this), $variable)) {
34 7
            $this->$variable = $value;
35
        }
36 7
    }
37
38
    /**
39
     * Magic getter
40
     * @param string $variable
41
     * @return $mixed
42
     */
43 1
    public function __get($variable)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45 1
        return property_exists(get_class($this), $variable) ? $this->$variable : null;
46
    }
47
48
    /**
49
     * Método que devuelve si una clase está isntanciada correctamente
50
     * @return bool
51
     */
52 2
    public function isLoaded()
53
    {
54 2
        return $this->loaded;
55
    }
56
57
    /**
58
     * Método que configura como cargada una clase
59
     * @param bool $loaded
60
     */
61 2
    public function setLoaded($loaded = true)
62
    {
63 2
        $this->loaded = $loaded;
64 2
    }
65
66
    /**
67
     * HELPERS
68
     */
69
70
    /**
71
     * Método que extrae el nombre de la clase
72
     * @return string
73
     */
74 1
    public function getShortName()
75
    {
76 1
        $reflector = new \ReflectionClass(get_class($this));
77 1
        return $reflector->getShortName();
78
    }
79
80
    /**
81
     * Dependency injector service invoker
82
     * @param string $variable
83
     * @param bool $singleton
84
     * @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...
85
     * @return $this
86
     */
87 2
    public function load($variable, $singleton = true, $classNameSpace = null)
88
    {
89 2
        $calledClass = get_called_class();
90
        try {
91 2
            $instance = InjectorHelper::constructInyectableInstance($variable, $singleton, $classNameSpace, $calledClass);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
92 2
            $setter = "set" . ucfirst($variable);
93 2
            if (method_exists($calledClass, $setter)) {
94 1
                $this->$setter($instance);
95
            } else {
96 2
                $this->$variable = $instance;
97
            }
98
        } catch (\Exception $e) {
99
            Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR);
100
        }
101 2
        return $this;
102
    }
103
104
    /**
105
     * Método que inyecta automáticamente las dependencias en la clase
106
     */
107 2
    public function init()
108
    {
109 2
        if (!$this->isLoaded()) {
110 2
            $filename = sha1(get_class($this));
111 2
            $cacheFilename = "reflections" . DIRECTORY_SEPARATOR . substr($filename, 0, 2) . DIRECTORY_SEPARATOR . substr($filename, 2, 2) . DIRECTORY_SEPARATOR . $filename . ".json";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 183 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
112
            /** @var \PSFS\base\Cache $cacheService */
113 2
            $cacheService = Cache::getInstance();
114
            /** @var \PSFS\base\config\Config $configService */
115 2
            $configService = Config::getInstance();
116 2
            $cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON;
117 2
            $properties = $cacheService->getDataFromFile($cacheFilename, $cache);
118 2
            if (true === $configService->getDebugMode() || !$properties) {
119 2
                $properties = InjectorHelper::getClassProperties(get_class($this));
120 2
                $cacheService->storeData($cacheFilename, $properties, $cache);
121
            }
122
            /** @var \ReflectionProperty $property */
123 2
            if (!empty($properties) && is_array($properties)) foreach ($properties as $property => $class) {
124 2
                $this->load($property, true, $class);
125
            }
126 2
            $this->setLoaded();
127
        } else {
128 1
            Logger::log(get_class($this) . ' already loaded', LOG_INFO);
129
        }
130 2
    }
131
}
132