Helper::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 3
b 1
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace XoopsModules\Pedigree;
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 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @package
19
 * @since
20
 * @author       XOOPS Development Team
21
 */
22
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
23
24
/**
25
 * Class Helper
26
 */
27
class Helper extends \Xmf\Module\Helper
28
{
29
    public $debug;
30
31
    /** @var self|null */
32
    protected static $helperInstance;
33
34
    /**
35
     * @param bool $debug
36
     */
37
    public function __construct($debug = false)
38
    {
39
        if (null === $this->dirname) {
40
            $dirname       = \basename(\dirname(__DIR__));
41
            $this->dirname = $dirname;
42
        }
43
        parent::__construct($this->dirname);
44
    }
45
46
    /**
47
     * @param bool $debug
48
     *
49
     * @return \XoopsModules\Pedigree\Helper
50
     */
51
    public static function getInstance($debug = false)
52
    {
53
        if (null === static::$helperInstance) {
54
            static::$helperInstance = new static($debug);
55
        }
56
57
        return static::$helperInstance;
58
    }
59
60
    /**
61
     * Replace the stored helper instance
62
     *
63
     * Primarily intended for unit tests where we want to inject
64
     * bespoke handler implementations without touching the
65
     * singleton bootstrap logic used in production.
66
     *
67
     * @param self|null $helper
68
     */
69
    public static function setInstance(?self $helper = null): void
70
    {
71
        static::$helperInstance = $helper;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getDirname()
78
    {
79
        return $this->dirname;
80
    }
81
82
    /**
83
     * Get an Object Handler
84
     *
85
     * @param string $name name of handler to load
86
     *
87
     * @return bool|\XoopsObjectHandler|\XoopsPersistableObjectHandler
88
     */
89
    public function getHandler($name)
90
    {
91
        $ret   = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
92
        $class = __NAMESPACE__ . '\\' . \ucfirst($name) . 'Handler';
93
        if (!\class_exists($class)) {
94
            throw new \RuntimeException("Class '$class' not found");
95
        }
96
        /** @var \XoopsMySQLDatabase $db */
97
        $db     = \XoopsDatabaseFactory::getDatabaseConnection();
98
        $helper = self::getInstance();
99
        $ret    = new $class($db, $helper);
100
        $this->addLog("Getting handler '$name'");
101
102
        return $ret;
103
    }
104
}
105