Completed
Push — master ( d1373b...6613a6 )
by Igor
03:16
created

DefaultRoute   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getResponse() 0 33 5
1
<?php
2
/**
3
 * @license MIT
4
 * @author Igor Sorokin <[email protected]>
5
 */
6
namespace Dspbee\Core;
7
8
/**
9
 * Base routing.
10
 *
11
 * Class DefaultRoute
12
 * @package Dspbee\Core
13
 */
14
class DefaultRoute implements IRoute
15
{
16
    /**
17
     * Get object of Response.
18
     *
19
     * @param string $packageRoot
20
     * @param Request $request
21
     *
22
     * @return Response|null
23
     */
24
    public function getResponse($packageRoot, Request $request)
25
    {
26
        $packageRoot = rtrim($packageRoot, '/');
27
        $path = $packageRoot . '/Route/' . $request->route() . '/' . $request->method() . '.php';
28
        if (file_exists($path)) {
29
            require $path;
30
            $controllerClass = $request->package() . '\\Route_' . str_replace('/', '_', $request->route()) . '\\' . $request->method();
31
32
            preg_match('/^[\\a-zA-Z0-9_-]*$/iD', $controllerClass, $match);
33
            if (isset($match[0])) {
34
                $controllerClass = $match[0];
35
                /**
36
                 * @var BaseController $controller
37
                 */
38
                $controller = new $controllerClass($packageRoot, $request);
39
40
                /**
41
                 * Call handler.
42
                 */
43
                $handler = $_POST['handler'] ?? $_GET['handler'] ?? 'index';
44
                preg_match('/^[a-zA-Z0-9_-]*$/iD', $handler, $match);
45
                if (isset($match[0])) {
46
                    $handler = $match[0];
47
                    if (method_exists($controllerClass, $handler)) {
48
                        $controller->$handler();
49
                        return $controller->getResponse();
50
                    }
51
                }
52
            }
53
        }
54
55
        return null;
56
    }
57
}