The formattening, part 1

This commit is contained in:
2025-03-12 00:20:04 +01:00
parent ece3c8ab80
commit b6697e5750
31 changed files with 1420 additions and 1356 deletions
+60 -58
View File
@@ -1,74 +1,76 @@
<?php
declare(strict_types=1);
namespace pvv\side;
use \PDO;
class Door {
private $pdo;
class Door{
private $pdo;
public function __construct(\PDO $pdo) {
$this->pdo = $pdo;
}
public function __construct(PDO $pdo){
$this->pdo = $pdo;
}
public function getAll() {
$query = 'SELECT time, open FROM door ORDER BY time DESC';
$statement = $this->pdo->prepare($query);
$statement->execute();
public function getAll() {
$query = 'SELECT time, open FROM door ORDER BY time DESC';
$statement = $this->pdo->prepare($query);
$statement->execute();
$doorEvents = [];
foreach ($statement->fetchAll() as $row) {
$doorEvents[] = [
'time' => (int) $row['time'],
'open' => (bool) $row['open'],
];
}
$doorEvents = [];
foreach($statement->fetchAll() as $row){
$doorEvents[] = [
'time' => (int)$row['time'],
'open' => (bool)$row['open']
];
}
return $doorEvents;
}
return $doorEvents;
}
public function getEntriesAfter($startTime) {
$query = 'SELECT time, open FROM door WHERE time > :startTime ORDER BY time DESC';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':startTime', $startTime, \PDO::PARAM_STR);
$statement->execute();
public function getEntriesAfter($startTime) {
$query = 'SELECT time, open FROM door WHERE time > :startTime ORDER BY time DESC';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':startTime', $startTime, PDO::PARAM_STR);
$statement->execute();
$doorEvents = [];
foreach ($statement->fetchAll() as $row) {
$doorEvents[] = [
'time' => (int) $row['time'],
'open' => (bool) $row['open'],
];
}
$doorEvents = [];
foreach($statement->fetchAll() as $row){
$doorEvents[] = [
'time' => (int)$row['time'],
'open' => (bool)$row['open']
];
}
return $doorEvents;
}
return $doorEvents;
}
public function getCurrent() {
$query = 'SELECT time, open FROM door ORDER BY time DESC LIMIT 1';
$statement = $this->pdo->prepare($query);
$statement->execute();
$row = $statement->fetch();
public function getCurrent() {
$query = 'SELECT time, open FROM door ORDER BY time DESC LIMIT 1';
$statement = $this->pdo->prepare($query);
$statement->execute();
$row = $statement->fetch();
return [
'time' => (int)$row['time'],
'open' => (bool)$row['open']
];
}
return [
'time' => (int) $row['time'],
'open' => (bool) $row['open'],
];
}
private function removeOld() {
$firstValidTime = time() - 60*60*24*7; //One week before now
$query = 'DELETE FROM door WHERE time < :firstValid';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':firstValid', $firstValidTime, PDO::PARAM_STR);
$statement->execute();
}
private function removeOld(): void {
$firstValidTime = time() - 60 * 60 * 24 * 7; // One week before now
$query = 'DELETE FROM door WHERE time < :firstValid';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':firstValid', $firstValidTime, \PDO::PARAM_STR);
$statement->execute();
}
public function createEvent($time, $open) {
$query = 'INSERT INTO door(time, open) VALUES (:time, :open)';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':time', $time, PDO::PARAM_STR);
$statement->bindParam(':open', $open, PDO::PARAM_STR);
$statement->execute();
public function createEvent($time, $open): void {
$query = 'INSERT INTO door(time, open) VALUES (:time, :open)';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':time', $time, \PDO::PARAM_STR);
$statement->bindParam(':open', $open, \PDO::PARAM_STR);
$statement->execute();
$this->removeOld();
}
$this->removeOld();
}
}