Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

Application::getResponse()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.3158
c 0
b 0
f 0
cc 6
nc 8
nop 4

How to fix   Long Method   

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
/**
3
 * @license MIT
4
 */
5
namespace Pivasic\Core;
6
7
use Pivasic\Bundle\Template\Native;
8
9
/**
10
 * Initialize autoload.
11
 * Process request and get response.
12
 *
13
 * Class Application
14
 * @package Dspbee\Core
15
 */
16
class Application
17
{
18
    /**
19
     * Application constructor.
20
     *
21
     * @param string $appRoot
22
     */
23
    public function __construct($appRoot)
24
    {
25
        $appRoot = rtrim($appRoot, '/') . '/';
26
        $this->packageRoot = $appRoot;
27
28
        /**
29
         * Register autoload to app/$package/src dir's.
30
         */
31
        spl_autoload_register(function ($path) use ($appRoot) {
32
            $path = explode('\\', $path);
33
            array_shift($path);                           // Vendor
34
            $package = $appRoot . array_shift($path);     // Package
35
            $path = $package . '/src/' . implode('/', $path) . '.php';
36
            if (file_exists($path)) {
37
                require_once $path;
38
            }
39
        });
40
    }
41
42
    /**
43
     * Process request.
44
     *
45
     * @param array $packageList
46
     * @param array $languageList
47
     * @param array $customRouteList
48
     * @param null|string $url
49
     *
50
     * @return Response
51
     */
52
    public function getResponse(array $packageList, array $languageList, array $customRouteList, $url = null): Response
53
    {
54
        $request = new Request($languageList, $packageList, $url);
55
56
        $this->packageRoot .= $request->package() . '/';
57
58
        /**
59
         * Process request.
60
         */
61
        if (isset($customRouteList[$request->package()])) {
62
            /**
63
             * Custom routing.
64
             */
65
            /**
66
             * Path to router class.
67
             */
68
            $path = $this->packageRoot . 'CustomRoute.php';
69
            if (file_exists($path)) {
70
                require $path;
71
                /**
72
                 * Name of router class.
73
                 */
74
                $route = $request->package() . '\\CustomRoute';
75
                /**
76
                 * @var IRoute $route
77
                 */
78
                $route = new $route();
79
                $response = $route->getResponse($this->packageRoot, $request);
80
                if (null !== $response) {
81
                    return $response;
82
                }
83
            } else {
84
                throw new \RuntimeException(sprintf('The file "%s" does not exist', $path));
85
            }
86
        }
87
88
        $response = (new DefaultRoute())->getResponse($this->packageRoot, $request);
89
        if (null !== $response) {
90
            return $response;
91
        }
92
93
94
        /**
95
         * If not found.
96
         */
97
        $response = new Response();
98
        $response->setStatusCode(404);
99
100
        $content = '404 Not Found';
101
        if (file_exists($this->packageRoot . '/view/404.html.php')) {
102
            $content = (new Native($this->packageRoot))->getContent('404.html.php');
103
        }
104
105
        $response->setContent($content);
106
107
        return $response;
108
    }
109
110
    private $packageRoot;
111
}