Die Gemeinsamkeiten der Controller wurde in eine abstrakte Klasse gepackt.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
use JsonSerializable;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Psr7\Response;
|
||||
|
||||
abstract class AbstractController
|
||||
{
|
||||
|
||||
protected PDO $databases;
|
||||
|
||||
/**
|
||||
* WorkingHoursController constructor.
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @throws NotDatabasesException
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
if ($container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$this->databases = $container->get('databases');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response $response
|
||||
* @param JsonSerializable|JsonSerializable[] $data
|
||||
* @param int $status_code
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function printResponse(Response $response, mixed $data, int $status_code): Response {
|
||||
$payload = json_encode($data);
|
||||
|
||||
$response->getBody()->write($payload);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus($status_code);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user