selamiphp /
foundation
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Selami; |
||
| 5 | |||
| 6 | use Psr\Http\Message\ResponseInterface; |
||
| 7 | use Psr\Http\Message\ServerRequestInterface; |
||
| 8 | use Zend\Config\Config; |
||
| 9 | use Selami\View\ViewInterface; |
||
| 10 | use Selami\Router\Router; |
||
| 11 | use Selami\Stdlib\CaseConverter; |
||
| 12 | use Zend\Diactoros\Response; |
||
| 13 | use Zend\Diactoros\Response\JsonResponse; |
||
| 14 | use Zend\Diactoros\Response\HtmlResponse; |
||
| 15 | use Zend\Diactoros\Response\TextResponse; |
||
| 16 | use Zend\Diactoros\Response\RedirectResponse; |
||
| 17 | use Zend\Diactoros\Response\EmptyResponse; |
||
| 18 | use Zend\Diactoros\Response\XmlResponse; |
||
| 19 | use Zend\Diactoros\Stream; |
||
| 20 | |||
| 21 | class ApplicationResponse |
||
| 22 | { |
||
| 23 | private $controllerClass; |
||
| 24 | private $controllerResponse; |
||
| 25 | private $config; |
||
| 26 | private $view; |
||
| 27 | private $headers; |
||
| 28 | |||
| 29 | public function __construct( |
||
| 30 | ServerRequestInterface $request, |
||
| 31 | string $controllerClass, |
||
| 32 | ControllerResponse $controllerResponse, |
||
| 33 | Config $config, |
||
| 34 | ViewInterface $view |
||
| 35 | ) { |
||
| 36 | $this->controllerClass = $controllerClass; |
||
| 37 | $this->controllerResponse = $controllerResponse; |
||
| 38 | $this->config = $config; |
||
| 39 | $this->headers = isset( $config->get('app')['default_headers']) ? |
||
| 40 | $config->get('app')->get('default_headers')->toArray() : []; |
||
| 41 | $this->view = $view; |
||
| 42 | $this->view->addGlobal('Request', $request); |
||
| 43 | $this->view->addGlobal( |
||
| 44 | 'QueryParameters', |
||
| 45 | array_merge($request->getQueryParams(), $request->getParsedBody()) |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getResponseHeaders() : array |
||
| 50 | { |
||
| 51 | return array_merge($this->headers, $this->controllerResponse->getHeaders()); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function returnResponse() : ResponseInterface |
||
| 55 | { |
||
| 56 | switch ($this->controllerResponse->getReturnType()) { |
||
| 57 | case Router::HTML: |
||
| 58 | return new HtmlResponse( |
||
| 59 | $this->renderResponse(), |
||
| 60 | $this->controllerResponse->getStatusCode(), |
||
| 61 | $this->getResponseHeaders() |
||
| 62 | ); |
||
| 63 | break; |
||
|
0 ignored issues
–
show
|
|||
| 64 | case Router::JSON: |
||
| 65 | return new JsonResponse( |
||
| 66 | $this->controllerResponse->getData(), |
||
| 67 | $this->controllerResponse->getStatusCode(), |
||
| 68 | $this->getResponseHeaders(), |
||
| 69 | JsonResponse::DEFAULT_JSON_FLAGS | JSON_PARTIAL_OUTPUT_ON_ERROR |
||
| 70 | ); |
||
| 71 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 72 | case Router::TEXT: |
||
| 73 | return new TextResponse( |
||
| 74 | $this->renderResponse(), |
||
| 75 | $this->controllerResponse->getStatusCode(), |
||
| 76 | $this->getResponseHeaders() |
||
| 77 | ); |
||
| 78 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 79 | case Router::XML: |
||
| 80 | return new XmlResponse( |
||
| 81 | $this->renderResponse(), |
||
| 82 | $this->controllerResponse->getStatusCode(), |
||
| 83 | $this->getResponseHeaders() |
||
| 84 | ); |
||
| 85 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 86 | case Router::DOWNLOAD: |
||
| 87 | $metaData = $this->controllerResponse->getMetaData(); |
||
| 88 | /** |
||
| 89 | * @var $stream Stream |
||
| 90 | */ |
||
| 91 | $stream = $metaData['stream']; |
||
| 92 | return new Response( |
||
| 93 | $stream, |
||
| 94 | $this->controllerResponse->getStatusCode(), |
||
| 95 | $this->getResponseHeaders() |
||
| 96 | ); |
||
| 97 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 98 | case Router::REDIRECT: |
||
| 99 | return new RedirectResponse( |
||
| 100 | $this->controllerResponse->getMetaData()['uri'], |
||
| 101 | $this->controllerResponse->getStatusCode(), |
||
| 102 | $this->getResponseHeaders() |
||
| 103 | ); |
||
| 104 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 105 | case Router::CUSTOM: |
||
| 106 | return new HtmlResponse( |
||
| 107 | $this->renderResponse(), |
||
| 108 | $this->controllerResponse->getStatusCode(), |
||
| 109 | $this->getResponseHeaders() |
||
| 110 | ); |
||
| 111 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 112 | case Router::EMPTY: |
||
| 113 | return new EmptyResponse( |
||
| 114 | $this->controllerResponse->getStatusCode(), |
||
| 115 | $this->getResponseHeaders() |
||
| 116 | ); |
||
| 117 | break; |
||
|
0 ignored issues
–
show
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...
|
|||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | private function renderResponse() : string |
||
| 122 | { |
||
| 123 | $paths = explode("\\", $this->controllerClass); |
||
| 124 | $templateFile = array_pop($paths); |
||
| 125 | $templateFolder = array_pop($paths); |
||
| 126 | $template = CaseConverter::toSnakeCase($templateFolder) |
||
| 127 | . '/' . CaseConverter::toSnakeCase($templateFile); |
||
| 128 | $layout = $this->controllerResponse->getMetaData()['layout'] ?? $template; |
||
| 129 | $templatePath = $layout. '.' . $this->config->view->get('template_file_extension'); |
||
| 130 | |||
| 131 | $this->checkTemplateFile($templatePath, 'Method\'s', $this->controllerClass); |
||
| 132 | return $this->view->render($templatePath, $this->controllerResponse->getData()); |
||
| 133 | } |
||
| 134 | |||
| 135 | private function checkTemplateFile($template, $type, $controller) : void |
||
| 136 | { |
||
| 137 | if (!file_exists($this->config->view->get('templates_path') .'/'. $template)) { |
||
| 138 | $message = sprintf( |
||
| 139 | '%s template file not found! %s needs a main template file at: %s', |
||
| 140 | $type, |
||
| 141 | $controller, |
||
| 142 | $this->config['app_dir'] .'/'. $template |
||
| 143 | ); |
||
| 144 | throw new \DomainException($message); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | public function notFound(int $status, int $returnType, string $message) : ResponseInterface |
||
| 149 | { |
||
| 150 | if ($returnType === Router::JSON) { |
||
| 151 | return new JsonResponse(['status' => $status, 'message' => $message], $status); |
||
| 152 | } |
||
| 153 | $notFoundTemplate = '_layouts/404.twig'; |
||
| 154 | $content = $this->view->render( |
||
| 155 | $notFoundTemplate, |
||
| 156 | ['message' => $message, 'status' => $status] |
||
| 157 | ); |
||
| 158 | return new HtmlResponse($content, $status); |
||
| 159 | } |
||
| 160 | } |
||
| 161 |
The break statement is not necessary if it is preceded for example by a return statement:
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.