Helper::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
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    XOOPS Project (https://xoops.org)
17
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author       XOOPS Development Team
19
 */
20
21
/**
22
 * Class Helper
23
 */
24
class Helper extends \Xmf\Module\Helper
25
{
26
    public $debug;
27
28
    /**
29
     * @param bool $debug
30
     */
31
    public function __construct($debug = false)
32
    {
33
        $this->debug   = $debug;
34
        $moduleDirName = \basename(\dirname(__DIR__));
35
        parent::__construct($moduleDirName);
36
    }
37
38
    /**
39
     * @param bool $debug
40
     * @return \XoopsModules\Xhelp\Helper
41
     */
42
    public static function getInstance(bool $debug = false): self
43
    {
44
        static $instance;
45
        if (null === $instance) {
46
            $instance = new static($debug);
47
        }
48
49
        return $instance;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getDirname(): string
56
    {
57
        return $this->dirname;
58
    }
59
60
    /**
61
     * Get an Object Handler
62
     *
63
     * @param string $name name of handler to load
64
     *
65
     * @return \XoopsPersistableObjectHandler|\XoopsObjectHandler|bool
66
     */
67
    public function getHandler($name)
68
    {
69
        $ret = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
70
71
        $class = __NAMESPACE__ . '\\' . \ucfirst($name) . 'Handler';
72
        if (!\class_exists($class)) {
73
            throw new \RuntimeException("Class '$class' not found");
74
        }
75
        /** @var \XoopsMySQLDatabase $db */
76
        $db     = \XoopsDatabaseFactory::getDatabaseConnection();
77
        $helper = self::getInstance();
78
        $ret    = new $class($db, $helper);
79
        $this->addLog("Getting handler '{$name}'");
80
81
        return $ret;
82
    }
83
}
84