ReportFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 63
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getReports() 0 37 7
A getReport() 0 15 3
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    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       Eric Juden <[email protected]>
20
 * @author       XOOPS Development Team
21
 */
22
23
if (!\defined('XHELP_CLASS_PATH')) {
24
    exit();
25
}
26
27
/**
28
 * class ReportFactory
29
 */
30
class ReportFactory
31
{
32
    /**
33
     * @param string $name
34
     * @return bool
35
     */
36
    public function getReport(string $name): bool
37
    {
38
        $report = false;
39
        if ('' != $name) {
40
            //            $classname = 'xhelp' . \ucfirst($name) . 'Report';
41
            //            require_once \XHELP_REPORT_PATH . "/$name.php";
42
            $classname = __NAMESPACE__ . '\Reports\\' . \ucfirst($name);
43
            if (!\class_exists($classname)) {
44
                throw new \RuntimeException("Class '$classname' not found");
45
            }
46
47
            $report = new $classname();
48
        }
49
50
        return $report;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $report could return the type object which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public static function getReports(): array
57
    {
58
        $aReports = [];
59
60
        // Step 1 - directory listing of all files in /reports directory
61
        $report_dir = @\dir(\XHELP_REPORT_PATH);
62
        if ($report_dir) {
63
            while (false !== ($file = $report_dir->read())) {
64
                $meta = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $meta is dead and can be removed.
Loading history...
65
                if (\preg_match('|^\.+$|', $file)) {
66
                    continue;
67
                }
68
                if (\preg_match('|\.php$|', $file)) {
69
                    $filename = \basename($file, '.php'); // Get name without file extension
70
71
                    // Check that class exists in file
72
                    //                    $report_data = implode('', file(XHELP_REPORT_PATH . '/' . $file));
73
                    //                    $report_data = file_get_contents(\XHELP_REPORT_PATH . '/' . $file);
74
                    //                    $classname   = 'xhelp' . \ucfirst($filename) . 'Report';
75
                    //                    if (\preg_match("|class $classname(.*)|i", $report_data) > 0) {
76
                    //                        require_once \XHELP_REPORT_PATH . "/$file";
77
                    //                        $aReports[$filename] = new $classname();
78
                    //                    }
79
                    //                    unset($report_data);
80
81
                    if (false !== \strpos($filename, 'Report')) {
82
                        $classname = __NAMESPACE__ . '\Reports\\' . \ucfirst($filename);
83
                        if (!\class_exists($classname)) {
84
                            throw new \RuntimeException("Class '$classname' not found");
85
                        }
86
                        $aReports[$filename] = new $classname();
87
                    }
88
                }
89
            }
90
        }
91
92
        return $aReports;
93
    }
94
}
95