diff --git a/src/pvv/side/door.php b/src/pvv/side/door.php
index 2539fdd..9880cb5 100644
--- a/src/pvv/side/door.php
+++ b/src/pvv/side/door.php
@@ -6,6 +6,28 @@ namespace pvv\side;
use DateTimeImmutable;
+class DoorStatus {
+ private DateTimeImmutable $time;
+ private bool $open;
+
+ public function __construct(DateTimeImmutable $time, bool $open) {
+ $this->time = $time;
+ $this->open = $open;
+ }
+
+ public function getTime(): DateTimeImmutable {
+ return $this->time;
+ }
+
+ public function getTimeStamp(): int {
+ return $this->time->getTimestamp();
+ }
+
+ public function isOpen(): bool {
+ return $this->open;
+ }
+}
+
class Door {
private $pdo;
@@ -16,7 +38,7 @@ class Door {
}
/**
- * @return array{time: DateTimeImmutable, open: bool}[]
+ * @return DoorStatus[]
*/
public function getAll(): array {
$query = '
@@ -29,19 +51,21 @@ class Door {
$statement = $this->pdo->prepare($query);
$statement->execute();
- $doorEvents = [];
- foreach ($statement->fetchAll() as $row) {
- $doorEvents[] = [
- 'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
- 'open' => (bool) $row['open'],
- ];
- }
+ $result = array_map(
+ function ($row) {
+ return new DoorStatus(
+ (new DateTimeImmutable)->setTimestamp((int) $row['time']),
+ (bool) $row['open'],
+ );
+ },
+ $statement->fetchAll(),
+ );
- return $doorEvents;
+ return $result;
}
/**
- * @return array{time: DateTimeImmutable, open: bool}[]
+ * @return DoorStatus[]
*/
public function getEntriesAfter(\DateTimeImmutable $startTime): array {
$timestamp = $startTime->getTimestamp();
@@ -58,21 +82,20 @@ class Door {
$statement->bindParam(':startTime', $timestamp, \PDO::PARAM_INT);
$statement->execute();
- $doorEvents = [];
- foreach ($statement->fetchAll() as $row) {
- $doorEvents[] = [
- 'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
- 'open' => (bool) $row['open'],
- ];
- }
+ $result = array_map(
+ function ($row) {
+ return new DoorStatus(
+ (new DateTimeImmutable)->setTimestamp((int) $row['time']),
+ (bool) $row['open'],
+ );
+ },
+ $statement->fetchAll(),
+ );
- return $doorEvents;
+ return $result;
}
- /**
- * @return ?array{time: DateTimeImmutable, open: bool}
- */
- public function getCurrent(): ?array {
+ public function getCurrent(): ?DoorStatus {
$query = '
SELECT
time,
@@ -89,10 +112,12 @@ class Door {
return null;
}
- return [
- 'time' => (new DateTimeImmutable)->setTimestamp((int) $row['time']),
- 'open' => (bool) $row['open'],
- ];
+ $result = new DoorStatus(
+ (new DateTimeImmutable)->setTimestamp((int) $row['time']),
+ (bool) $row['open'],
+ );
+
+ return $result;
}
private function removeOld(): void {
diff --git a/www/door/index.php b/www/door/index.php
index 16fc742..5bcbe4a 100644
--- a/www/door/index.php
+++ b/www/door/index.php
@@ -50,13 +50,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
echo json_encode([
- 'status' => 'OK',
- 'entries' => array_map(function ($line) {
- return [
- 'time' => $line['time']->getTimestamp(),
- 'open' => $line['open'],
- ];
- }, $lines),
+ 'status' => 'OK',
+ 'entries' => array_map(
+ function ($line) {
+ return [
+ 'time' => $line->getTimestamp(),
+ 'open' => $line->isOpen(),
+ ];
+ },
+ $lines
+ ),
]);
} else {
// Only last entry
@@ -65,11 +68,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo '{"status": "error", "message": "No door data"}';
exit;
}
- $line = (object) $line;
echo json_encode([
- 'status' => 'OK',
- 'time' => $line->time->getTimestamp(),
- 'open' => $line->open,
+ 'status' => 'OK',
+ 'time' => $line->getTimestamp(),
+ 'open' => $line->isOpen(),
]);
}
}
@@ -101,9 +103,9 @@ function getChanges($items) {
$res = [];
foreach ($items as $item) {
- if ($item['open'] !== $prevState) {
+ if ($item->isOpen() !== $prevState) {
$res[] = $item;
- $prevState = $item['open'];
+ $prevState = $item->isOpen();
}
}
diff --git a/www/index.php b/www/index.php
index 95243eb..e224b6e 100644
--- a/www/index.php
+++ b/www/index.php
@@ -1,4 +1,7 @@
getMOTD();
$door = new pvv\side\Door($pdo);
$doorEntry = $door->getCurrent();
-if (!is_null($doorEntry)) {
- $doorEntry = (object) $doorEntry;
-} else {
- $doorEntry = (object) [
- 'time' => new DateTimeImmutable('@0'),
- 'open' => false,
- ];
+if (is_null($doorEntry)) {
+ $doorEntry = new DoorStatus(
+ new DateTimeImmutable('@0'),
+ false,
+ );
}
-if ($doorEntry->time->getTimestamp() < (time() - 60 * 30)) {
+if ($doorEntry->getTimestamp() < (time() - 60 * 30)) {
$doorStateText = 'Ingen data fra dørsensor';
} else {
- if ($doorEntry->open) {
+ if ($doorEntry->isOpen()) {
$doorStateText = 'Døren er åpen';
} else {
$doorStateText = 'Døren er ikke åpen';
}
}
-$doorTime = $doorEntry->time->format('H:i');
+$doorTime = $doorEntry->getTime()->format('H:i');
?>
@@ -64,7 +65,7 @@ $doorTime = $doorEntry->time->format('H:i');
(Oppdatert )