Controller   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 3 Features 3
Metric Value
c 8
b 3
f 3
dl 0
loc 102
ccs 0
cts 40
cp 0
rs 10
wmc 7
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initialize() 0 15 1
A out() 0 4 1
A create() 0 4 1
A getResponse() 0 4 1
A setResponse() 0 4 1
A view() 0 7 1
1
<?php
2
namespace JayaCode\Framework\Core\Controller;
3
4
use JayaCode\Framework\Core\Application\Application;
5
use JayaCode\Framework\Core\Http\Request;
6
use JayaCode\Framework\Core\Http\Response;
7
use JayaCode\Framework\Core\Session\Session;
8
use JayaCode\Framework\Core\View\View;
9
10
/**
11
 * Class Controller
12
 * @package JayaCode\Framework\Core\Controller
13
 */
14
class Controller
15
{
16
    /**
17
     * @var Application
18
     */
19
    protected $app;
20
    /**
21
     * @var Request
22
     */
23
    protected $request;
24
25
    /**
26
     * @var Response
27
     */
28
    protected $response;
29
30
    /**
31
     * @var Session
32
     */
33
    protected $session;
34
35
    /**
36
     * @var View
37
     */
38
    protected $viewEngine;
39
40
    /**
41
     * Controller constructor.
42
     * @param Application $app
43
     */
44
    public function __construct(Application $app)
45
    {
46
        $this->initialize($app);
47
    }
48
49
    /**
50
     * @param Application $app
51
     */
52
    public function initialize(Application $app)
53
    {
54
        $this->app = $app;
55
        $this->request = $app->request;
56
        $this->response = $app->response;
57
        $this->session = $app->session;
58
59
        $this->viewEngine = new View(config("app.viewDir", __DIR__));
60
        $this->viewEngine->vars->add([
61
            "app" => $this->app,
62
            "request" => $this->request,
63
            "response" => $this->response,
64
            "session" => $this->session
65
        ]);
66
    }
67
68
    /**
69
     * @param Response $response
70
     * @return Response
71
     */
72
    public function out(Response $response)
73
    {
74
        return $response->send();
75
    }
76
77
    /**
78
     * @param Application $app
79
     * @return Controller
80
     */
81
    public static function create(Application $app)
82
    {
83
        return new static($app);
84
    }
85
86
    /**
87
     * @return Response
88
     */
89
    public function getResponse()
90
    {
91
        return $this->response;
92
    }
93
94
    /**
95
     * @param Response $response
96
     */
97
    public function setResponse($response)
98
    {
99
        $this->response = $response;
100
    }
101
102
    /**
103
     * Render Template View
104
     * @param $name
105
     * @param array $data
106
     * @return string
107
     */
108
    protected function view($name, array $data = array())
109
    {
110
        $tpl = $this->viewEngine->template($name);
111
        $tpl->vars->add($data);
112
113
        return $tpl->render();
114
    }
115
}
116