Compare commits
7
Commits
8086b86eb2
...
0.5.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31a3f4cc01 | ||
|
|
0a59cb60db | ||
|
|
9430f3c74c | ||
|
|
2b31e17e14 | ||
|
|
9e35ab3b07 | ||
|
|
056bf82c7b | ||
|
|
be54a659c9 |
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,13 +2,10 @@
|
|||||||
|
|
||||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||||
|
|
||||||
|
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Fig\Http\Message\StatusCodeInterface;
|
use Fig\Http\Message\StatusCodeInterface;
|
||||||
use PDO;
|
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use Slim\Exception\HttpBadRequestException;
|
use Slim\Exception\HttpBadRequestException;
|
||||||
use Slim\Exception\HttpInternalServerErrorException;
|
use Slim\Exception\HttpInternalServerErrorException;
|
||||||
use Slim\Exception\HttpNotFoundException;
|
use Slim\Exception\HttpNotFoundException;
|
||||||
@@ -19,26 +16,9 @@ use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistExc
|
|||||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
||||||
|
|
||||||
class WorkingHoursController
|
class WorkingHoursController extends 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 Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
@@ -131,24 +111,6 @@ class WorkingHoursController
|
|||||||
return $this->printResponse($response, $model, StatusCodeInterface::STATUS_CREATED);
|
return $this->printResponse($response, $model, StatusCodeInterface::STATUS_CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Response $response
|
|
||||||
* @param mixed $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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param string $date
|
* @param string $date
|
||||||
|
|||||||
@@ -4,25 +4,16 @@ namespace TorstenHettstedt\TimekeepingApi\Controller;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Psr\Container\ContainerInterface;
|
use Fig\Http\Message\StatusCodeInterface;
|
||||||
|
use Slim\Exception\HttpInternalServerErrorException;
|
||||||
use Slim\Psr7\Request;
|
use Slim\Psr7\Request;
|
||||||
use Slim\Psr7\Response;
|
use Slim\Psr7\Response;
|
||||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
||||||
|
|
||||||
class WorkingHoursViewController
|
class WorkingHoursViewController extends AbstractController
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var ContainerInterface
|
|
||||||
*/
|
|
||||||
protected ContainerInterface $container;
|
|
||||||
|
|
||||||
public function __construct(ContainerInterface $container) {
|
|
||||||
$this->container = $container;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
@@ -30,17 +21,18 @@ class WorkingHoursViewController
|
|||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
*
|
*
|
||||||
|
* @throws HttpInternalServerErrorException
|
||||||
* @noinspection PhpUnusedParameterInspection
|
* @noinspection PhpUnusedParameterInspection
|
||||||
* @throws NotDatabasesException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function browseWeekly(Request $request, Response $response, array $args): Response
|
public function browseWeekly(Request $request, Response $response, array $args): Response
|
||||||
{
|
{
|
||||||
if ($this->container->has('databases') === false) {
|
$repository = new WorkingHoursWeeklyViewRepository($this->databases);
|
||||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
try {
|
||||||
|
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new HttpInternalServerErrorException($request,
|
||||||
|
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||||
}
|
}
|
||||||
$repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases'));
|
|
||||||
return $this->printResponse($response, $repository->findAll());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,16 +42,18 @@ class WorkingHoursViewController
|
|||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
*
|
*
|
||||||
|
* @throws HttpInternalServerErrorException
|
||||||
* @noinspection PhpUnusedParameterInspection
|
* @noinspection PhpUnusedParameterInspection
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function browseMonthly(Request $request, Response $response, array $args): Response
|
public function browseMonthly(Request $request, Response $response, array $args): Response
|
||||||
{
|
{
|
||||||
if ($this->container->has('databases') === false) {
|
$repository = new WorkingHoursMonthlyViewRepository($this->databases);
|
||||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
try {
|
||||||
|
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new HttpInternalServerErrorException($request,
|
||||||
|
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||||
}
|
}
|
||||||
$repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases'));
|
|
||||||
return $this->printResponse($response, $repository->findAll());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,33 +63,18 @@ class WorkingHoursViewController
|
|||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
*
|
*
|
||||||
|
* @throws HttpInternalServerErrorException
|
||||||
* @noinspection PhpUnusedParameterInspection
|
* @noinspection PhpUnusedParameterInspection
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function browseYearly(Request $request, Response $response, array $args): Response
|
public function browseYearly(Request $request, Response $response, array $args): Response
|
||||||
{
|
{
|
||||||
if ($this->container->has('databases') === false) {
|
$repository = new WorkingHoursYearlyViewRepository($this->databases);
|
||||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
try {
|
||||||
|
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw new HttpInternalServerErrorException($request,
|
||||||
|
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||||
}
|
}
|
||||||
$repository = new WorkingHoursYearlyViewRepository($this->container->get('databases'));
|
|
||||||
return $this->printResponse($response, $repository->findAll());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Response $response
|
|
||||||
* @param WorkingHoursView[] $data
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
protected function printResponse( Response $response, array $data): Response
|
|
||||||
{
|
|
||||||
$payload = json_encode($data);
|
|
||||||
|
|
||||||
$response->getBody()->write($payload);
|
|
||||||
|
|
||||||
return $response
|
|
||||||
->withHeader('Content-Type', 'application/json')
|
|
||||||
->withStatus(200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -44,16 +44,16 @@ class WorkingHoursViewControllerTest extends Unit
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @throws NotDatabasesException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function testBrowseMonthlyWithNonDatabase(): void
|
public function testConstructWithNonDatabase(): void
|
||||||
{
|
{
|
||||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||||
'has' => false
|
'has' => false
|
||||||
]);
|
]);
|
||||||
$controller = new WorkingHoursViewController($this->container);
|
|
||||||
$this->expectException(NotDatabasesException::class);
|
$this->expectException(NotDatabasesException::class);
|
||||||
$controller->browseMonthly($this->request, $this->response, []);
|
new WorkingHoursViewController($this->container);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,19 +66,6 @@ class WorkingHoursViewControllerTest extends Unit
|
|||||||
$this->assertInstanceOf(Response::class, $response);
|
$this->assertInstanceOf(Response::class, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function testBrowseYearlyWithNonDatabase(): void
|
|
||||||
{
|
|
||||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
|
||||||
'has' => false
|
|
||||||
]);
|
|
||||||
$controller = new WorkingHoursViewController($this->container);
|
|
||||||
$this->expectException(NotDatabasesException::class);
|
|
||||||
$controller->browseYearly($this->request, $this->response, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -89,19 +76,6 @@ class WorkingHoursViewControllerTest extends Unit
|
|||||||
$this->assertInstanceOf(Response::class, $response);
|
$this->assertInstanceOf(Response::class, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function testBrowseWeeklyWithNonDatabase(): void
|
|
||||||
{
|
|
||||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
|
||||||
'has' => false
|
|
||||||
]);
|
|
||||||
$controller = new WorkingHoursViewController($this->container);
|
|
||||||
$this->expectException(NotDatabasesException::class);
|
|
||||||
$controller->browseWeekly($this->request, $this->response, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -72,9 +72,7 @@ class ErrorHandlerTest extends Unit
|
|||||||
'file' => '/path(to/file',
|
'file' => '/path(to/file',
|
||||||
'getTitle' => Expected::once('The Title'),
|
'getTitle' => Expected::once('The Title'),
|
||||||
]);
|
]);
|
||||||
$this->logger = $this->makeEmpty(LoggerInterface::class, [
|
$this->logger = $this->makeEmpty(LoggerInterface::class, []);
|
||||||
'error' => Expected::once(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$middleWare = new ErrorHandler($this->app);
|
$middleWare = new ErrorHandler($this->app);
|
||||||
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
||||||
@@ -91,9 +89,7 @@ class ErrorHandlerTest extends Unit
|
|||||||
'code' => 400,
|
'code' => 400,
|
||||||
'file' => '/path(to/file',
|
'file' => '/path(to/file',
|
||||||
]);
|
]);
|
||||||
$this->logger = $this->makeEmpty(LoggerInterface::class, [
|
$this->logger = $this->makeEmpty(LoggerInterface::class, []);
|
||||||
'error' => Expected::once(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$middleWare = new ErrorHandler($this->app);
|
$middleWare = new ErrorHandler($this->app);
|
||||||
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use DateTime;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
|
||||||
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
|
||||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
|
||||||
@@ -24,7 +23,6 @@ class WorkingHoursRepositoryTest extends Unit
|
|||||||
const RECORDS_COUNT = 61;
|
const RECORDS_COUNT = 61;
|
||||||
|
|
||||||
protected PDO $pdoObject;
|
protected PDO $pdoObject;
|
||||||
protected PDOStatement $pdoStatement;
|
|
||||||
|
|
||||||
protected function _before(): void
|
protected function _before(): void
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user