Passed
Push — master ( 08392e...cc821f )
by Fran
09:32
created

Singleton::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 3
    public function __construct()
24
    {
25 3
        Logger::log(get_class($this) . ' constructor invoked');
26 3
        $this->init();
27 3
    }
28
29
    /**
30
     * @param string $variable
31
     * @param mixed $value
32
     */
33 8
    public function __set($variable, $value)
34
    {
35 8
        if ($this->__isset($variable)) {
36 8
            $this->$variable = $value;
37
        }
38 8
    }
39
40
    /**
41
     * @param string $name
42
     * @return bool
43
     */
44 8
    public function __isset($name)
45
    {
46 8
        return property_exists(get_class($this), $name);
47
    }
48
49
    /**
50
     * @param string $variable
51
     * @return mixed
52
     */
53 1
    public function __get($variable)
54
    {
55 1
        return $this->__isset($variable) ? $this->$variable : null;
56
    }
57
58
    /**
59
     * HELPERS
60
     */
61
62
    /**
63
     * @return string
64
     */
65 1
    public function getShortName()
66
    {
67 1
        $reflector = new \ReflectionClass(get_class($this));
68 1
        return $reflector->getShortName();
69
    }
70
71
    /**
72
     * @param string $variable
73
     * @param bool $singleton
74
     * @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...
75
     * @return $this
76
     * @throws \Exception
77
     */
78 3
    public function load($variable, $singleton = true, $classNameSpace = null)
79
    {
80 3
        $calledClass = static::class;
81
        try {
82 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...
83 3
            $setter = 'set' . ucfirst($variable);
84 3
            if (method_exists($calledClass, $setter)) {
85 2
                $this->$setter($instance);
86
            } else {
87 3
                $this->$variable = $instance;
88
            }
89
        } catch (\Exception $e) {
90
            Logger::log($e->getMessage() . ': ' . $e->getFile() . ' [' . $e->getLine() . ']', LOG_ERR);
91
            throw $e;
92
        }
93 3
        return $this;
94
    }
95
96
    /**
97
     * @throws \Exception
98
     * @throws exception\GeneratorException
99
     * @throws ConfigException
100
     */
101 3
    public function init()
102
    {
103 3
        if (!$this->isLoaded()) {
104 3
            $filename = sha1(get_class($this));
105 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...
106
            /** @var \PSFS\base\Cache $cacheService */
107 3
            $cacheService = Cache::getInstance();
108
            /** @var \PSFS\base\config\Config $configService */
109 3
            $configService = Config::getInstance();
110 3
            $cache = Cache::canUseMemcache() ? Cache::MEMCACHE : Cache::JSON;
111 3
            $properties = $cacheService->getDataFromFile($cacheFilename, $cache);
112 3
            if (true === $configService->getDebugMode() || !$properties) {
113 3
                $properties = InjectorHelper::getClassProperties(get_class($this));
114 3
                $cacheService->storeData($cacheFilename, $properties, $cache);
115
            }
116
            /** @var \ReflectionProperty $property */
117 3
            if (!empty($properties) && is_array($properties)) {
118 3
                foreach ($properties as $property => $class) {
119 3
                    $this->load($property, true, $class);
120
                }
121
            }
122 3
            $this->setLoaded();
123
        } else {
124 1
            Logger::log(get_class($this) . ' already loaded', LOG_INFO);
125
        }
126 3
    }
127
}
128