Passed
Push — master ( 407b7e...67a3d5 )
by Radu
01:29
created

Application::execute()   C

Complexity

Conditions 12
Paths 128

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 47
rs 6.7333
c 0
b 0
f 0
cc 12
nc 128
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
namespace WebServCo\Framework;
3
4
use WebServCo\Framework\ErrorHandler;
5
use WebServCo\Framework\Framework;
6
use WebServCo\Framework\Settings;
7
use WebServCo\Framework\Exceptions\ApplicationException;
8
use WebServCo\Framework\Exceptions\NotFoundException;
9
10
class Application extends \WebServCo\Framework\AbstractApplication
11
{
12
    use \WebServCo\Framework\Traits\ExposeLibrariesTrait;
13
14
    /**
15
     * Starts the execution of the application.
16
     */
17
    final public function start()
18
    {
19
        try {
20
            ErrorHandler::set();
21
            register_shutdown_function([$this, 'shutdown']);
22
23
            $this->setEnvironmentValue();
24
25
            /**
26
             * With no argument, timezone will be set from the configuration.
27
             */
28
            $this->date()->setTimezone();
29
            /**
30
             * @todo i18n, log, session (if not cli), users (if not cli)
31
             */
32
33
            return true;
34
        } catch (\Throwable $e) { // php7
35
            $this->shutdown($e, true);
36
        } catch (\Exception $e) { // php5
37
            $this->shutdown($e, true);
38
        }
39
    }
40
41
    /**
42
     * Runs the application.
43
     */
44
    final public function run()
45
    {
46
        try {
47
            $response = $this->execute();
48
            if ($response instanceof
49
                \WebServCo\Framework\Interfaces\ResponseInterface) {
50
                $statusCode = $response->send();
51
                $this->shutdown(
52
                    null,
53
                    true,
54
                    Framework::isCli() ? $statusCode : 0
55
                );
56
            }
57
        } catch (\Throwable $e) { // php7
58
            $this->shutdown($e, true);
59
        } catch (\Exception $e) { // php5
60
            $this->shutdown($e, true);
61
        }
62
    }
63
64
    final protected function execute()
65
    {
66
        $classType = Framework::isCli() ? 'Command' : 'Controller';
67
        $route = $this->router()->getRoute(
68
            $this->request()->getTarget(),
69
            $this->router()->setting('routes'),
70
            $this->request()->getArgs()
71
        );
72
73
        $class = isset($route[0]) ? $route[0] : null;
74
        $method = isset($route[1]) ? $route[1] : null;
75
        $args = isset($route[2]) ? $route[2] : [];
76
77
        if (empty($class) || empty($method)) {
78
            throw new ApplicationException("Invalid route");
79
        }
80
81
        $className = sprintf("\\%s\\Domain\\%s\\%s", $this->projectNamespace, $class, $classType);
82
        if (!class_exists($className)) {
83
            /* enable in V10 *
84
            throw new NotFoundException(
85
                sprintf('No matching %s found', $classType)
86
            );
87
            /* enable in V10 */
88
89
            /* remove in V10 */
90
            // check for v9 class name
91
            $className = sprintf("\\%s\\Domain\\%s\\%s%s", $this->projectNamespace, $class, $class, $classType);
92
            if (!class_exists($className)) {
93
                throw new NotFoundException(
94
                    sprintf('No matching %s found', $classType)
95
                );
96
            }
97
            /* remove in V10 */
98
        }
99
100
        $object = new $className;
101
        $parent = get_parent_class($object);
102
        if (method_exists((string) $parent, $method) ||
103
            !is_callable([$className, $method])) {
104
            throw new NotFoundException('No matching Action found');
105
        }
106
        $callable = [$object, $method];
107
        if (!is_callable($callable)) {
108
            throw new ApplicationException('Method not found');
109
        }
110
        return call_user_func_array($callable, $args);
111
    }
112
113
    /**
114
     * Finishes the execution of the Application.
115
     *
116
     * This method is also registered as a shutdown handler.
117
     */
118
    final public function shutdown($exception = null, $manual = false, $statusCode = 0)
119
    {
120
        $hasError = $this->handleErrors($exception);
121
        if ($hasError) {
122
            $statusCode = 1;
123
        }
124
125
        if (!$manual) { //if shutdown handler
126
            /**
127
             * Warning: this part will always be executed,
128
             * independent of the outcome of the script.
129
             */
130
            ErrorHandler::restore();
131
        }
132
        exit($statusCode);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
133
    }
134
}
135