Test Failed
Push — master ( f7ffcc...a40e1b )
by Fran
03:18
created

Singleton::constructInyectableInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 4
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 4
1
<?php
2
3
namespace PSFS\base;
4
5
use PSFS\base\config\Config;
6
use PSFS\base\types\helpers\InjectorHelper;
7
use PSFS\base\types\SingletonTrait;
8
9
/**
10
 * Class Singleton
11
 * @package PSFS\base
12
 */
13
class Singleton
14
{
15
    use SingletonTrait;
16
    /**
17
     * @var bool Flag that indicated if the class is already loaded
18
     */
19
    protected $loaded = false;
20 3
21
    public function __construct()
22 3
    {
23 3
        Logger::log(get_class($this) . ' constructor invoked');
24 3
        $this->init();
25
    }
26
27
    /**
28
     * Magic setter
29
     * @param $variable
30
     * @param $value
31 8
     */
32
    public function __set($variable, $value)
33 8
    {
34 8
        if (property_exists(get_class($this), $variable)) {
35 8
            $this->$variable = $value;
36 8
        }
37
    }
38
39
    /**
40
     * Magic getter
41
     * @param string $variable
42
     * @return $mixed
43 1
     */
44
    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...
45 1
    {
46
        return property_exists(get_class($this), $variable) ? $this->$variable : null;
47
    }
48
49
    /**
50
     * Método que devuelve si una clase está isntanciada correctamente
51
     * @return bool
52 3
     */
53
    public function isLoaded()
54 3
    {
55
        return $this->loaded;
56
    }
57
58
    /**
59
     * Método que configura como cargada una clase
60
     * @param bool $loaded
61 3
     */
62
    public function setLoaded($loaded = true)
63 3
    {
64 3
        $this->loaded = $loaded;
65
    }
66
67
    /**
68
     * HELPERS
69
     */
70
71
    /**
72
     * Método que extrae el nombre de la clase
73
     * @return string
74 1
     */
75
    public function getShortName()
76 1
    {
77 1
        $reflector = new \ReflectionClass(get_class($this));
78
        return $reflector->getShortName();
79
    }
80
81
    /**
82
     * Dependency injector service invoker
83
     * @param string $variable
84
     * @param bool $singleton
85
     * @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...
86
     * @return $this
87 3
     */
88
    public function load($variable, $singleton = true, $classNameSpace = null)
89 3
    {
90
        $calledClass = get_called_class();
91 3
        try {
92 3
            $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...
93 3
            $setter = "set" . ucfirst($variable);
94 2
            if (method_exists($calledClass, $setter)) {
95 2
                $this->$setter($instance);
96 3
            } else {
97
                $this->$variable = $instance;
98 3
            }
99 1
        } catch (\Exception $e) {
100
            Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR);
101 3
        }
102
        return $this;
103
    }
104
105
    /**
106
     * Método que inyecta automáticamente las dependencias en la clase
107 3
     */
108
    public function init()
109 3
    {
110 3
        if (!$this->isLoaded()) {
111
            $cacheFilename = "reflections" . DIRECTORY_SEPARATOR . sha1(get_class($this)) . ".json";
112 3
            /** @var \PSFS\base\Cache $cacheService */
113
            $cacheService = Cache::getInstance();
114 3
            /** @var \PSFS\base\config\Config $configService */
115 3
            $configService = Config::getInstance();
116 3
            $properties = $cacheService->getDataFromFile($cacheFilename, Cache::JSON);
117 3
            if (true === $configService->getDebugMode() || null === $properties) {
118 3
                $properties = InjectorHelper::getClassProperties(get_class($this));
119 3
                $cacheService->storeData($cacheFilename, $properties, Cache::JSON);
120
            }
121 3
            /** @var \ReflectionProperty $property */
122 3
            if (!empty($properties) && is_array($properties)) foreach ($properties as $property => $class) {
123 3
                $this->load($property, true, $class);
124 3
            }
125 3
            $this->setLoaded();
126 3
        } else {
127
            Logger::log(get_class($this) . ' already loaded', LOG_INFO);
128 3
        }
129
    }
130
}
131