death-notifier/src/main/php/TrackingManager.php

128 lines
4.3 KiB
PHP

<?php
namespace php;
use Monolog\Logger;
use SQLite3;
/**
* Manages interaction with the database in the context of trackings.
*/
class TrackingManager
{
/**
* @var Logger The logger to use for logging.
*/
private Logger $logger;
/**
* @var string The filename of the database to interact with.
*/
private string $db_filename;
/**
* Constructs a new tracking manager.
*
* @param Logger $logger the logger to use for logging
* @param string $db_filename the filename of the database to interact with
*/
public function __construct(Logger $logger, string $db_filename)
{
$this->logger = $logger;
$this->db_filename = $db_filename;
}
/**
* Populates the database with the necessary structures for users.
*
* @return void
*/
public function install(): void
{
$db = new SQLite3($this->db_filename, SQLITE3_OPEN_READWRITE);
$db->exec("CREATE TABLE trackings(user_uuid text not null, person_name text not null, is_deceased int not null default 0, PRIMARY KEY (user_uuid, person_name));");
$db->close();
}
/**
* Adds a tracking to the database.
*
* @param string $user_uuid the user to whom the tracking belongs
* @param string $person_name the name of the person to track
* @return Response a response with no message, and a status indicating whether the insertion succeeded
*/
public function add_tracking(string $user_uuid, string $person_name): Response
{
$db = new SQLite3($this->db_filename, SQLITE3_OPEN_READWRITE);
$stmt = $db->prepare("INSERT INTO trackings (user_uuid, person_name) VALUES (:user_uuid, :person_name);");
if ($stmt === false) {
$db->close();
$this->logger->error("Failed to prepare query in 'add_tracking'.");
return new Response("Unexpected database error.", false);
}
$stmt->bindValue(":user_uuid", $user_uuid);
$stmt->bindValue(":person_name", $person_name);
$inserted = $stmt->execute() !== false;
$db->close();
return new Response(null, $inserted);
}
/**
* Removes a tracking from the database.
*
* @param string $user_uuid the user to whom the tracking belongs
* @param string $person_name the name of the tracked person to remove
* @return Response a response with no message, and a status indicating whether the removal succeeded
*/
public function remove_tracking(string $user_uuid, string $person_name): Response
{
$db = new SQLite3($this->db_filename, SQLITE3_OPEN_READWRITE);
$stmt = $db->prepare("DELETE FROM trackings WHERE user_uuid=:user_uuid AND person_name=:person_name;");
if ($stmt === false) {
$db->close();
$this->logger->error("Failed to prepare query in 'remove_tracking'.");
return new Response("Unexpected database error.", false);
}
$stmt->bindValue(":user_uuid", $user_uuid);
$stmt->bindValue(":person_name", $person_name);
$inserted = $stmt->execute() !== false;
$db->close();
return new Response(null, $inserted);
}
/**
* Lists all trackings of the indicated user.
*
* @param string $user_uuid the user to return the trackings of
* @return Response a response with all trackings of the indicated user
*/
public function list_trackings(string $user_uuid): Response
{
$db = new SQLite3($this->db_filename, SQLITE3_OPEN_READONLY);
$stmt = $db->prepare("SELECT * FROM trackings WHERE user_uuid=:user_uuid;");
if ($stmt === false) {
$db->close();
$this->logger->error("Failed to prepare query in 'list_trackings'.");
return new Response("Unexpected database error.", false);
}
$stmt->bindValue(":user_uuid", $user_uuid);
$results = $stmt->execute();
if ($results === false) {
$db->close();
$this->logger->error("Failed to read query results in 'list_trackings'.");
return new Response("Unexpected database error.", false);
}
$trackings = [];
while ($row = $results->fetchArray(SQLITE3_ASSOC))
$trackings[] = $row;
$db->close();
return new Response($trackings, true);
}
}