AbstractController::responseHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\mvc\controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use WebComplete\core\utils\container\ContainerInterface;
9
use WebComplete\mvc\view\View;
10
use WebComplete\mvc\view\ViewInterface;
11
12
abstract class AbstractController
13
{
14
15
    /**
16
     * @var ContainerInterface
17
     */
18
    protected $container;
19
    /**
20
     * @var ViewInterface
21
     */
22
    protected $view;
23
    /**
24
     * @var Request
25
     */
26
    protected $request;
27
    /**
28
     * @var Response
29
     */
30
    protected $response;
31
    protected $layout;
32
33
    /**
34
     * @param ContainerInterface $container
35
     */
36
    public function __construct(ContainerInterface $container)
37
    {
38
        $this->container = $container;
39
        $this->request = $this->container->get(Request::class);
40
        $this->response = $this->container->get(Response::class);
41
        $this->view = $this->container->make(View::class);
42
        $this->view->setController($this);
43
    }
44
45
    /**
46
     * @return bool|string|Response
47
     */
48
    public function beforeAction()
49
    {
50
        return true;
51
    }
52
53
    /**
54
     * @param $result
55
     *
56
     * @return mixed
57
     */
58
    public function afterAction($result)
59
    {
60
        return $result;
61
    }
62
63
    /**
64
     * @param $templatePath
65
     * @param array $vars
66
     *
67
     * @return Response
68
     * @throws \Exception
69
     */
70
    protected function responseHtml($templatePath, array $vars = []): Response
71
    {
72
        $html = $this->view->layout($this->layout)->render($templatePath, $vars);
73
        $this->updateResponse(200, 'text/html', $html);
74
        return $this->response;
75
    }
76
77
    /**
78
     * @param $templatePath
79
     * @param array $vars
80
     *
81
     * @return Response
82
     * @throws \Exception
83
     */
84
    protected function responseHtmlPartial($templatePath, array $vars = []): Response
85
    {
86
        $html = $this->view->layout()->render($templatePath, $vars);
87
        $this->updateResponse(200, 'text/html', $html);
88
        return $this->response;
89
    }
90
91
    /**
92
     * @param array $data
93
     *
94
     * @return Response
95
     * @throws \Exception
96
     */
97
    protected function responseJson(array $data): Response
98
    {
99
        $this->updateResponse(200, 'application/json', \json_encode($data));
100
        return $this->response;
101
    }
102
103
    /**
104
     * @param string $url
105
     * @param int $code
106
     * @param array $headers
107
     *
108
     * @return RedirectResponse
109
     * @throws \InvalidArgumentException
110
     */
111
    protected function responseRedirect(string $url, int $code = 302, array $headers = []): RedirectResponse
112
    {
113
        return new RedirectResponse($url, $code, $headers);
114
    }
115
116
    /**
117
     * @return Response
118
     * @throws \InvalidArgumentException
119
     */
120
    protected function responseNotFound(): Response
121
    {
122
        $this->response->setStatusCode(404);
123
        return $this->response;
124
    }
125
126
    /**
127
     * @return Response
128
     * @throws \InvalidArgumentException
129
     */
130
    protected function responseAccessDenied(): Response
131
    {
132
        $this->response->setStatusCode(403);
133
        return $this->response;
134
    }
135
136
    /**
137
     * @param $statusCode
138
     * @param $contentType
139
     * @param $content
140
     *
141
     * @throws \InvalidArgumentException
142
     * @throws \UnexpectedValueException
143
     */
144
    private function updateResponse($statusCode, $contentType, $content)
145
    {
146
        $this->response->setStatusCode($statusCode);
147
        $this->response->headers->set('content-type', $contentType);
148
        $this->response->setContent($content);
149
    }
150
}
151