WebDispatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 30
dl 0
loc 71
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dieWithMessage() 0 4 1
B run() 0 52 6
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\Dispatcher;
20
21
use Alxarafe\Lib\Functions;
22
use Alxarafe\Lib\Routes;
23
use Alxarafe\Tools\Debug;
24
use CoreModules\Admin\Controller\Error404Controller;
25
use DebugBar\DebugBarException;
26
27
class WebDispatcher extends Dispatcher
28
{
29
    /**
30
     * Run the controller for the indicated module, if it exists.
31
     * Returns true if it can be executed.
32
     *
33
     * @param string $module
34
     * @param string $controller
35
     * @param string $method
36
     *
37
     * @return bool
38
     * @throws DebugBarException
39
     */
40
    public static function run(string $module, string $controller, string $method): bool
41
    {
42
        Debug::initialize();
43
44
        $routes = Routes::getAllRoutes();
45
        $endpoint = $routes['Controller'][$module][$controller] ?? null;
46
        if ($endpoint === null) {
47
            static::dieWithMessage($module . '::' . $controller . 'does not exists');
48
        }
49
50
        Debug::message("Dispatcher::runWeb executing $module::$controller ($endpoint)");
51
        $route_array = explode('|', $endpoint);
52
        $className = $route_array[0];
53
        $filename = $route_array[1];
54
55
        if (!file_exists($filename)) {
56
            static::dieWithMessage($filename . 'does not exists');
57
        }
58
59
        require_once $filename;
60
61
        $controllerClass = new $className();
62
        if ($controllerClass === null) {
63
            static::dieWithMessage($className . ' not found');
64
        }
65
66
        $templates_path = [
67
            constant('ALX_PATH') . '/src/Modules/' . $module . '/Templates/',
68
            constant('BASE_PATH') . '/../Modules/' . $module . '/Templates/',
69
        ];
70
71
        /**
72
         * If the class exists and is successfully instantiated, the module blade templates folder
73
         * is added, if they exist.
74
         */
75
        if (method_exists($controllerClass, 'setTemplatesPath')) {
76
            Debug::message('Templates: ' . $templates_path[0]);
77
            Debug::message('Templates: ' . $templates_path[1]);
78
            $controllerClass->setTemplatesPath($templates_path);
79
        }
80
81
        if (!method_exists($controllerClass, $method)) {
82
            Debug::message('Method ' . $method . ' not found in controller ' . $className);
83
            $method = 'index';
84
        }
85
86
        /**
87
         * Runs the index method to launch the controller.
88
         */
89
        $controllerClass->{$method}();
90
91
        return true;
92
    }
93
94
    protected static function dieWithMessage($message)
95
    {
96
        Debug::message('WebDispatcher error: ' . $message);
97
        Functions::httpRedirect(Error404Controller::url());
98
    }
99
}
100