Alan Lam 4 سال پیش
والد
کامیت
1bbaa611a2
3فایلهای تغییر یافته به همراه34 افزوده شده و 21 حذف شده
  1. 24 21
      controllers/map_overview_ajax.php
  2. 6 0
      engine/constraint.php
  3. 4 0
      engine/test.php

+ 24 - 21
controllers/map_overview_ajax.php

@@ -4,7 +4,7 @@
  * @var string $action
  */
 
-$date_format = "/^2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9]$/";
+$date_format = "/^2\d{3}-[01]\d-[0-3]\d$/";
 
 JSON::header();
 
@@ -28,31 +28,33 @@ if($submit) {
 
 				// Get customer
 				$customer = Customer::get($customer_id);
-				if(!$customer) throw new Exception("Customer not found.");
+				if(!$customer) throw new RuntimeException("Customer not found.");
 
 				// Get locations
 				$locations = $customer->getLocations(["archived" => 0]);
 
 				// Get tests
 				$nvp = [
-					"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
+					"status" => Constraint::notEquals(Test::STATUS_CANCELLED),
 					"location_id" => array_column($locations, "id")
 				];
 
 				if($min_date && $max_date) {
 					if(preg_match($date_format, $min_date) && preg_match($date_format, $max_date)) {
-						$min_dts = "{$min_date}";
-						$max_dts = "{$max_date}";
+						$min_dts = $min_date;
+						$max_dts = $max_date;
 
-						$nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($min_dts, $max_dts);
+						$nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
+					}
+					else {
+						throw new RuntimeException("Invalid dates submitted.");
 					}
 				}
 				else {
-					list($min_dts, $max_dts) = $customer->getYearCycleDates();
-					$nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($min_dts, $max_dts);
+					[$min_dts, $max_dts] = $customer->getYearCycleDates();
+					$nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
 				}
 
-
 				$tests = Test::query($nvp);
 
 				$location_test_array = [];
@@ -65,8 +67,8 @@ if($submit) {
 						/** @var Test $existing */
 						$existing = $location_test_array[$test->location_id];
 
-						$unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
-						$unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC");
+						$unix_start_existing = ($existing->dts_start === null) ? 0 : strtotime($existing->dts_start . " UTC");
+						$unix_start_current = ($test->dts_start === null) ? 0 : strtotime($test->dts_start . " UTC");
 
 						if($unix_start_current > $unix_start_existing) {
 							$location_test_array[$test->location_id] = $test;
@@ -99,20 +101,21 @@ if($submit) {
 
 						$tester = $test->getTester();
 
-						if(!is_null($test->dts_start)) {
+						if($test->dts_start !== null) {
 							$unix_start = strtotime($test->dts_start);
 							$formatted_start_dts = date("d/m/Y H:i", $unix_start);
 						}
 
-						if(is_null($test->pass)) {
+						if($test->pass === null) {
 							$icon_file = "{$customer_slug}_pending.png";
 
-							if(!is_null($test->dts_due)) {
+							$unix_due = null;
+							if($test->dts_due !== null) {
 								$unix_due = strtotime($test->dts_due);
 								$formatted_due_dts = date("d/m/Y H:i", $unix_due);
 							}
 
-							if($tester && $test->dts_due && time() > $unix_due) {
+							if($tester && $unix_due && time() > $unix_due) {
 								$icon_file = "{$customer_slug}_past_due.png";
 								$past_due = true;
 							}
@@ -120,7 +123,7 @@ if($submit) {
 						else if($test->pass) {
 							$icon_file = "{$customer_slug}_pass.png";
 						}
-						else if(!$test->pass) {
+						else {
 							$icon_file = "{$customer_slug}_fail.png";
 						}
 					}
@@ -132,7 +135,7 @@ if($submit) {
 						$icon_file = "closed.png";
 					}
 
-					if($icon_file && file_exists("public/img/markers/$icon_file")) {
+					if(file_exists("public/img/markers/$icon_file")) {
 						$icon = $icon_file;
 					}
 
@@ -183,7 +186,7 @@ if($submit) {
 
 				$project_id = http::value("project_id", "int");
 				$project = Project::get($project_id);
-				if(!$project) throw new Exception("Project not found.");
+				if(!$project) throw new RuntimeException("Project not found.");
 
 				$customer = $project->getCustomer();
 				$tests = $project->getTests();
@@ -193,7 +196,7 @@ if($submit) {
 				$location_tests = [];
 
 				foreach($tests as $test) {
-					if($test->status == Test::STATUS_CANCELLED) continue;
+					if($test->isCancelled()) continue;
 					$location_tests[$test->location_id] = $test;
 				}
 
@@ -251,7 +254,7 @@ if($submit) {
 					];
 				}
 
-				usort($output, function($a, $b) {
+				usort($output, static function(object $a, object $b): int {
 					return $a->status <=> $b->status;
 				});
 
@@ -264,7 +267,7 @@ if($submit) {
 
 
 			default:
-				throw new Exception("Invalid request.");
+				throw new RuntimeException("Invalid request.");
 		}
 
 	}

+ 6 - 0
engine/constraint.php

@@ -42,6 +42,12 @@ class Constraint {
 		return $constraint;
 	}
 
+	public static function notEquals(string $value): Constraint {
+		$constraint = new Constraint("NOT_EQUALS");
+		$constraint->setValues($value);
+		return $constraint;
+	}
+
 	/**
 	 * Constraint constructor.
 	 * @param string $type

+ 4 - 0
engine/test.php

@@ -824,4 +824,8 @@ class Test extends Model {
 		return $this->status_tester_invoice;
 	}
 
+	public function isCancelled(): bool {
+		return $this->getStatus() === self::STATUS_CANCELLED;
+	}
+
 }