2 Commits
2 changed files with 17 additions and 39 deletions
@@ -4,6 +4,7 @@ namespace TorstenHettstedt\TimekeepingApi\Controller;
use Exception; use Exception;
use PDO;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Slim\Psr7\Request; use Slim\Psr7\Request;
use Slim\Psr7\Response; use Slim\Psr7\Response;
@@ -14,13 +15,26 @@ use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepositor
class WorkingHoursViewController class WorkingHoursViewController
{ {
protected PDO $databases;
/** /**
* @var ContainerInterface * @var ContainerInterface
*/ */
protected ContainerInterface $container; protected ContainerInterface $container;
/**
* WorkingHoursViewController constructor.
*
* @param ContainerInterface $container
*
* @throws NotDatabasesException
*/
public function __construct(ContainerInterface $container) { public function __construct(ContainerInterface $container) {
$this->container = $container; $this->container = $container;
if ($container->has('databases') === false) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$this->databases = $container->get('databases');
} }
/** /**
@@ -31,14 +45,10 @@ class WorkingHoursViewController
* @return Response * @return Response
* *
* @noinspection PhpUnusedParameterInspection * @noinspection PhpUnusedParameterInspection
* @throws NotDatabasesException
* @throws Exception * @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) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases')); $repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases'));
return $this->printResponse($response, $repository->findAll()); return $this->printResponse($response, $repository->findAll());
} }
@@ -55,9 +65,6 @@ class WorkingHoursViewController
*/ */
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) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases')); $repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases'));
return $this->printResponse($response, $repository->findAll()); return $this->printResponse($response, $repository->findAll());
} }
@@ -74,9 +81,6 @@ class WorkingHoursViewController
*/ */
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) {
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
}
$repository = new WorkingHoursYearlyViewRepository($this->container->get('databases')); $repository = new WorkingHoursYearlyViewRepository($this->container->get('databases'));
return $this->printResponse($response, $repository->findAll()); return $this->printResponse($response, $repository->findAll());
} }
@@ -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
*/ */