Test Failed
Push — master ( 398493...d4ef72 )
by Michael
11:04
created

GenericHelper::loadLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xmf\Module\Helper;
13
14
use Xmf\Language;
15
16
/**
17
 * GenericHelper implements a Xoops 2.6 Xoops\Module\Helper\HelperAbstract.
18
 * We use it pre 2.6 systems so we can encapsulate many of the changes
19
 * needed to make modules more compatible with 2.6 in these methods.
20
 * The most common deprecated warnings can be avoided by using module
21
 * helper methods.
22
 *
23
 * @category  Xmf\Module\Helper\GenericHelper
24
 * @package   Xmf
25
 * @author    trabis <[email protected]>
26
 * @author    Richard Griffith <[email protected]>
27
 * @copyright 2016-2018 XOOPS Project (https://xoops.org)
28
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
29
 * @link      https://xoops.org
30
 */
31
abstract class GenericHelper extends AbstractHelper
32
{
33
    /**
34
     * @var string module directory name
35
     */
36
    protected $dirname;
37
38
    /**
39
     * @var \XoopsModule
40
     */
41
    protected $object;
42
43
    /**
44
     * @var array of XoopsObjectHandler|XoopsPersistableObjectHandler
45
     */
46
    protected $handlers;
47
48
    /**
49
     * @var array config items
50
     */
51
    protected $configs;
52
53
    /**
54
     * Initialize parent::__construct calls this after verifying module object.
55
     *
56
     * @return void
57
     */
58
    public function init()
59
    {
60
        $this->object = $this->module;
61
        $this->dirname = $this->object->getVar('dirname');
62
    }
63
64
    /**
65
     * get the module object
66
     *
67
     * @return \XoopsModule
68
     */
69
    public function getModule()
70
    {
71
        if ($this->object == null) {
72
            $this->initObject();
73
        }
74
        if (!is_object($this->object)) {
75
            $this->addLog("ERROR :: Module '{$this->dirname}' does not exist");
76
        }
77
78
        return $this->object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->object could also return false which is incompatible with the documented return type XoopsModule. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
79
    }
80
81
    /**
82
     * get a module config item
83
     *
84
     * @param string $name    name of config item, or blank for all items
85
     * @param mixed  $default default value to return if config $name is not set
86
     *
87
     * @return mixed string config item, array of config items,
88
     *                or null if config not found
89
     */
90
    public function getConfig($name = null, $default = null)
91
    {
92
        if ($this->configs == null) {
93
            $this->initConfig();
94
        }
95
        if (empty($name)) {
96
            $this->addLog("Getting all config");
97
98
            return $this->configs;
99
        }
100
101
        if (!isset($this->configs[$name])) {
102
            $this->addLog("ERROR :: Config '{$name}' does not exist");
103
            return $default;
104
        }
105
106
        $this->addLog("Getting config '{$name}' : " . $this->serializeForHelperLog($this->configs[$name]));
107
108
        return $this->configs[$name];
109
    }
110
111
    /**
112
     * Get an Object Handler
113
     *
114
     * @param string $name name of handler to load
115
     *
116
     * @return bool|XoopsObjectHandler|XoopsPersistableObjectHandler
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Helper\XoopsObjectHandler was not found. Did you mean XoopsObjectHandler? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
The type Xmf\Module\Helper\XoopsPersistableObjectHandler was not found. Did you mean XoopsPersistableObjectHandler? If so, make sure to prefix the type with \.
Loading history...
117
     */
118
    public function getHandler($name)
119
    {
120
        $ret = false;
121
        $name = strtolower($name);
122
        if (!isset($this->handlers[$name])) {
123
            $this->initHandler($name);
124
        }
125
126
        if (!isset($this->handlers[$name])) {
127
            $this->addLog("ERROR :: Handler '{$name}' does not exist");
128
        } else {
129
            $this->addLog("Getting handler '{$name}'");
130
            $ret = $this->handlers[$name];
131
        }
132
133
        return $ret;
134
    }
135
136
    /**
137
     * get a module object
138
     *
139
     * @return void
140
     */
141
    protected function initObject()
142
    {
143
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
144
        if (isset($xoopsModule) && is_object($xoopsModule)
145
            && $xoopsModule->getVar('dirname') == $this->dirname
146
        ) {
147
            $this->object = $xoopsModule;
148
        } else {
149
            /* @var $module_handler \XoopsModuleHandler */
150
            $module_handler = xoops_getHandler('module');
151
            $this->object = $module_handler->getByDirname($this->dirname);
0 ignored issues
show
Documentation Bug introduced by
It seems like $module_handler->getByDirname($this->dirname) can also be of type false. However, the property $object is declared as type XoopsModule. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
152
        }
153
        $this->addLog('INIT MODULE OBJECT');
154
    }
155
156
    /**
157
     * get module configs
158
     *
159
     * @return void
160
     */
161
    protected function initConfig()
162
    {
163
        $this->addLog('INIT CONFIG');
164
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
165
        if (isset($xoopsModule) && is_object($xoopsModule)
166
            && $xoopsModule->getVar('dirname') == $this->dirname
167
        ) {
168
            global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
169
            $this->configs = $xoopsModuleConfig;
170
        } else {
171
            /* @var $config_handler \XoopsConfigHandler */
172
            $config_handler = xoops_getHandler('config');
173
            $this->configs = $config_handler->getConfigsByCat(0, $this->getModule()->getVar('mid'));
174
        }
175
    }
176
177
    /**
178
     * get a handler instance and store in $this->_handlers
179
     *
180
     * @param string $name name of handler to load
181
     *
182
     * @return void
183
     */
184
    protected function initHandler($name)
185
    {
186
        $this->addLog('INIT ' . $name . ' HANDLER');
187
188
        if (!isset($this->handlers[$name])) {
189
            $hnd_file = XOOPS_ROOT_PATH . "/modules/{$this->dirname}/class/{$name}.php";
190
            if (file_exists($hnd_file)) {
191
                include_once $hnd_file;
192
            }
193
            $class = ucfirst(strtolower($this->dirname))
194
                . ucfirst(strtolower($name)) . 'Handler';
195
            if (class_exists($class)) {
196
                $db = \XoopsDatabaseFactory::getDatabaseConnection();
197
                $this->handlers[$name] = new $class($db);
198
                $this->addLog("Loading class '{$class}'");
199
            } else {
200
                $this->addLog("ERROR :: Class '{$class}' could not be loaded");
201
            }
202
        }
203
    }
204
205
    /**
206
     * load a language file for this module
207
     *
208
     * @param string $name basename of language file (i.e. 'admin')
209
     *
210
     * @return bool
211
     */
212
    public function loadLanguage($name)
213
    {
214
        if ($ret = Language::load($name, $this->dirname)) {
215
            $this->addLog("Loading language '{$name}'");
216
        } else {
217
            $this->addLog("ERROR :: Language '{$name}' could not be loaded");
218
        }
219
220
        return $ret;
221
    }
222
223
    /**
224
     * Is this the currently active module?
225
     *
226
     * @return bool
227
     */
228
    public function isCurrentModule()
229
    {
230
        if ($GLOBALS['xoopsModule']->getVar('dirname') == $this->dirname) {
231
            return true;
232
        }
233
234
        return false;
235
    }
236
237
    /**
238
     * Does user have admin rights to this module?
239
     *
240
     * @return bool true is user has admin right, else false
241
     */
242
    public function isUserAdmin()
243
    {
244
        return (isset($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser'] instanceof \XoopsUser)
245
            ? $GLOBALS['xoopsUser']->isAdmin($this->getModule()->getVar('mid')) : false;
246
    }
247
248
    /**
249
     * Return absolute URL for a module relative URL
250
     *
251
     * @param string $url module relative URL
252
     *
253
     * @return string
254
     */
255
    public function url($url = '')
256
    {
257
        return XOOPS_URL . '/modules/' . $this->dirname . '/' . $url;
258
    }
259
260
    /**
261
     * Return absolute filesystem path for a module relative path
262
     *
263
     * @param string $path module relative file system path
264
     *
265
     * @return string
266
     */
267
    public function path($path = '')
268
    {
269
        return XOOPS_ROOT_PATH . '/modules/' . $this->dirname . '/' . $path;
270
    }
271
272
    /**
273
     * Redirect the user to a page within this module
274
     *
275
     * @param string $url     module relative url (i.e. index.php)
276
     * @param int    $time    time in seconds to show redirect message
277
     * @param string $message redirect message
278
     *
279
     * @return void
280
     */
281
    public function redirect($url, $time = 3, $message = '')
282
    {
283
        redirect_header($this->url($url), $time, $message);
284
    }
285
}
286