TDMCreateHelper::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * tdmcreate module.
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
18
 *
19
 * @since           2.5.0
20
 *
21
 * @author          trabis <[email protected]>
22
 *
23
 */
24
25
/**
26
 * Class TDMCreateHelper.
27
 */
28
class TDMCreateHelper
29
{
30
    /**
31
     * @var string
32
     */
33
    private $dirname;
34
    /**
35
     * @var string
36
     */
37
    private $module;
38
    /**
39
     * @var string
40
     */
41
    private $handler;
42
    /**
43
     * @var string
44
     */
45
    private $config;
46
    /**
47
     * @var string
48
     */
49
    private $debug;
50
    /*
51
    *  @protected function constructor class
52
    *  @param mixed $debug
53
    */
54
55
    /**
56
     * @param $debug
57
     */
58
    public function __construct($debug)
59
    {
60
        $this->debug   = $debug;
61
        $this->dirname = basename(dirname(__DIR__));
62
    }
63
64
    /*
65
    *  @static function getInstance
66
    *  @param mixed $debug
67
    */
68
69
    /**
70
     * @param bool $debug
71
     *
72
     * @return bool|\TDMCreateHelper
73
     */
74
    public static function getInstance($debug = false)
75
    {
76
        static $instance = false;
77
        if (!$instance) {
78
            $instance = new self($debug);
79
        }
80
81
        return $instance;
82
    }
83
84
    /*
85
    *  @static function getModule
86
    *  @param null
87
    */
88
89
    /**
90
     * @return string
91
     */
92
    public function &getModule()
93
    {
94
        if (null === $this->module) {
95
            $this->initModule();
96
        }
97
98
        return $this->module;
99
    }
100
101
    /*
102
    *  @static function getConfig
103
    *  @param string $name
104
    */
105
106
    /**
107
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
108
     *
109
     * @return null|string
110
     */
111
    public function getConfig($name = null)
112
    {
113
        if (null === $this->config) {
114
            $this->initConfig();
115
        }
116
        if (!$name) {
0 ignored issues
show
introduced by
$name is of type null, thus it always evaluated to false.
Loading history...
117
            $this->addLog('Getting all config');
118
119
            return $this->config;
120
        }
121
        if (!isset($this->config[$name])) {
122
            $this->addLog("ERROR :: CONFIG '{$name}' does not exist");
123
124
            return false;
125
        }
126
        $this->addLog("Getting config '{$name}' : " . $this->config[$name]);
127
128
        return $this->config[$name];
129
    }
130
131
    /*
132
    *  @static function setConfig
133
    *  @param string $name
134
    *  @param mixed $value
135
    */
136
137
    /**
138
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
139
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
140
     *
141
     * @return mixed
142
     */
143
    public function setConfig($name = null, $value = null)
144
    {
145
        if (null === $this->config) {
146
            $this->initConfig();
147
        }
148
        $this->config[$name] = $value;
149
        $this->addLog("Setting config '{$name}' : " . $this->config[$name]);
150
151
        return $this->config[$name];
152
    }
153
154
    /*
155
    *  @static function getHandler
156
    *  @param string $name
157
    */
158
159
    /**
160
     * @param $name
161
     *
162
     * @return mixed
163
     */
164
    public function &getHandler($name)
165
    {
166
        if (!isset($this->handler[$name . 'Handler'])) {
167
            $this->initHandler($name);
168
        }
169
        $this->addLog("Getting handler '{$name}'");
170
171
        return $this->handler[$name . 'Handler'];
172
    }
173
174
    /*
175
    *  @static function initModule
176
    *  @param null
177
    */
178
    public function initModule()
179
    {
180
        global $xoopsModule;
181
        if (isset($xoopsModule) && is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $this->dirname) {
182
            $this->module = $xoopsModule;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xoopsModule of type object is incompatible with the declared type string of property $module.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183
        } else {
184
            $hModule      = xoops_getHandler('module');
185
            $this->module = $hModule->getByDirname($this->dirname);
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
            /** @scrutinizer ignore-call */ 
186
            $this->module = $hModule->getByDirname($this->dirname);
Loading history...
186
        }
187
        $this->addLog('INIT MODULE');
188
    }
189
190
    /*
191
    *  @static function initConfig
192
    *  @param null
193
    */
194
    public function initConfig()
195
    {
196
        $this->addLog('INIT CONFIG');
197
        $hModConfig   = xoops_getHandler('config');
198
        $this->config = $hModConfig->getConfigsByCat(0, $this->getModule()->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
        /** @scrutinizer ignore-call */ 
199
        $this->config = $hModConfig->getConfigsByCat(0, $this->getModule()->getVar('mid'));
Loading history...
199
    }
200
201
    /*
202
    *  @static function initHandler
203
    *  @param string $name
204
    */
205
206
    /**
207
     * @param $name
208
     */
209
    public function initHandler($name)
210
    {
211
        $this->addLog('INIT ' . $name . ' HANDLER');
212
        $this->handler[$name . 'Handler'] = xoops_getModuleHandler($name, $this->dirname);
213
    }
214
215
    /*
216
    *  @static function addLog
217
    *  @param string $log
218
    */
219
220
    /**
221
     * @param $log
222
     */
223
    public function addLog($log)
224
    {
225
        if ($this->debug && is_object($GLOBALS['xoopsLogger'])) {
226
            $GLOBALS['xoopsLogger']->addExtra($this->module->name(), $log);
227
        }
228
    }
229
}
230