Passed
Pull Request — master (#190)
by Arman
02:51
created

WebAppTrait::setupViewCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
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 2.9.5
13
 */
14
15
namespace Quantum\App\Traits;
16
17
use Quantum\Libraries\Config\Exceptions\ConfigException;
18
use Quantum\Router\Exceptions\ModuleLoaderException;
19
use Quantum\Renderer\Exceptions\RendererException;
20
use Quantum\Exceptions\StopExecutionException;
21
use Quantum\Libraries\ResourceCache\ViewCache;
22
use Quantum\Router\Exceptions\RouteException;
23
use Quantum\Di\Exceptions\DiException;
24
use Quantum\Exceptions\BaseException;
25
use Quantum\Router\ModuleLoader;
26
use DebugBar\DebugBarException;
27
use Quantum\Environment\Server;
28
use Quantum\Debugger\Debugger;
29
use Quantum\Http\Response;
30
use Quantum\Router\Router;
31
use Quantum\Loader\Setup;
32
use ReflectionException;
33
34
/**
35
 * Trait WebAppTrait
36
 * @package Quantum\App
37
 */
38
trait WebAppTrait
39
{
40
41
    /**
42
     * @param $request
43
     * @param $response
44
     * @return void
45
     */
46
    private function initializeRequestResponse($request, $response)
47
    {
48
        $request->init(Server::getInstance());
49
        $response->init();
50
    }
51
52
    /**
53
     * @return void
54
     * @throws DebugBarException
55
     */
56
    private function initializeDebugger()
57
    {
58
        $debugger = Debugger::getInstance();
59
        $debugger->initStore();
60
    }
61
62
    /**
63
     * @return void
64
     * @throws ModuleLoaderException
65
     * @throws RouteException
66
     */
67
    private function loadModules()
68
    {
69
        ModuleLoader::getInstance()->loadModulesRoutes();
70
    }
71
72
    /**
73
     * @return void
74
     * @throws ConfigException
75
     * @throws DiException
76
     * @throws ReflectionException
77
     */
78
    private function setupViewCache()
79
    {
80
        $viewCache = ViewCache::getInstance();
81
82
        if ($viewCache->isEnabled()) {
83
            $viewCache->setup();
84
        }
85
    }
86
87
    /**
88
     * @param $request
89
     * @return void
90
     * @throws ConfigException
91
     * @throws DiException
92
     * @throws ReflectionException
93
     * @throws RouteException
94
     * @throws StopExecutionException
95
     * @throws BaseException
96
     * @throws RendererException
97
     */
98
    private function initializeRouter($request)
99
    {
100
        $router = new Router($request);
101
        $router->findRoute();
102
    }
103
104
    /**
105
     * @param Response $response
106
     * @return void
107
     * @throws ConfigException
108
     * @throws DiException
109
     * @throws ReflectionException
110
     */
111
    private function handleCors(Response $response)
112
    {
113
        if (!config()->has('cors')) {
114
            config()->import(new Setup('config', 'cors'));
115
        }
116
117
        foreach (config()->get('cors') as $key => $value) {
118
            $response->setHeader($key, $value);
119
        }
120
    }
121
}