Test Setup Failed
Push — master ( 4d9a9d...fda555 )
by Mehmet
02:43
created

ApplicationResponse::renderResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Zend\Config\Config;
8
use Selami\View\ViewInterface;
9
use Selami\Router\Router;
10
use Selami\Stdlib\CaseConverter;
11
use Zend\Diactoros\Response;
12
use Zend\Diactoros\Response\JsonResponse;
13
use Zend\Diactoros\Response\HtmlResponse;
14
use Zend\Diactoros\Response\TextResponse;
15
use Zend\Diactoros\Response\RedirectResponse;
16
use Zend\Diactoros\Response\EmptyResponse;
17
use Zend\Diactoros\Response\XmlResponse;
18
use Zend\Diactoros\Stream;
19
20
class ApplicationResponse
21
{
22
    private $controllerClass;
23
    private $controllerResponse;
24
    private $config;
25
    private $view;
26
    private $headers;
27
28
    public function __construct(
29
        string $controllerClass,
30
        ControllerResponse $controllerResponse,
31
        Config $config,
32
        ViewInterface $view
33
    ) {
34
        $this->controllerClass = $controllerClass;
35
        $this->controllerResponse = $controllerResponse;
36
        $this->config = $config;
37
        $this->headers = isset( $config->get('app')['default_headers']) ?
38
            $config->get('app')->get('default_headers')->toArray() : [];
39
        $this->view = $view;
40
    }
41
42
    public function getResponseHeaders() : array
43
    {
44
        return array_merge($this->headers, $this->controllerResponse->getHeaders());
45
    }
46
47
    public function returnResponse() : ResponseInterface
48
    {
49
        switch ($this->controllerResponse->getReturnType()) {
50
            case Router::HTML:
51
                return new HtmlResponse(
52
                    $this->renderResponse(),
53
                    $this->controllerResponse->getStatusCode(),
54
                    $this->getResponseHeaders()
55
                );
56
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
57
            case Router::JSON:
58
                return new JsonResponse(
59
                    $this->controllerResponse->getData(),
60
                    $this->controllerResponse->getStatusCode(),
61
                    $this->getResponseHeaders()
62
                );
63
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64
            case Router::TEXT:
65
                return new TextResponse(
66
                    $this->renderResponse(),
67
                    $this->controllerResponse->getStatusCode(),
68
                    $this->getResponseHeaders()
69
                );
70
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
71
            case Router::XML:
72
                return new XmlResponse(
73
                    $this->renderResponse(),
74
                    $this->controllerResponse->getStatusCode(),
75
                    $this->getResponseHeaders()
76
                );
77
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
78
            case Router::DOWNLOAD:
79
                $metaData = $this->controllerResponse->getMetaData();
80
                /**
81
                 * @var $stream Stream
82
                 */
83
                $stream = $metaData['stream'];
84
                return new Response(
85
                    $stream,
86
                    $this->controllerResponse->getStatusCode(),
87
                    $this->getResponseHeaders()
88
                );
89
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
90
            case Router::REDIRECT:
91
                return new RedirectResponse(
92
                    $this->controllerResponse->getMetaData()['uri'],
93
                    $this->controllerResponse->getStatusCode(),
94
                    $this->getResponseHeaders()
95
                );
96
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
97
            case Router::CUSTOM:
98
                return new HtmlResponse(
99
                    $this->renderResponse(),
100
                    $this->controllerResponse->getStatusCode(),
101
                    $this->getResponseHeaders()
102
                );
103
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
104
            case Router::EMPTY:
105
                return new EmptyResponse(
106
                    $this->controllerResponse->getStatusCode(),
107
                    $this->getResponseHeaders()
108
                );
109
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
110
        }
111
    }
112
113
    private function renderResponse() : string
114
    {
115
        $paths = explode("\\", $this->controllerClass);
116
        $templateFile = array_pop($paths);
117
        $templateFolder = array_pop($paths);
118
        $template = CaseConverter::toSnakeCase($templateFolder)
119
            . '/' . CaseConverter::toSnakeCase($templateFile);
120
        $layout = $this->controllerResponse->getMetaData()['layout'] ?? $template;
121
        $templatePath = $layout. '.' . $this->config->view->get('template_file_extension');
122
123
        $this->checkTemplateFile($templatePath, 'Method\'s', $this->controllerClass);
124
        return $this->view->render($templatePath, $this->controllerResponse->getData());
125
    }
126
127
    private function checkTemplateFile($template, $type, $controller) : void
128
    {
129
        if (!file_exists($this->config->view->get('templates_path') .'/'. $template)) {
130
            $message  = sprintf(
131
                '%s  template file not found! %s  needs a main template file at: %s',
132
                $type,
133
                $controller,
134
                $this->config['app_dir'] .'/'. $template
135
            );
136
            throw new \DomainException($message);
137
        }
138
    }
139
140
    public function notFound(int $status, int $returnType, string $message) : ResponseInterface
141
    {
142
        if ($returnType === Router::JSON) {
143
            return new JsonResponse(['status' => $status, 'message' => $message], $status);
144
        }
145
        $notFoundTemplate = '_layouts/404.twig';
146
        $content = $this->view->render(
147
            $notFoundTemplate,
148
            ['message' => $message, 'status' => $status]
149
        );
150
        return new HtmlResponse($content, $status);
151
    }
152
}
153