Completed
Push — master ( 10fc3d...cbc6e7 )
by Richard
14s
created

AbstractHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
ccs 16
cts 22
cp 0.7272
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDebug() 0 3 1
C __construct() 0 27 7
A addLog() 0 4 2
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-2016 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 2
    public function __construct($dirname = null)
47
    {
48 2
        $this->module = null;
49 2
        if (class_exists('Xoops', false)) {
50 2
            $xoops = \Xoops::getInstance();
51
        }
52 2
        if (empty($dirname)) {
53
            // nothing specified, use current module
54
            // check if we are running in 2.6
55 2
            if (isset($xoops)) {
56 2
                if ($xoops->isModule()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $xoops does not seem to be defined for all execution paths leading up to this point.
Loading history...
57 2
                    $this->module = $xoops->module;
58
                }
59
            } else {
60 2
                $this->module = $GLOBALS['xoopsModule'];
61
            }
62
        } else {
63
            // assume dirname specified, try to get a module object
64
            if (isset($xoops)) {
65
                $moduleHandler = $xoops->getHandlerModule();
66
            } else {
67
                $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

67
                $moduleHandler = /** @scrutinizer ignore-deprecated */ xoops_getHandler('module');
Loading history...
68
            }
69
            $this->module = $moduleHandler->getByDirname($dirname);
70
        }
71 2
        if (is_object($this->module)) {
72
            $this->init();
73
        }
74 2
    }
75
76
    /**
77
     * init() is called once/if __construct has a module object.
78
     * $this->module will have a module object that any further
79
     * initialization can use.
80
     *
81
     * @return void
82
     */
83
    abstract public function init();
84
85
    /**
86
     * Set debug option on or off
87
     *
88
     * @param bool $bool true to turn on debug logging, false for off
89
     *
90
     * @return void
91
     */
92 1
    public function setDebug($bool = true)
93
    {
94 1
        $this->debug = (bool) $bool;
95 1
    }
96
97
    /**
98
     * Add a message to the module log
99
     *
100
     * @param string $log log message
101
     *
102
     * @return void
103
     */
104 1
    public function addLog($log)
105
    {
106 1
        if ($this->debug) {
107
            \Xoops::getInstance()->logger()->debug($log, array('channel'=>'Extra'));
108
        }
109 1
    }
110
}
111