Test Failed
Push — main ( 82933d...ebe982 )
by Rafael
02:28
created

Dispatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processFolder() 0 19 4
A run() 0 9 2
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
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.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Tools;
20
21
class Dispatcher
22
{
23
    /**
24
     * Run the controller for the indicated module, if it exists.
25
     * Returns true if it can be executed.
26
     *
27
     * @param $module
28
     * @param $controller
29
     *
30
     * @return bool
31
     */
32
    public static function run($module, $controller): bool
33
    {
34
        $controller .= 'Controller';
35
        if (static::processFolder($module, $controller)) {
36
            Debug::message("Dispatcher::process(): Ok");
37
            return true;
38
        }
39
        Debug::message("Dispatcher::fail(): $module:$controller.");
40
        return false;
41
    }
42
43
    /**
44
     * Process modern application controller paths.
45
     *
46
     * @param string $module
47
     * @param string $controller
48
     * @return bool
49
     */
50
    protected static function processFolder(string $module, string $controller): bool
51
    {
52
        $className = 'Modules\\' . $module . '\\Controller\\' . $controller;
53
        $basepath = realpath(constant('BASE_PATH') . '/../Modules/' . $module);
54
        $filename = $basepath . '/Controller/' . $controller . '.php';
55
        Debug::message('Filename: ' . $filename);
56
        Debug::message('Class: ' . $className);
57
        if (!file_exists($filename)) {
58
            return false;
59
        }
60
        $controller = new $className();
61
        if ($controller === null) {
62
            return false;
63
        }
64
        if (method_exists($controller, 'setTemplatesPath')) {
65
            $controller->setTemplatesPath($basepath . '/Templates');
66
        }
67
        $controller->index();
68
        return true;
69
    }
70
}
71