Test Failed
Branch verbAction (28ca93)
by Jay
02:03
created

Framework::dispatch()   D

Complexity

Conditions 11
Paths 176

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
rs 4.9629
c 1
b 0
f 0
cc 11
eloc 19
nc 176
nop 0

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
declare(strict_types = 1);
3
4
namespace Nano;
5
6
/**
7
 * Class Framework
8
 * @package Nano
9
 */
10
final class Framework
11
{
12
    private $projectNamespace = '\Project';
13
    private $controllerPackage = '\Controller';
14
    private $controllerActionSuffix = 'Action';
15
16
    /**
17
     * Dispatch the request
18
     * @throws \Exception
19
     * @return mixed
20
     */
21
    public function dispatch()
22
    {
23
        $requestUri = $_SERVER['REQUEST_URI'];
24
        $verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get';
25
        $appendUri = strpos($requestUri, '?');
26
        $query = substr($requestUri, 0, $appendUri === false ? strlen($requestUri) : $appendUri);
27
        $parts = explode('/', preg_replace('~^' . Basepath::get() . '~', '', $query));
28
        $action = count($parts) >= 2 ? array_pop($parts) : 'index';
29
        $controllerName = isset($parts[0]) && $parts[0] ? implode($parts, '\\') : 'index';
30
        $controller = $this->projectNamespace . $this->controllerPackage . '\\' . ucfirst($controllerName);
31
        if (!class_exists($controller)) {
32
            throw new \Exception('controller ' . $controllerName . ' not found');
33
        };
34
        $controller = new $controller;
35
        $finalAction = $verb . ($action ?: 'index') . $this->controllerActionSuffix;
36
        if (is_callable(array($controller, $finalAction))) {
37
            return $controller->$finalAction();
38
        }
39
        $finalAction = ($action ?: 'index') . $this->controllerActionSuffix;
40
        if (!is_callable(array($controller, $finalAction))) {
41
            throw new \Exception('action ' . $action . ' not found in controller ' . $controllerName);
42
        }
43
        return $controller->$finalAction();
44
    }
45
46
    /**
47
     * Redefine personal namespace
48
     * @param string $namespace
49
     * @return Framework
50
     */
51
    public function setNamespace(string $namespace = '\Project') : Framework
52
    {
53
        $this->projectNamespace = strlen($namespace) && $namespace{0} != '\\' ? '\\' . $namespace : $namespace;
54
        return $this;
55
    }
56
57
    /**
58
     * Redefine controller subpackage
59
     * @param string $controllerPackage
60
     * @return Framework
61
     */
62
    public function setControllerPackage(string $controllerPackage = '\Controller') : Framework
63
    {
64
        $this->controllerPackage = strlen($controllerPackage) && $controllerPackage{0} != '\\'
65
            ? '\\' . $controllerPackage
66
            : $controllerPackage;
67
        return $this;
68
    }
69
70
    /**
71
     * Redefine controller action suffix
72
     * @param string $suffix
73
     * @return Framework
74
     */
75
    public function setControllerActionSuffix(string $suffix = 'Action') : Framework
76
    {
77
        $this->controllerActionSuffix = (string)$suffix;
78
        return $this;
79
    }
80
}
81