* @implements RepositoryWriterInterface */ class WorkingHoursRepository implements RepositoryReaderInterface, RepositoryWriterInterface { protected PDO $database; /** * 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 WorkingHours[] * @throws Exception * @throws PDOException */ public function findAll(): array { $models = []; $stmt = $this->database->prepare('select "Datum" as "workingDay", "Arbeitszeit" as "workingTime" from public."Arbeitszeiten" order by "Datum"'); $stmt->execute(); while ($row = $stmt->fetch()) { $model = new WorkingHours(); $model ->setWorkingDay(new DateTime($row['workingDay'])) ->setWorkingTime(new DateInterval('P0000-00-00T' . $row['workingTime'])); $models[] = $model; } return $models; } /** * Gibt den Eintrag zurück, der durch die ID * * @param mixed $primary_key * * @return WorkingHours * @throws RepositoryRecordNotFoundException * @throws Exception * @throws PDOException */ public function findByKey(mixed $primary_key): WorkingHours { $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(); if ($row === false) { throw new RepositoryRecordNotFoundException(); } $model = new WorkingHours(); $model ->setWorkingDay(new DateTime($row['workingDay'])) ->setWorkingTime(new DateInterval('P0000-00-00T' . $row['workingTime'])); return $model; } /** * @param WorkingHours|ModelInterface $model * * @throws RepositoryRecordAlreadyExistException * @throws Exception */ public function insert(mixed $model): void { if ($model instanceof WorkingHours) { $workingDay = $model->getWorkingDay()->format('Y-m-d'); $workingTime = $model->getWorkingTime()->format('%H:%I:%S'); } else { throw new InvalidArgumentException('Es wird ein Modell der Klasse "' . WorkingHours::class . '" verlangt.'); } try { $this->findByKey($workingDay); throw new RepositoryRecordAlreadyExistException(); } /** @noinspection PhpUnusedLocalVariableInspection */ catch (RepositoryRecordNotFoundException $exception) { $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(); } } /** * @param WorkingHours|ModelInterface $model * * @throws RepositoryRecordNotFoundException */ public function update(mixed $model): void { if ($model instanceof WorkingHours) { $workingDay = $model->getWorkingDay()->format('Y-m-d'); $workingTime = $model->getWorkingTime()->format('%H:%I:%S'); } else { throw new InvalidArgumentException('Es wird ein Modell der Klasse "' . WorkingHours::class . '" verlangt.'); } $stmt = $this->database->prepare('update public."Arbeitszeiten" set "Arbeitszeit" = ? where "Datum" = ?'); $stmt->bindParam(1, $workingTime); $stmt->bindParam(2, $workingDay); $stmt->execute(); if ($stmt->rowCount() < 1) { throw new RepositoryRecordNotFoundException(); } } /** * @param WorkingHours|ModelInterface $model * * @throws RepositoryRecordNotFoundException */ public function delete(mixed $model): void { if ($model instanceof WorkingHours) { $workingDay = $model->getWorkingDay()->format('Y-m-d'); } else { throw new InvalidArgumentException('Es wird ein Modell der Klasse "' . WorkingHours::class . '" verlangt.'); } $stmt = $this->database->prepare('delete from public."Arbeitszeiten" where "Datum" = ?'); $stmt->bindParam(1, $workingDay); $stmt->execute(); if ($stmt->rowCount() < 1) { throw new RepositoryRecordNotFoundException(); } } }