Passed
Pull Request — master (#43)
by Arman
03:54
created

MvcManager::runMvc()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 17
c 10
b 0
f 0
dl 0
loc 32
rs 8.8333
cc 7
nc 19
nop 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.5.0
13
 */
14
15
namespace Quantum\Mvc;
16
17
use Quantum\Exceptions\ControllerException;
18
use Quantum\Libraries\Storage\FileSystem;
19
use Quantum\Middleware\MiddlewareManager;
20
use Quantum\Libraries\Csrf\Csrf;
21
use Quantum\Http\Response;
22
use Quantum\Http\Request;
23
use Quantum\Di\Di;
24
25
/**
26
 * Class MvcManager
27
 * @package Quantum\Mvc
28
 */
29
class MvcManager
30
{
31
32
    /**
33
     * Handles the request
34
     * @param \Quantum\Http\Request $request
35
     * @param \Quantum\Http\Response $response
36
     * @throws \Quantum\Exceptions\ControllerException
37
     * @throws \Quantum\Exceptions\CsrfException
38
     * @throws \Quantum\Exceptions\DiException
39
     * @throws \Quantum\Exceptions\MiddlewareException
40
     * @throws \Quantum\Exceptions\ModelException
41
     * @throws \ReflectionException
42
     */
43
    public static function handle(Request $request, Response $response)
44
    {
45
        if ($request->getMethod() != 'OPTIONS') {
46
47
            if (current_middlewares()) {
48
                list($request, $response) = (new MiddlewareManager())->applyMiddlewares($request, $response);
49
            }
50
51
            $routeArgs = current_route_args();
52
            $callback = route_callback();
53
54
            if ($callback) {
55
                call_user_func_array($callback, self::getArgs($callback, $routeArgs));
56
            } else {
57
                $controller = self::getController();
58
                $action = self::getAction($controller);
59
60
                if ($controller->csrfVerification ?? true) {
0 ignored issues
show
Bug introduced by
The property csrfVerification does not seem to exist on Quantum\Mvc\QtController.
Loading history...
61
                    Csrf::checkToken($request, session());
62
                }
63
64
                if (method_exists($controller, '__before')) {
65
                    call_user_func_array([$controller, '__before'], self::getArgs([$controller, '__before'], $routeArgs));
66
                }
67
68
                call_user_func_array([$controller, $action], self::getArgs([$controller, $action], $routeArgs));
69
70
                if (method_exists($controller, '__after')) {
71
                    call_user_func_array([$controller, '__after'], self::getArgs([$controller, '__after'], $routeArgs));
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * Get Controller
79
     * @return \Quantum\Mvc\QtController
80
     * @throws \Quantum\Exceptions\ControllerException
81
     * @throws \Quantum\Exceptions\DiException
82
     * @throws \ReflectionException
83
     */
84
    private static function getController(): QtController
85
    {
86
        $fs = Di::get(FileSystem::class);
87
88
        $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php';
89
90
        if (!$fs->exists($controllerPath)) {
91
            throw ControllerException::controllerNotFound(current_controller());
92
        }
93
94
        require_once $controllerPath;
95
96
        $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller();
97
98
        if (!class_exists($controllerClass, false)) {
99
            throw ControllerException::controllerNotDefined(current_controller());
100
        }
101
102
        return new $controllerClass();
103
    }
104
105
    /**
106
     * Get Action
107
     * @param \Quantum\Mvc\QtController $controller
108
     * @return string|null
109
     * @throws \Quantum\Exceptions\ControllerException
110
     */
111
    private static function getAction(QtController $controller): ?string
112
    {
113
        $action = current_action();
114
115
        if ($action && !method_exists($controller, $action)) {
116
            throw ControllerException::actionNotDefined($action);
117
        }
118
119
        return $action;
120
    }
121
122
    /**
123
     * Get Args
124
     * @param callable $callable
125
     * @param array $routeArgs
126
     * @return array
127
     * @throws \Quantum\Exceptions\DiException
128
     * @throws \ReflectionException
129
     */
130
    private static function getArgs(callable $callable, array $routeArgs): array
131
    {
132
        return Di::autowire($callable, $routeArgs);
133
    }
134
135
}
136