Passed
Push — master ( 66afcb...9df01b )
by Fran
05:56
created

Singleton::setLoaded()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 2
    public function __construct()
17
    {
18 2
        Logger::log(get_class($this) . ' constructor invoked');
19 2
        $this->init();
20 2
    }
21
22
    /**
23
     * Magic setter
24
     * @param $variable
25
     * @param $value
26
     */
27 7
    public function __set($variable, $value)
28
    {
29 7
        if (property_exists(get_class($this), $variable)) {
30 7
            $this->$variable = $value;
31
        }
32 7
    }
33
34
    /**
35
     * Magic getter
36
     * @param string $variable
37
     * @return $mixed
38
     */
39 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...
40
    {
41 1
        return property_exists(get_class($this), $variable) ? $this->$variable : null;
42
    }
43
44
    /**
45
     * HELPERS
46
     */
47
48
    /**
49
     * Método que extrae el nombre de la clase
50
     * @return string
51
     */
52 1
    public function getShortName()
53
    {
54 1
        $reflector = new \ReflectionClass(get_class($this));
55 1
        return $reflector->getShortName();
56
    }
57
58
    /**
59
     * Dependency injector service invoker
60
     * @param string $variable
61
     * @param bool $singleton
62
     * @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...
63
     * @return $this
64
     */
65 2
    public function load($variable, $singleton = true, $classNameSpace = null)
66
    {
67 2
        $calledClass = get_called_class();
68
        try {
69 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...
70 2
            $setter = "set" . ucfirst($variable);
71 2
            if (method_exists($calledClass, $setter)) {
72 1
                $this->$setter($instance);
73
            } else {
74 2
                $this->$variable = $instance;
75
            }
76
        } catch (\Exception $e) {
77
            Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR);
78
        }
79 2
        return $this;
80
    }
81
82
    /**
83
     * Método que inyecta automáticamente las dependencias en la clase
84
     */
85 2
    public function init()
86
    {
87 2
        if (!$this->isLoaded()) {
88 2
            $filename = sha1(get_class($this));
89 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...
90
            /** @var \PSFS\base\Cache $cacheService */
91 2
            $cacheService = Cache::getInstance();
92
            /** @var \PSFS\base\config\Config $configService */
93 2
            $configService = Config::getInstance();
94 2
            $cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON;
95 2
            $properties = $cacheService->getDataFromFile($cacheFilename, $cache);
96 2
            if (true === $configService->getDebugMode() || !$properties) {
97 2
                $properties = InjectorHelper::getClassProperties(get_class($this));
98 2
                $cacheService->storeData($cacheFilename, $properties, $cache);
99
            }
100
            /** @var \ReflectionProperty $property */
101 2
            if (!empty($properties) && is_array($properties)) foreach ($properties as $property => $class) {
102 2
                $this->load($property, true, $class);
103
            }
104 2
            $this->setLoaded();
105
        } else {
106 1
            Logger::log(get_class($this) . ' already loaded', LOG_INFO);
107
        }
108 2
    }
109
}
110