2022-08-12 20:46:43 +02:00
< ? php
2022-08-14 16:49:52 +02:00
namespace php ;
2022-08-12 20:46:43 +02:00
2022-08-14 15:52:47 +02:00
use Monolog\Logger ;
2022-08-12 20:46:43 +02:00
use SQLite3 ;
2022-08-14 12:31:47 +02:00
/**
* Manages interaction with the database in the context of trackings .
*/
2022-08-12 20:46:43 +02:00
class TrackingManager
{
2022-08-14 15:52:47 +02:00
/**
* @ var Logger The logger to use for logging .
*/
private Logger $logger ;
2022-08-12 20:46:43 +02:00
/**
* @ var string The filename of the database to interact with .
*/
private string $db_filename ;
/**
* Constructs a new tracking manager .
*
2022-08-14 15:52:47 +02:00
* @ param Logger $logger the logger to use for logging
2022-08-12 20:46:43 +02:00
* @ param string $db_filename the filename of the database to interact with
*/
2022-08-14 15:52:47 +02:00
public function __construct ( Logger $logger , string $db_filename )
2022-08-12 20:46:43 +02:00
{
2022-08-14 15:52:47 +02:00
$this -> logger = $logger ;
2022-08-12 20:46:43 +02:00
$this -> db_filename = $db_filename ;
}
2022-08-14 12:31:47 +02:00
/**
* 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
*/
2022-08-12 20:46:43 +02:00
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); " );
$stmt -> bindValue ( " :user_uuid " , $user_uuid );
$stmt -> bindValue ( " :person_name " , $person_name );
$inserted = $stmt -> execute () !== false ;
$db -> close ();
return new Response ( null , $inserted );
}
2022-08-14 12:31:47 +02:00
/**
* 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
*/
2022-08-12 20:46:43 +02:00
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; " );
$stmt -> bindValue ( " :user_uuid " , $user_uuid );
$stmt -> bindValue ( " :person_name " , $person_name );
$inserted = $stmt -> execute () !== false ;
$db -> close ();
return new Response ( null , $inserted );
}
2022-08-14 12:31:47 +02:00
/**
* 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
*/
2022-08-12 20:46:43 +02:00
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; " );
$stmt -> bindValue ( " :user_uuid " , $user_uuid );
$results = $stmt -> execute ();
$trackings = [];
while ( $row = $results -> fetchArray ( SQLITE3_ASSOC ))
$trackings [] = $row ;
$db -> close ();
return new Response ( $trackings , true );
}
}