Passed
Push — master ( d6d25f...deb4f9 )
by Fran
05:23
created

Singleton::getShortName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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 3
    public function __construct()
17
    {
18 3
        Logger::log(get_class($this) . ' constructor invoked');
19 3
        $this->init();
20 3
    }
21
22
    /**
23
     * Magic setter
24
     * @param $variable
25
     * @param $value
26
     */
27 8
    public function __set($variable, $value)
28
    {
29 8
        if (property_exists(get_class($this), $variable)) {
30 8
            $this->$variable = $value;
31
        }
32 8
    }
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
     * @throws \Exception
65
     */
66 3
    public function load($variable, $singleton = true, $classNameSpace = null)
67
    {
68 3
        $calledClass = get_called_class();
69
        try {
70 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...
71 3
            $setter = "set" . ucfirst($variable);
72 3
            if (method_exists($calledClass, $setter)) {
73 2
                $this->$setter($instance);
74
            } else {
75 3
                $this->$variable = $instance;
76
            }
77
        } catch (\Exception $e) {
78
            Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR);
79
            throw $e;
80
        }
81 3
        return $this;
82
    }
83
84
    /**
85
     * Método que inyecta automáticamente las dependencias en la clase
86
     */
87 3
    public function init()
88
    {
89 3
        if (!$this->isLoaded()) {
90 3
            $filename = sha1(get_class($this));
91 3
            $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...
92
            /** @var \PSFS\base\Cache $cacheService */
93 3
            $cacheService = Cache::getInstance();
94
            /** @var \PSFS\base\config\Config $configService */
95 3
            $configService = Config::getInstance();
96 3
            $cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON;
97 3
            $properties = $cacheService->getDataFromFile($cacheFilename, $cache);
98 3
            if (true === $configService->getDebugMode() || !$properties) {
99 3
                $properties = InjectorHelper::getClassProperties(get_class($this));
100 3
                $cacheService->storeData($cacheFilename, $properties, $cache);
101
            }
102
            /** @var \ReflectionProperty $property */
103 3
            if (!empty($properties) && is_array($properties)) foreach ($properties as $property => $class) {
104 3
                $this->load($property, true, $class);
105
            }
106 3
            $this->setLoaded();
107
        } else {
108 1
            Logger::log(get_class($this) . ' already loaded', LOG_INFO);
109
        }
110 3
    }
111
}
112