Die einzelnen Perioden-Klassen werden erfolgreich getestet.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Repositories;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\PeriodDesignationEnum;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
||||
|
||||
/**
|
||||
* @implements RepositoryReaderInterface<WorkingHoursView>
|
||||
*/
|
||||
abstract class AbstractWorkingHoursViewRepository implements RepositoryReaderInterface
|
||||
{
|
||||
|
||||
protected const SQL_SELECT = '';
|
||||
protected const SQL_WHERE = '';
|
||||
|
||||
protected PDO $database;
|
||||
protected PeriodDesignationEnum $periodDesignation;
|
||||
|
||||
/**
|
||||
* WorkingHoursRepository constructor.
|
||||
*
|
||||
* @param PDO $database
|
||||
*/
|
||||
public function __construct(PDO $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gibt alle vorhandenen Einträge zurück
|
||||
*
|
||||
* @return WorkingHoursView[]
|
||||
* @throws Exception
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function findAll(): array
|
||||
{
|
||||
$models = [];
|
||||
$stmt = $this->database->prepare(static::SQL_SELECT);
|
||||
$stmt->execute();
|
||||
while ($row = $stmt->fetch()) {
|
||||
$model = new WorkingHoursView(
|
||||
$this->buildDateFromPeriod($row['period']),
|
||||
$this->periodDesignation,
|
||||
(int) $row['workingDays'],
|
||||
$this->buildDateInterval($row['totalHours']),
|
||||
$this->buildDateInterval($row['overtime'])
|
||||
);
|
||||
$models[] = $model;
|
||||
}
|
||||
return $models;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Eintrag zurück, der durch die ID
|
||||
*
|
||||
* @param mixed $primary_key
|
||||
*
|
||||
* @return WorkingHoursView
|
||||
* @throws RepositoryRecordNotFoundException
|
||||
* @throws Exception
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function findByKey(mixed $primary_key): WorkingHoursView
|
||||
{
|
||||
$stmt = $this->database->prepare(static::SQL_SELECT . static::SQL_WHERE);
|
||||
$stmt->bindParam(1, $primary_key);
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) {
|
||||
throw new RepositoryRecordNotFoundException();
|
||||
}
|
||||
return new WorkingHoursView(
|
||||
$this->buildDateFromPeriod($row['period']),
|
||||
$this->periodDesignation,
|
||||
(int) $row['workingDays'],
|
||||
$this->buildDateInterval($row['totalHours']),
|
||||
$this->buildDateInterval($row['overtime'])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $time
|
||||
*
|
||||
* @return DateInterval
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function buildDateInterval(string $time): DateInterval
|
||||
{
|
||||
$time_array = explode(':', $time);
|
||||
$hours = (int)$time_array[0];
|
||||
$minutes = (int)$time_array[1];
|
||||
$seconds = (int)$time_array[2];
|
||||
$interval = new DateInterval(sprintf('PT%dH%dM%dS', abs($hours), abs($minutes), abs($seconds)));
|
||||
if ($hours < 0) {
|
||||
$interval->invert = 1;
|
||||
}
|
||||
if ($minutes < 0) {
|
||||
$interval->invert = 1;
|
||||
}
|
||||
if ($seconds < 0) {
|
||||
$interval->invert = 1;
|
||||
}
|
||||
return $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $period
|
||||
*
|
||||
* @return DateTimeInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
abstract protected function buildDateFromPeriod(string $period): DateTimeInterface;
|
||||
}
|
||||
Reference in New Issue
Block a user