Completed
Push — master ( d9f04e...3eb62a )
by Arman
21s queued 11s
created

MvcManager::collectParams()   B

Complexity

Conditions 11
Paths 2

Size

Total Lines 43
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 43
rs 7.3166
cc 11
nc 2
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 1.0.0
13
 */
14
15
namespace Quantum\Mvc;
16
17
use Quantum\Exceptions\ExceptionMessages;
18
use Quantum\Middleware\MiddlewareManager;
19
use Quantum\Exceptions\RouteException;
20
use Quantum\Libraries\Csrf\Csrf;
21
use Quantum\Http\Response;
22
use Quantum\Http\Request;
23
use Quantum\Di\Di;
24
25
/**
26
 * MvcManager Class
27
 * 
28
 * @package Quantum
29
 * @category MVC
30
 */
31
class MvcManager
32
{
33
34
    /**
35
     * @var QtController
36
     */
37
    private static $controller;
38
39
    /**
40
     * Run MVC
41
     * @param Request $request
42
     * @param Response $response
43
     * @throws RouteException
44
     * @throws \ReflectionException
45
     */
46
    public static function runMvc(Request $request, Response $response)
47
    {
48
49
        if ($request->getMethod() != 'OPTIONS') {
50
51
            if (current_middlewares()) {
52
                list($request, $response) = (new MiddlewareManager())->applyMiddlewares($request, $response);
53
            }
54
55
            $routeArgs = current_route_args();
56
57
            $callback = route_callback();
58
59
            if ($callback) {
0 ignored issues
show
introduced by
$callback is of type Closure, thus it always evaluated to true.
Loading history...
60
                call_user_func_array($callback, self::getCallbackArgs($routeArgs, $callback, $request, $response));
0 ignored issues
show
Unused Code introduced by
The call to Quantum\Mvc\MvcManager::getCallbackArgs() has too many arguments starting with $request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                call_user_func_array($callback, self::/** @scrutinizer ignore-call */ getCallbackArgs($routeArgs, $callback, $request, $response));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
61
            } else {
62
                self::$controller = self::getController();
63
64
                $action = self::getAction();
65
66
                if (self::$controller->csrfVerification ?? true) {
67
                    Csrf::checkToken($request, session());
68
                }
69
70
                if (method_exists(self::$controller, '__before')) {
71
                    call_user_func_array([self::$controller, '__before'], self::getArgs($routeArgs, '__before', $request, $response));
72
                }
73
74
                call_user_func_array([self::$controller, $action], self::getArgs($routeArgs, $action, $request, $response));
75
76
                if (method_exists(self::$controller, '__after')) {
77
                    call_user_func_array([self::$controller, '__after'], self::getArgs($routeArgs, '__after', $request, $response));
78
                }
79
            }
80
        }
81
    }
82
83
    /**
84
     * Get Controller
85
     * @return object
86
     * @throws RouteException
87
     */
88
    private static function getController()
89
    {
90
        $controllerPath = modules_dir() . DS . current_module() . DS . 'Controllers' . DS . current_controller() . '.php';
91
92
        if (!file_exists($controllerPath)) {
93
            throw new RouteException(_message(ExceptionMessages::CONTROLLER_NOT_FOUND, current_controller()));
94
        }
95
96
        require_once $controllerPath;
97
98
        $controllerClass = '\\Modules\\' . current_module() . '\\Controllers\\' . current_controller();
99
100
        if (!class_exists($controllerClass, false)) {
101
            throw new RouteException(_message(ExceptionMessages::CONTROLLER_NOT_DEFINED, current_controller()));
102
        }
103
104
        return new $controllerClass();
105
    }
106
107
    /**
108
     * Get Action
109
     * @return string
110
     * @throws RouteException
111
     */
112
    private static function getAction()
113
    {
114
        $action = current_action();
115
116
        if ($action && !method_exists(self::$controller, $action)) {
117
            throw new RouteException(_message(ExceptionMessages::ACTION_NOT_DEFINED, $action));
118
        }
119
120
        return $action;
121
    }
122
123
    /**
124
     * Get Args
125
     * @param  array $routeArgs
126
     * @param  string $action
127
     * @return array
128
     * @throws \ReflectionException
129
     */
130
    private static function getArgs($routeArgs, $action)
131
    {
132
        return Di::autowire(get_class(self::$controller) . ':' . $action, $routeArgs);
133
    }
134
135
    /**
136
     * Get Callback Args
137
     * @param  array $routeArgs
138
     * @param  \Closure $callback
139
     * @return array
140
     * @throws \ReflectionException
141
     */
142
    private static function getCallbackArgs($routeArgs, $callback)
143
    {
144
        return Di::autowire($callback, $routeArgs);
145
    }
146
147
}
148