Compare commits

..

7 Commits

Author SHA1 Message Date
2c18f74f97
add/modify template for audit logger
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-04-18 23:15:06 +02:00
831433f9d8
implement audit logger 2023-04-18 23:15:06 +02:00
aa31c857c7
Merge pull request 'fix archive in pipeline' (#10) from bugfix/fix-archive-in-pipeline into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #10
2023-04-18 23:13:59 +02:00
73272fa6e5
fix archive in pipeline
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-04-18 23:13:11 +02:00
0b047b0bcc
Merge pull request 'feature/database-transactions' (#9) from feature/database-transactions into master
All checks were successful
soko-web/pipeline/head This commit looks good
Reviewed-on: #9
2023-04-18 23:07:18 +02:00
ad7b8c3eda
handle errors and exceptions in controllers
All checks were successful
soko-web/pipeline/pr-master This commit looks good
2023-04-18 22:43:21 +02:00
51801d4228
execute every db command in a transaction in controllers 2023-04-18 22:22:02 +02:00
2 changed files with 40 additions and 4 deletions

6
Jenkinsfile vendored
View File

@ -32,7 +32,7 @@ pipeline {
sh 'vendor/bin/phpunit --log-junit unit_test_results.xml --testdox tests'
}
post {
success {
always {
archiveArtifacts 'unit_test_results.xml'
}
}
@ -47,10 +47,10 @@ pipeline {
}
}
steps {
sh 'php vendor/bin/phpstan analyse -c phpstan.neon --error-format=prettyJson > static_code_analysis_results.json'
sh 'php -d memory_limit=1G vendor/bin/phpstan analyse -c phpstan.neon --error-format=prettyJson > static_code_analysis_results.json'
}
post {
success {
always {
archiveArtifacts 'static_code_analysis_results.json'
}
}

View File

@ -1,9 +1,12 @@
<?php namespace SokoWeb\Response;
use ErrorException;
use Exception;
use SokoWeb\Interfaces\Response\IRedirect;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Database\IConnection;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Response\Redirect;
use SokoWeb\Response\HtmlContent;
@ -14,6 +17,8 @@ class HttpResponse
{
private IRequest $request;
private IConnection $dbConnection;
private RouteCollection $routeCollection;
private array $appConfig;
@ -26,12 +31,16 @@ class HttpResponse
public function __construct(
IRequest $request,
IConnection $dbConnection,
RouteCollection $routeCollection,
array $appConfig,
string $requestMethod,
string $requestUrl
) {
set_error_handler([$this, 'exceptionsErrorHandler']);
$this->request = $request;
$this->dbConnection = $dbConnection;
$this->routeCollection = $routeCollection;
$this->appConfig = $appConfig;
$this->method = strtolower($requestMethod);
@ -39,6 +48,11 @@ class HttpResponse
$this->rawUrl = $requestUrl;
}
public function exceptionsErrorHandler($severity, $message, $filename, $lineno)
{
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
public function render(): void
{
$match = $this->routeCollection->match($this->method, $this->parsedUrl['path']);
@ -75,7 +89,16 @@ class HttpResponse
return;
}
$response = call_user_func([$controller, $handler[1]]);
$this->dbConnection->startTransaction();
try {
$response = call_user_func([$controller, $handler[1]]);
} catch (Exception $exception) {
$this->dbConnection->rollback();
$this->render500($exception);
return;
}
$this->dbConnection->commit();
if ($response instanceof IContent) {
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
$response->render();
@ -106,4 +129,17 @@ class HttpResponse
header('Content-Type: text/html; charset=UTF-8', true, 404);
$content->render();
}
private function render500(Exception $exception): void
{
if (empty($_ENV['DEV'])) {
$exceptionToPrint = null;
} else {
$exceptionToPrint = (string)$exception;
}
$content = new HtmlContent($this->appConfig['error500View'], ['exceptionToPrint' => $exceptionToPrint]);
header('Content-Type: text/html; charset=UTF-8', true, 500);
$content->render();
}
}