2019-01-13 13:06:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Actions;
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
use \SBRL\TomlConfig;
|
|
|
|
use \AirQuality\Repositories\IMeasurementDataRepository;
|
|
|
|
use \AirQuality\Repositories\IMeasurementTypeRepository;
|
|
|
|
use \AirQuality\ApiResponseSender;
|
2019-01-13 13:06:32 +00:00
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
use \AirQuality\Validator;
|
|
|
|
use \AirQuality\PerfFormatter;
|
|
|
|
|
|
|
|
class FetchData implements IAction {
|
|
|
|
/** @var TomlConfig */
|
2019-01-13 13:06:32 +00:00
|
|
|
private $settings;
|
2019-01-19 21:12:11 +00:00
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
/** @var IMeasurementDataRepository */
|
|
|
|
private $measurement_repo;
|
|
|
|
/** @var IMeasurementTypeRepository */
|
|
|
|
private $type_repo;
|
|
|
|
|
|
|
|
/** @var ApiResponseSender */
|
|
|
|
private $sender;
|
|
|
|
|
|
|
|
/** @var Validator */
|
2019-01-14 20:41:25 +00:00
|
|
|
private $validator;
|
2019-01-13 13:06:32 +00:00
|
|
|
|
|
|
|
public function __construct(
|
2019-01-15 15:46:24 +00:00
|
|
|
TomlConfig $in_settings,
|
|
|
|
IMeasurementDataRepository $in_measurement_repo,
|
|
|
|
IMeasurementTypeRepository $in_type_repo,
|
|
|
|
ApiResponseSender $in_sender) {
|
2019-01-13 13:06:32 +00:00
|
|
|
$this->settings = $in_settings;
|
2019-01-15 15:46:24 +00:00
|
|
|
$this->measurement_repo = $in_measurement_repo;
|
|
|
|
$this->type_repo = $in_type_repo;
|
|
|
|
$this->sender = $in_sender;
|
|
|
|
|
|
|
|
$this->validator = new Validator($_GET);
|
2019-01-13 13:06:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:02:24 +00:00
|
|
|
public function handle() : bool {
|
2019-01-14 22:18:30 +00:00
|
|
|
global $start_time;
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
$start_handle = microtime(true);
|
|
|
|
|
2019-01-14 22:18:30 +00:00
|
|
|
// 1: Validate params
|
2019-01-14 20:41:25 +00:00
|
|
|
$this->validator->is_datetime("datetime");
|
2019-01-15 15:46:24 +00:00
|
|
|
$this->validator->exists("reading_type");
|
|
|
|
$this->validator->is_max_length("reading_type", 256);
|
2019-01-14 20:41:25 +00:00
|
|
|
$this->validator->run();
|
|
|
|
|
2019-02-18 20:39:35 +00:00
|
|
|
$measurement_type_id = $this->type_repo->get_id($_GET["reading_type"]);
|
|
|
|
|
|
|
|
if($measurement_type_id == null) {
|
2019-01-15 15:46:24 +00:00
|
|
|
$this->sender->send_error_plain(
|
|
|
|
400, "Error: That reading type is invalid.", [
|
|
|
|
[ "x-time-taken", PerfFormatter::format_perf_data($start_time, $start_handle, null) ]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-14 22:18:30 +00:00
|
|
|
// 2: Pull data from database
|
2019-01-15 15:46:24 +00:00
|
|
|
$data = $this->measurement_repo->get_readings_by_date(
|
|
|
|
new \DateTime($_GET["datetime"]),
|
2019-02-18 20:44:37 +00:00
|
|
|
$measurement_type_id
|
2019-01-15 15:46:24 +00:00
|
|
|
);
|
2019-01-14 22:18:30 +00:00
|
|
|
|
|
|
|
// 2.5: Validate data from database
|
2019-01-15 15:46:24 +00:00
|
|
|
if(empty($data)) {
|
|
|
|
http_response_code(404);
|
|
|
|
header("content-type: text/plain");
|
|
|
|
header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, null));
|
|
|
|
echo("Error: No data could be found for that timestamp.");
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-14 22:18:30 +00:00
|
|
|
|
|
|
|
// 3: Serialise data
|
2019-01-15 15:46:24 +00:00
|
|
|
$start_encode = microtime(true);
|
|
|
|
$response = json_encode($data);
|
2019-01-14 22:18:30 +00:00
|
|
|
|
|
|
|
// 4: Send response
|
2019-01-15 23:03:53 +00:00
|
|
|
|
2019-01-17 14:51:19 +00:00
|
|
|
// Send a cache-control header, but only in production mode
|
|
|
|
if($this->settings->get("env.mode") == "production") {
|
|
|
|
header("cache-control: public, max-age=" . $this->settings->get("cache.max-age"));
|
|
|
|
}
|
|
|
|
|
2019-01-17 15:33:10 +00:00
|
|
|
header("content-length: " . strlen($response));
|
2019-01-14 22:18:30 +00:00
|
|
|
header("content-type: application/json");
|
2019-01-15 15:46:24 +00:00
|
|
|
header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, $start_encode));
|
2019-01-14 22:18:30 +00:00
|
|
|
echo($response);
|
|
|
|
return true;
|
2019-01-13 13:06:32 +00:00
|
|
|
}
|
|
|
|
}
|