Passed
Pull Request — master (#410)
by Arman
03:12
created

WebAppTrait::initializeRequestResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\App\Traits;
16
17
use Quantum\App\Exceptions\StopExecutionException;
18
use Quantum\Environment\Exceptions\EnvException;
19
use Quantum\Libraries\ResourceCache\ViewCache;
20
use Quantum\Module\Exceptions\ModuleException;
21
use Quantum\Config\Exceptions\ConfigException;
22
use Quantum\Router\Exceptions\RouteException;
23
use Quantum\Http\Exceptions\HttpException;
24
use Quantum\App\Exceptions\BaseException;
25
use Quantum\Di\Exceptions\DiException;
26
use Quantum\Environment\Environment;
27
use Quantum\Module\ModuleLoader;
28
use Quantum\Router\RouteBuilder;
29
use DebugBar\DebugBarException;
30
use Quantum\Environment\Server;
31
use Quantum\Debugger\Debugger;
32
use Quantum\Http\Response;
33
use Quantum\Router\Router;
34
use Quantum\Http\Request;
35
use Quantum\Loader\Setup;
36
use ReflectionException;
37
use Quantum\Di\Di;
38
39
/**
40
 * Trait WebAppTrait
41
 * @package Quantum\App
42
 */
43
trait WebAppTrait
44
{
45
    /**
46
     * @throws DiException
47
     * @throws ReflectionException
48
     * @throws EnvException
49
     * @throws BaseException
50
     */
51
    protected function loadEnvironment()
52
    {
53
        Environment::getInstance()->load(new Setup('config', 'env'));
54
    }
55
56
    /**
57
     * @param Request $request
58
     * @param Response $response
59
     * @throws BaseException
60
     * @throws DiException
61
     * @throws ReflectionException
62
     * @throws HttpException
63
     */
64
    private function initializeRequestResponse(Request $request, Response $response)
65
    {
66
        $request->init(Server::getInstance());
0 ignored issues
show
Bug introduced by
The method init() does not exist on Quantum\Http\Request. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $request->/** @scrutinizer ignore-call */ 
67
                  init(Server::getInstance());
Loading history...
67
        $response->init();
68
    }
69
70
    /**
71
     * @throws DebugBarException
72
     */
73
    private function initializeDebugger()
74
    {
75
        $debugger = Debugger::getInstance();
76
        $debugger->initStore();
77
    }
78
79
    /**
80
     * Load modules
81
     * @throws ModuleException
82
     */
83
    private function loadModules()
84
    {
85
        $moduleLoader = ModuleLoader::getInstance();
86
87
        $modulesDependencies = $moduleLoader->loadModulesDependencies();
88
        Di::registerDependencies($modulesDependencies);
89
90
        $builder = new RouteBuilder();
91
        $allRoutes = $builder->build($moduleLoader->loadModulesRoutes(), $moduleLoader->getModuleConfigs());
92
93
        Router::setRoutes($allRoutes);
94
    }
95
96
    /**
97
     * @return ViewCache
98
     * @throws ConfigException
99
     * @throws DiException
100
     * @throws ReflectionException
101
     */
102
    private function setupViewCache(): ViewCache
103
    {
104
        $viewCache = ViewCache::getInstance();
105
106
        if ($viewCache->isEnabled()) {
107
            $viewCache->setup();
108
        }
109
110
        return $viewCache;
111
    }
112
113
    /**
114
     * @param $request
115
     * @throws BaseException
116
     * @throws ConfigException
117
     * @throws DebugBarException
118
     * @throws DiException
119
     * @throws ReflectionException
120
     * @throws RouteException
121
     * @throws StopExecutionException
122
     */
123
    private function initializeRouter($request)
124
    {
125
        $router = new Router($request);
126
        $router->findRoute();
127
    }
128
129
    /**
130
     * @param Response $response
131
     * @throws ConfigException
132
     * @throws DiException
133
     * @throws ReflectionException
134
     */
135
    private function handleCors(Response $response)
136
    {
137
        if (!config()->has('cors')) {
138
            config()->import(new Setup('config', 'cors'));
139
        }
140
141
        foreach (config()->get('cors') as $key => $value) {
142
            $response->setHeader($key, $value);
143
        }
144
    }
145
}
146