Passed
Push — master ( 3aecf0...39a7f4 )
by Radu
01:48
created

Application   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
eloc 54
dl 0
loc 110
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A shutdown() 0 15 3
A run() 0 17 5
B execute() 0 34 11
A start() 0 21 3
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%s", $this->projectNamespace, $class, $class, $classType);
82
        if (!class_exists($className)) {
83
            throw new NotFoundException(
84
                sprintf('No matching %s found', $classType)
85
            );
86
        }
87
        $object = new $className;
88
        $parent = get_parent_class($object);
89
        if (method_exists((string) $parent, $method) ||
90
            !is_callable([$className, $method])) {
91
            throw new NotFoundException('No matching Action found');
92
        }
93
        $callable = [$object, $method];
94
        if (!is_callable($callable)) {
95
            throw new ApplicationException('Method not found');
96
        }
97
        return call_user_func_array($callable, $args);
98
    }
99
100
    /**
101
     * Finishes the execution of the Application.
102
     *
103
     * This method is also registered as a shutdown handler.
104
     */
105
    final public function shutdown($exception = null, $manual = false, $statusCode = 0)
106
    {
107
        $hasError = $this->handleErrors($exception);
108
        if ($hasError) {
109
            $statusCode = 1;
110
        }
111
112
        if (!$manual) { //if shutdown handler
113
            /**
114
             * Warning: this part will always be executed,
115
             * independent of the outcome of the script.
116
             */
117
            ErrorHandler::restore();
118
        }
119
        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...
120
    }
121
}
122