Refactor Classes

This commit is contained in:
2025-05-03 09:29:03 +02:00
parent 41a3c79c91
commit 6fa6b28af6
17 changed files with 125 additions and 151 deletions
@@ -18,9 +18,8 @@ abstract class AbstractWorkingHoursViewRepository implements RepositoryReaderInt
{
protected const SQL_SELECT = '';
protected const SQL_WHERE = '';
protected const SQL_WHERE = '';
protected PDO $database;
protected PeriodDesignationEnum $periodDesignation;
/**
@@ -28,9 +27,8 @@ abstract class AbstractWorkingHoursViewRepository implements RepositoryReaderInt
*
* @param PDO $database
*/
public function __construct(PDO $database)
public function __construct(protected PDO $database)
{
$this->database = $database;
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
@@ -51,7 +49,7 @@ abstract class AbstractWorkingHoursViewRepository implements RepositoryReaderInt
$model = new WorkingHoursView(
$this->buildDateFromPeriod($row['period']),
$this->periodDesignation,
(int) $row['workingDays'],
(int)$row['workingDays'],
$this->buildDateInterval($row['totalHours']),
$this->buildDateInterval($row['overtime'])
);
@@ -82,7 +80,7 @@ abstract class AbstractWorkingHoursViewRepository implements RepositoryReaderInt
return new WorkingHoursView(
$this->buildDateFromPeriod($row['period']),
$this->periodDesignation,
(int) $row['workingDays'],
(int)$row['workingDays'],
$this->buildDateInterval($row['totalHours']),
$this->buildDateInterval($row['overtime'])
);
@@ -3,8 +3,10 @@
namespace TorstenHettstedt\TimekeepingApi\Repositories;
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
/**
* @template T of \TorstenHettstedt\TimekeepingApi\Models\ModelInterface
* @template T of ModelInterface
*/
interface RepositoryReaderInterface
{
@@ -3,8 +3,10 @@
namespace TorstenHettstedt\TimekeepingApi\Repositories;
use TorstenHettstedt\TimekeepingApi\Models\ModelInterface;
/**
* @template T of \TorstenHettstedt\TimekeepingApi\Models\ModelInterface
* @template T of ModelInterface
*/
interface RepositoryWriterInterface
{
@@ -13,22 +13,19 @@ class WorkingHoursMonthlyViewRepository extends AbstractWorkingHoursViewReposito
{
protected const SQL_SELECT = <<<SQL
select
select
"Monat" as "period",
"Gesamtarbeitszeit" as "totalHours",
"Arbeitstage" as "workingDays",
"Überstunden" as "overtime"
from "Arbeitszeiten - Monat"
SQL;
protected const SQL_WHERE = ' where "Monat" = ?';
protected PDO $database;
protected PeriodDesignationEnum $periodDesignation;
protected const SQL_WHERE = ' where "Monat" = ?';
public function __construct(PDO $database)
{
parent::__construct($database);
$this->periodDesignation = PeriodDesignationEnum::MONTHLY();
$this->periodDesignation = PeriodDesignationEnum::MONTHLY;
}
protected function buildDateFromPeriod(string $period): DateTimeInterface
@@ -21,16 +21,8 @@ use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWriterInterface
{
protected PDO $database;
/**
* WorkingHoursRepository constructor.
*
* @param PDO $database
*/
public function __construct(PDO $database)
public function __construct(protected PDO $database)
{
$this->database = $database;
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
@@ -106,7 +98,7 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
if (is_string($end)) {
$stmt->bindParam(':end', $end);
}
return $stmt ?? null;
return $stmt ?: null;
}
/**
@@ -121,7 +113,9 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
*/
public function findByKey(mixed $primary_key): WorkingHours
{
$stmt = $this->database->prepare('select "Datum" as "workingDay", "Arbeitszeit" as "workingTime" from public."Arbeitszeiten" where "Datum" = ?');
$stmt = $this->database->prepare(
'select "Datum" as "workingDay", "Arbeitszeit" as "workingTime" from public."Arbeitszeiten" where "Datum" = ?'
);
$stmt->bindParam(1, $primary_key);
$stmt->execute();
$row = $stmt->fetch();
@@ -155,7 +149,9 @@ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWri
throw new RepositoryRecordAlreadyExistException();
} /** @noinspection PhpUnusedLocalVariableInspection */
catch (RepositoryRecordNotFoundException $exception) {
$stmt = $this->database->prepare('insert into public."Arbeitszeiten" ("Datum", "Arbeitszeit") values (?, ?) on conflict do nothing');
$stmt = $this->database->prepare(
'insert into public."Arbeitszeiten" ("Datum", "Arbeitszeit") values (?, ?) on conflict do nothing'
);
$stmt->bindParam(1, $workingDay);
$stmt->bindParam(2, $workingTime);
$stmt->execute();
@@ -12,22 +12,19 @@ class WorkingHoursWeeklyViewRepository extends AbstractWorkingHoursViewRepositor
{
protected const SQL_SELECT = <<<SQL
select
select
"Woche" as "period",
"Gesamtarbeitszeit" as "totalHours",
"Arbeitstage" as "workingDays",
"Überstunden" as "overtime"
from "Arbeitszeiten - Woche"
SQL;
protected const SQL_WHERE = ' where "Woche" = ?';
protected PDO $database;
protected PeriodDesignationEnum $periodDesignation;
protected const SQL_WHERE = ' where "Woche" = ?';
public function __construct(PDO $database)
{
parent::__construct($database);
$this->periodDesignation = PeriodDesignationEnum::WEEKLY();
$this->periodDesignation = PeriodDesignationEnum::WEEKLY;
}
protected function buildDateFromPeriod(string $period): DateTimeInterface
@@ -12,22 +12,19 @@ class WorkingHoursYearlyViewRepository extends AbstractWorkingHoursViewRepositor
{
protected const SQL_SELECT = <<<SQL
select
select
"Jahr" as "period",
"Gesamtarbeitszeit" as "totalHours",
"Arbeitstage" as "workingDays",
"Überstunden" as "overtime"
from "Arbeitszeiten - Jahr"
SQL;
protected const SQL_WHERE = ' where "Jahr" = ?';
protected PDO $database;
protected PeriodDesignationEnum $periodDesignation;
protected const SQL_WHERE = ' where "Jahr" = ?';
public function __construct(PDO $database)
{
parent::__construct($database);
$this->periodDesignation = PeriodDesignationEnum::YEARLY();
$this->periodDesignation = PeriodDesignationEnum::YEARLY;
}
/**