From c2676abfa21459a10ff1cf5fa94dd1e008362780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20L=C3=BCcke?= Date: Fri, 2 Apr 2021 21:19:15 +0200 Subject: [PATCH] Die einzelnen Perioden-Klassen werden erfolgreich getestet. --- .../AbstractWorkingHoursViewRepository.php | 123 ++++++++++++++++++ .../WorkingHoursMonthlyViewRepository.php | 38 ++++++ .../WorkingHoursWeeklyViewRepository.php | 42 ++++++ .../WorkingHoursYearlyViewRepository.php | 43 ++++++ 4 files changed, 246 insertions(+) create mode 100644 api/src/Repositories/AbstractWorkingHoursViewRepository.php create mode 100644 api/src/Repositories/WorkingHoursMonthlyViewRepository.php create mode 100644 api/src/Repositories/WorkingHoursWeeklyViewRepository.php create mode 100644 api/src/Repositories/WorkingHoursYearlyViewRepository.php diff --git a/api/src/Repositories/AbstractWorkingHoursViewRepository.php b/api/src/Repositories/AbstractWorkingHoursViewRepository.php new file mode 100644 index 0000000..908822a --- /dev/null +++ b/api/src/Repositories/AbstractWorkingHoursViewRepository.php @@ -0,0 +1,123 @@ + + */ +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; +} \ No newline at end of file diff --git a/api/src/Repositories/WorkingHoursMonthlyViewRepository.php b/api/src/Repositories/WorkingHoursMonthlyViewRepository.php new file mode 100644 index 0000000..eb73c7e --- /dev/null +++ b/api/src/Repositories/WorkingHoursMonthlyViewRepository.php @@ -0,0 +1,38 @@ +periodDesignation = PeriodDesignationEnum::MONTHLY(); + } + + protected function buildDateFromPeriod(string $period): DateTimeInterface + { + return new DateTime($period . '-01'); + } +} \ No newline at end of file diff --git a/api/src/Repositories/WorkingHoursWeeklyViewRepository.php b/api/src/Repositories/WorkingHoursWeeklyViewRepository.php new file mode 100644 index 0000000..05fec39 --- /dev/null +++ b/api/src/Repositories/WorkingHoursWeeklyViewRepository.php @@ -0,0 +1,42 @@ +periodDesignation = PeriodDesignationEnum::WEEKLY(); + } + + protected function buildDateFromPeriod(string $period): DateTimeInterface + { + $time_array = explode('#', $period); + $year = (int)$time_array[0]; + $week = (int)$time_array[1]; + $date = new DateTime($year. '-01-01'); + $date->add(new DateInterval(sprintf('P%dW', $week))); + return $date; + } +} \ No newline at end of file diff --git a/api/src/Repositories/WorkingHoursYearlyViewRepository.php b/api/src/Repositories/WorkingHoursYearlyViewRepository.php new file mode 100644 index 0000000..1efb106 --- /dev/null +++ b/api/src/Repositories/WorkingHoursYearlyViewRepository.php @@ -0,0 +1,43 @@ +periodDesignation = PeriodDesignationEnum::YEARLY(); + } + + /** + * @param string $period + * + * @return DateTimeInterface + * @throws Exception + */ + protected function buildDateFromPeriod(string $period): DateTimeInterface + { + return new DateTime($period . '-01-01'); + } +} \ No newline at end of file