GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

App   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 3
dl 0
loc 221
rs 9.6
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C run() 0 45 7
B invoke() 0 22 4
B isControllerValid() 0 12 5
B isMethodValid() 0 12 5
A areArgsValid() 0 11 4
B splitUrl() 0 17 5
A notFound() 0 3 1
1
<?php
2
3
/**
4
 * The application class.
5
 *
6
 * Handles the request for each call to the application.
7
 *
8
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
9
 * @author     Omar El Gabry <[email protected]>
10
 */
11
12
class App {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
14
    /**
15
     * controller
16
     * @var mixed
17
     */
18
    private $controller = null;
19
20
    /**
21
     * action method
22
     * @var string
23
     */
24
    private $method = null;
25
26
    /**
27
     * passed arguments
28
     * @var array
29
     */
30
    private $args = array();
31
32
    /**
33
     * request
34
     * @var Request
35
     */
36
    public $request = null;
37
38
    /**
39
     * response
40
     * @var Response
41
     */
42
    public $response = null;
43
44
    /**
45
     * application constructor
46
     *
47
     * @access public
48
     */
49
    public function __construct(){
50
51
        // initialize request and respond objects
52
        $this->request  = new Request();
53
        $this->response = new Response();
54
55
    }
56
57
    public function run(){
58
59
        // split the requested URL
60
        $this->splitUrl();
61
62
        if(!self::isControllerValid($this->controller)){
63
            return $this->notFound();
64
        }
65
66
        if(!empty($this->controller)){
67
68
            $controllerName = $this->controller;
69
70
            if(!self::isMethodValid($controllerName, $this->method)){
71
                return $this->notFound();
72
            }
73
74
            if(!empty($this->method)){
75
76
                if(!self::areArgsValid($controllerName, $this->method, $this->args)){
77
                    return $this->notFound();
78
                }
79
80
                // finally instantiate the controller object, and call it's action method.
81
                return $this->invoke($controllerName, $this->method, $this->args);
82
83
            } else{
84
85
                $this->method = "index";
86
                if(!method_exists($controllerName, $this->method)){
87
                    return $this->notFound();
88
                }
89
90
                return $this->invoke($controllerName, $this->method, $this->args);
91
            }
92
93
        } else{
94
95
            // if no controller defined,
96
            // then send to login controller, and it should take care of the request
97
            // either redirect to login page, or dashboard.
98
            $this->method = "index";
99
            return $this->invoke("LoginController", $this->method, $this->args);
100
        }
101
    }
102
103
    /**
104
     * instantiate controller object and trigger it's action method
105
     *
106
     * @param  string $controller
107
     * @param  string $method
108
     * @param  array  $args
109
     * @return Response 
110
     */
111
     private function invoke($controller, $method = "index", $args = []){
112
113
         $this->request->addParams(['controller' => $controller, 'action' => $method, 'args' => $args]);
114
         $this->controller = new $controller($this->request, $this->response);
115
116
         $result = $this->controller->startupProcess();
117
         if ($result instanceof Response) {
118
            return $result->send();
119
         }
120
121
         if(!empty($args)){
122
             $response = call_user_func_array([$this->controller, $method], $args);
123
         }else{
124
             $response = $this->controller->{$method}();
125
         }
126
127
         if ($response instanceof Response) {
128
            return $response->send();
129
         }
130
131
        return $this->response->send();
132
     }
133
134
    /**
135
     * detect if controller is valid
136
     *
137
     * any request to error controller will be considered as invalid,
138
     * because error pages will be rendered(even with ajax) from inside the application
139
     *
140
     * @param  string $controller
141
     * @return boolean
142
     */
143
    private static function isControllerValid($controller){
144
145
        if(!empty($controller)){
146
            if (!preg_match('/\A[a-z]+\z/i', $controller) ||
147
                strtolower($controller) === "errorscontroller" ||
148
                !file_exists(APP . 'controllers/' . $controller . '.php')){
149
                return false;
150
            }else { return true; }
151
152
        }else { return true; }
153
154
    }
155
156
    /**
157
     * detect if action method is valid
158
     *
159
     * make a request to 'index' method will be considered as invalid,
160
     * the constructor will take care of index methods.
161
     *
162
     * @param string $controller
163
     * @param string $method
164
     * @return boolean
165
     */
166
    private static function isMethodValid($controller, $method){
167
168
        if(!empty($method)){
169
            if (!preg_match('/\A[a-z]+\z/i', $method) ||
170
                !method_exists($controller, $method)  ||
171
                strtolower($method) === "index" ){
172
                return false;
173
            }else { return true; }
174
175
        }else { return true; }
176
177
    }
178
179
    /**
180
     * detect if arguments are valid(number and alphanumeric)
181
     *
182
     * You can enhance, or modify this method, or don't use it at all.
183
     *
184
     * @param string $controller
185
     * @param string $method
186
     * @param array  $args
187
     * @return boolean
188
     * @see http://stackoverflow.com/questions/346777/how-to-dynamically-check-number-of-arguments-of-a-function-in-php?lq=1
189
     */
190
    private static function areArgsValid($controller, $method, $args){
191
192
        $reflection = new ReflectionMethod ($controller, $method);
193
        $_args = $reflection->getNumberOfParameters();
194
195
        if($_args !== count($args)) { return false; }
196
        foreach($args as $arg){
197
            if(!preg_match('/\A[a-z0-9]+\z/i', $arg)){ return false; }
198
        }
199
        return true;
200
    }
201
202
    /**
203
     * Split the URL for the current request.
204
     *
205
     */
206
    public function splitUrl(){
207
208
        $url = $this->request->query("url");
209
210
        if (!empty($url)) {
211
212
            $url = explode('/', filter_var(trim($url, '/'), FILTER_SANITIZE_URL));
213
214
            $this->controller = !empty($url[0]) ? ucwords($url[0]) . 'Controller' : null;
215
            $this->method = !empty($url[1]) ? $url[1] : null;
216
217
            unset($url[0], $url[1]);
218
219
            $this->args = !empty($url)? array_values($url): [];
220
221
        }
222
    }
223
224
    /**
225
     * Shows not found error page
226
     *
227
     */
228
    private function notFound(){
229
        return (new ErrorsController())->error(404)->send();
230
    }
231
232
}
233