Completed
Pull Request — master (#573)
by Richard
08:56 queued 27s
created

AbstractHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
dl 0
loc 105
ccs 20
cts 32
cp 0.625
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDebug() 0 3 1
A addLog() 0 8 4
B __construct() 0 26 6
A serializeForHelperLog() 0 9 4
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
/**
15
 * Xmf\Module\Helper\AbstractHelper defines the basis for various
16
 * helpers that simplify routine module tasks.
17
 *
18
 * @category  Xmf\Module\Helper\AbstractHelper
19
 * @package   Xmf
20
 * @author    trabis <[email protected]>
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2011-2018 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @link      http://xoops.org
25
 */
26
abstract class AbstractHelper
27
{
28
    /**
29
     * @var XoopsModule
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Helper\XoopsModule was not found. Did you mean XoopsModule? If so, make sure to prefix the type with \.
Loading history...
30
     */
31
    protected $module;
32
33
    /**
34
     * @var bool true if debug is enabled
35
     */
36
    protected $debug;
37
38
    /**
39
     * Instantiate a XoopsModule object for the helper to use.
40
     * The module is determined as follows:
41
     * - if null is passed, use the current module
42
     * - if a string is passed, use as dirname to load
43
     *
44
     * @param string|null $dirname dirname
45
     */
46 3
    public function __construct($dirname = null)
47
    {
48 3
        $this->module = null;
49
50 3
        if (class_exists('Xoops', false)) {
51 3
            $xoops = \Xoops::getInstance();
52
        }
53 3
        if (empty($dirname)) {
54
            // nothing specified, use current module
55 2
            if (isset($xoops)) {
56 2
                $this->module = $xoops->module;
57
            } else {
58 2
                $this->module = $GLOBALS['xoopsModule'];
59
            }
60
        } else {
61
            // assume dirname specified, try to get a module object
62 1
            if (isset($xoops)) {
63 1
                $moduleHandler = $xoops->getHandlerModule();
64
            } else {
65
                /* @var $moduleHandler \XoopsModuleHandler */
66
                $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getHandler() has been deprecated. ( Ignorable by Annotation )

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

66
                $moduleHandler = /** @scrutinizer ignore-deprecated */ xoops_getHandler('module');
Loading history...
67
            }
68 1
            $this->module = $moduleHandler->getByDirname($dirname);
69
        }
70 3
        if (is_object($this->module)) {
71 1
            $this->init();
72
        }
73 3
    }
74
75
    /**
76
     * init() is called once/if __construct has a module object.
77
     * $this->module will have a module object that any further
78
     * initialization can use.
79
     *
80
     * @return void
81
     */
82
    abstract public function init();
83
84
    /**
85
     * Set debug option on or off
86
     *
87
     * @param bool $bool true to turn on debug logging, false for off
88
     *
89
     * @return void
90
     */
91 1
    public function setDebug($bool = true)
92
    {
93 1
        $this->debug = (bool) $bool;
94 1
    }
95
96
    /**
97
     * Add a message to the module log
98
     *
99
     * @param mixed $log log item, can be message or variable
100
     *
101
     * @return void
102
     */
103 1
    public function addLog($log)
104
    {
105 1
        if ($this->debug) {
106
            $message = $this->serializeForHelperLog($log);
107
            if (class_exists('Xoops', false)) {
108
                \Xoops::getInstance()->logger()->debug($message, array('channel'=>'Extra'));
109
            } elseif (is_object($GLOBALS['xoopsLogger'])) {
110
                $GLOBALS['xoopsLogger']->addExtra(get_called_class(), $message);
111
            }
112
        }
113 1
    }
114
115
    /**
116
     * Serialize an arbitrary value to string. Intended for data being addLog()ed
117
     *
118
     * @param mixed $value
119
     *
120
     * @return string
121
     */
122
    protected function serializeForHelperLog($value)
123
    {
124
        if (is_resource($value) || !is_null(@get_resource_type($value))) {
0 ignored issues
show
introduced by
The condition is_resource($value) || !..._resource_type($value)) can never be false.
Loading history...
125
            $value = '(resource)';
126
        }
127
        if (!is_string($value)) {
0 ignored issues
show
introduced by
The condition ! is_string($value) can never be true.
Loading history...
128
            $value = json_encode($value);
129
        }
130
        return (string) $value;
131
    }
132
}
133