From a3259403223562702ab2cf906cd3837214b2efac Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 18 Jul 2019 21:19:16 +0100
Subject: [PATCH] Implement new ISensorRepository.

This is thanks to @BNNorman's recent DB changes :D
---
 logic/Repositories/ISensorRepository.php      | 22 +++++
 .../Repositories/MariaDBSensorRepository.php  | 87 +++++++++++++++++++
 2 files changed, 109 insertions(+)
 create mode 100644 logic/Repositories/ISensorRepository.php
 create mode 100644 logic/Repositories/MariaDBSensorRepository.php

diff --git a/logic/Repositories/ISensorRepository.php b/logic/Repositories/ISensorRepository.php
new file mode 100644
index 0000000..b53c2e8
--- /dev/null
+++ b/logic/Repositories/ISensorRepository.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace AirQuality\Repositories;
+
+/**
+ * Defines the interface of repositories that fetch information about sensors.
+ */
+interface ISensorRepository {
+	/**
+	 * Returns an array of all the different types of sensor that devices can 
+	 * have.
+	 * @return	array	An array of sensors.
+	 */
+	public function get_all_sensors();
+	
+	/**
+	 * Returns a list of sensors that a given device posesses.
+	 * @param	int		$device_id	The id of the device to return a list of sensors for.
+	 * @return	array	An array of sensors that the device with the given id has.
+	 */
+	public function get_device_sensors(int $device_id) : array;
+}
diff --git a/logic/Repositories/MariaDBSensorRepository.php b/logic/Repositories/MariaDBSensorRepository.php
new file mode 100644
index 0000000..ee6bbf8
--- /dev/null
+++ b/logic/Repositories/MariaDBSensorRepository.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace AirQuality\Repositories;
+
+use Location\Coordinate;
+use Location\Distance\Vincenty;
+
+
+/**
+ * Fetches device info from a MariaDB database.
+ */
+class MariaDBDeviceRepository implements ISensorRepository {
+	public static $table_name = "sensors";
+	public static $col_id = "id";
+	public static $col_type = "Type";
+	public static $col_descriptioon = "Description";
+	
+	public static $table_name_assoc = "device_sensors";
+	public static $col_assoc_device_id = "device_id";
+	public static $col_assoc_sensor_id = "sensors_id";
+	
+	
+	// ------------------------------------------------------------------------
+	
+	/**
+	 * The database connection.
+	 * @var \AirQuality\Database
+	 */
+	private $database;
+	
+	/** 
+	 * Function that gets a static variable by it's name. Useful in preparing SQL queries.
+	 * @var callable
+	 */
+	private $get_static;
+	/**
+	 * Function that gets a static variable by it's name & class. Useful in preparing SQL queries.
+	 * @var callable
+	 */
+	private $get_static_extra;
+	
+	/**
+	 * Creates a new ISensorRepository implementation that's designed for 
+	 * talking to MariaDB databases.
+	 * @param	\AirQuality\Database	$in_database	The database connection instance to use to talk to the database.
+	 */
+	function __construct(\AirQuality\Database $in_database) {
+		$this->database = $in_database;
+		
+		$this->get_static = function($name) { return self::$$name; };
+		$this->get_static_extra = function($class_name, $name) {
+			return $class_name::$$name;
+		};
+	}
+	
+	
+	public function get_all_sensors() {
+		$s = $this->get_static;
+		
+		return $this->database->query(
+			"SELECT
+				{$s("table_name")}.{$s("col_id")} AS id,
+				{$s("table_name")}.{$s("col_type")} AS type,
+				{$s("table_name")}.{$s("col_description")} AS description,
+			FROM {$s("table_name")};", [
+				
+			]
+		)->fetchAll();
+	}
+	
+	public function get_device_sensors(int $device_id) : array {
+		$s = $this->get_static;
+		
+		return $this->database->query(
+			"SELECT
+				{$s("table_name")}.{$s("col_id")} AS id,
+				{$s("table_name")}.{$s("col_type")} AS type,
+				{$s("table_name")}.{$s("col_description")} AS description,
+			FROM {$s("table_name")}
+			JOIN {$s("table_name_assoc")} ON
+				{$s("table_name_assoc")}.{$s("col_assoc_sensor_id")}
+			WHERE {$s("table_name_assoc")}.{$s("col_assoc_device_id")} = :device_id;", [
+				"device_id" => $device_id
+			]
+		)->fetchAll();
+	}
+}
