PedigreePedigree::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 10
rs 10
c 1
b 0
f 0
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
/**
13
 *  PedigreePedigree class
14
 *
15
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
16
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @package         Class
18
 * @subpackage      Utils
19
 * @since           1.0
20
 * @author          trabis <[email protected]>
21
 */
22
23
/**
24
 * Class PedigreePedigree
25
 */
26
class PedigreePedigree
27
{
28
    public $dirname;
29
    public $module;
30
    public $handler;
31
    public $config;
32
    public $debug;
33
    public $debugArray = [];
34
35
    /**
36
     * @param $debug
37
     */
38
    protected function __construct($debug)
39
    {
40
        $this->debug   = $debug;
41
        $moduleDirName = \basename(\dirname(__DIR__));
42
        parent::__construct($moduleDirName);
43
    }
44
45
    /**
46
     * @param bool $debug
47
     *
48
     * @return PedigreePedigree
49
     */
50
    public static function getInstance($debug = false)
51
    {
52
        static $instance;
53
        if (null === $instance) {
54
            $instance = new static($debug);
55
        }
56
        //error_log("istance: [" . print_r($istance,true) . "]");
57
        //phpinfo();
58
        //debug_print_backtrace ();
59
        return $instance;
60
    }
61
62
    public function getModule()
63
    {
64
        if (null === $this->module) {
65
            $this->initModule();
66
        }
67
68
        return $this->module;
69
    }
70
71
    /**
72
     * @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...
73
     * @return |null
0 ignored issues
show
Documentation Bug introduced by
The doc comment |null at position 0 could not be parsed: Unknown type name '|' at position 0 in |null.
Loading history...
74
     */
75
    public function getConfig($name = null)
76
    {
77
        if (null === $this->config) {
78
            $this->initConfig();
79
        }
80
        if (!$name) {
0 ignored issues
show
introduced by
$name is of type null, thus it always evaluated to false.
Loading history...
81
            $this->addLog('Getting all config');
82
83
            return $this->config;
84
        }
85
        if (!isset($this->config[$name])) {
86
            $this->addLog("ERROR :: CONFIG '{$name}' does not exist");
87
88
            return null;
89
        }
90
        $this->addLog("Getting config '{$name}' : " . print_r($this->config[$name], true));
91
92
        return $this->config[$name];
93
    }
94
95
    /**
96
     * @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...
97
     * @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...
98
     *
99
     * @return mixed
100
     */
101
    public function setConfig($name = null, $value = null)
102
    {
103
        if (null === $this->config) {
104
            $this->initConfig();
105
        }
106
        $this->config[$name] = $value;
107
        $this->addLog("Setting config '{$name}' : " . $this->config[$name]);
108
109
        return $this->config[$name];
110
    }
111
112
    /**
113
     * @param $name
114
     *
115
     * @return mixed
116
     */
117
    public function getHandler($name)
118
    {
119
        if (!isset($this->handler[$name . 'Handler'])) {
120
            $this->initHandler($name);
121
        }
122
        $this->addLog("Getting handler '{$name}'");
123
124
        return $this->handler[$name . 'Handler'];
125
    }
126
127
    public function initModule()
128
    {
129
        global $xoopsModule;
130
        if (isset($xoopsModule) && is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $this->dirname) {
131
            $this->module = $xoopsModule;
132
        } else {
133
            /** @var \XoopsModuleHandler $moduleHandler */
134
            $moduleHandler = xoops_getHandler('module');
135
            $this->module  = $moduleHandler->getByDirname($this->dirname);
136
        }
137
        $this->addLog('INIT MODULE');
138
    }
139
140
    public function initConfig()
141
    {
142
        $this->addLog('INIT CONFIG');
143
        $hModConfig   = xoops_getHandler('config');
144
        $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

144
        /** @scrutinizer ignore-call */ 
145
        $this->config = $hModConfig->getConfigsByCat(0, $this->getModule()->getVar('mid'));
Loading history...
145
    }
146
147
    /**
148
     * @param $name
149
     */
150
    public function initHandler($name)
151
    {
152
        $this->addLog('INIT ' . $name . ' HANDLER');
153
        $this->handler[$name . 'Handler'] = xoops_getModuleHandler($name, $this->dirname);
154
    }
155
156
    /**
157
     * @param $log
158
     */
159
    public function addLog($log)
160
    {
161
        if ($this->debug) {
162
            if (is_object($GLOBALS['xoopsLogger'])) {
163
                $GLOBALS['xoopsLogger']->addExtra($this->module->name(), $log);
164
            }
165
        }
166
    }
167
}
168