Test Setup Failed
Push — master ( 1ac915...9b6ae8 )
by Mehmet
09:50
created

ApplicationResponse   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 9
dl 0
loc 121
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B returnResponse() 0 60 9
A renderResponse() 0 13 1
A checkTemplateFile() 0 12 2
A notFound() 0 12 2
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
19
class ApplicationResponse
20
{
21
    private $controllerClass;
22
    private $controllerResponse;
23
    private $config;
24
    private $view;
25
    private $response;
26
27
    public function __construct(
28
        string $controllerClass,
29
        ControllerResponse $controllerResponse,
30
        Config $config,
31
        ViewInterface $view
32
    ) {
33
        $this->controllerClass = $controllerClass;
34
        $this->controllerResponse = $controllerResponse;
35
        $this->config = $config;
36
        $this->view = $view;
37
    }
38
39
    public function returnResponse() : ResponseInterface
40
    {
41
        switch ($this->controllerResponse->getReturnType()) {
42
            case Router::HTML:
43
                return new HtmlResponse(
44
                    $this->renderResponse(),
45
                    $this->controllerResponse->getStatusCode(),
46
                    $this->controllerResponse->getHeaders()
47
                );
48
                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...
49
            case Router::JSON:
50
                return new JsonResponse(
51
                    $this->controllerResponse->getData(),
52
                    $this->controllerResponse->getStatusCode(),
53
                    $this->controllerResponse->getHeaders()
54
                );
55
                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...
56
            case Router::TEXT:
57
                return new TextResponse(
58
                    $this->renderResponse(),
59
                    $this->controllerResponse->getStatusCode(),
60
                    $this->controllerResponse->getHeaders()
61
                );
62
                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...
63
            case Router::XML:
64
                return new XmlResponse(
65
                    $this->renderResponse(),
66
                    $this->controllerResponse->getStatusCode(),
67
                    $this->controllerResponse->getHeaders()
68
                );
69
                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...
70
            case Router::DOWNLOAD:
71
                $stream = $this->controllerResponse->getMetaData()['stream']->read();
72
                return (new EmptyResponse(
73
                    $this->controllerResponse->getStatusCode(),
74
                    $this->controllerResponse->getHeaders()
75
                ))->withBody($stream);
76
                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...
77
            case Router::REDIRECT:
78
                return new RedirectResponse(
79
                    $this->controllerResponse->getMetaData()['uri'],
80
                    $this->controllerResponse->getStatusCode(),
81
                    $this->controllerResponse->getHeaders()
82
                );
83
                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...
84
            case Router::CUSTOM:
85
                return new HtmlResponse(
86
                    $this->renderResponse(),
87
                    $this->controllerResponse->getStatusCode(),
88
                    $this->controllerResponse->getHeaders()
89
                );
90
                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...
91
            case Router::EMPTY:
92
                return new EmptyResponse(
93
                    $this->controllerResponse->getStatusCode(),
94
                    $this->controllerResponse->getHeaders()
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
        }
98
    }
99
100
    private function renderResponse() : string
101
    {
102
        $paths = explode("\\", $this->controllerClass);
103
        $templateFile = array_pop($paths);
104
        $templateFolder = array_pop($paths);
105
        $template = CaseConverter::toSnakeCase($templateFolder)
106
            . '/' . CaseConverter::toSnakeCase($templateFile);
107
        $layout = $this->controllerResponse->getMetaData()['layout'] ?? $template;
108
        $templatePath = $layout. '.twig';
109
110
        $this->checkTemplateFile($templatePath, 'Method\'s', $this->controllerClass);
111
        return $this->view->render($templatePath, $this->controllerResponse->getData());
112
    }
113
114
    private function checkTemplateFile($template, $type, $controller) : void
115
    {
116
        if (!file_exists($this->config->app->get('templates_path') .'/'. $template)) {
117
            $message  = sprintf(
118
                '%s  template file not found! %s  needs a main template file at: %s',
119
                $type,
120
                $controller,
121
                $this->config['app_dir'] .'/'. $template
122
            );
123
            throw new \DomainException($message);
124
        }
125
    }
126
127
    public function notFound(int $status, int $returnType, string $message) : ResponseInterface
128
    {
129
        if ($returnType === Router::JSON) {
130
            return new JsonResponse(['status' => $status, 'message' => $message], $status);
131
        }
132
        $notFoundTemplate = '_layouts/404.twig';
133
        $content = $this->view->render(
134
            $notFoundTemplate,
135
            ['message' => $message, 'status' => $status]
136
        );
137
        return new HtmlResponse($content, $status);
138
    }
139
}
140