Files
PHP-App-Timekeeping/api/src/Controller/WorkingHoursViewController.php
T

101 lines
3.0 KiB
PHP

<?php
namespace TorstenHettstedt\TimekeepingApi\Controller;
use Exception;
use Psr\Container\ContainerInterface;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
class WorkingHoursViewController
{
/**
* @var ContainerInterface
*/
protected ContainerInterface $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
/**
* @param Request $request
* @param Response $response
* @param mixed[] $args
*
* @return Response
*
* @noinspection PhpUnusedParameterInspection
* @throws NotDatabasesException
* @throws Exception
*/
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'));
return $this->printResponse($response, $repository->findAll());
}
/**
* @param Request $request
* @param Response $response
* @param mixed[] $args
*
* @return Response
*
* @noinspection PhpUnusedParameterInspection
* @throws Exception
*/
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'));
return $this->printResponse($response, $repository->findAll());
}
/**
* @param Request $request
* @param Response $response
* @param mixed[] $args
*
* @return Response
*
* @noinspection PhpUnusedParameterInspection
* @throws Exception
*/
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'));
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);
}
}