Browse Source

Cleanup code

Alan Lam 2 years ago
parent
commit
6989af381e
3 changed files with 43 additions and 16 deletions
  1. 17 10
      controllers/test_file.php
  2. 12 6
      controllers/test_pdf.php
  3. 14 0
      engine/model.php

+ 17 - 10
controllers/test_file.php

@@ -1,25 +1,32 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var int $id
+ * @var string $name
+ */
 
 $test = Test::get($id);
-if(!$test) throw new Exception(__("Test not found."));
+if(!$test) {
+	throw new RuntimeException(__("Test not found."));
+}
 
-$is_pdf = $name == "pdf";
+$user = Core::$user;
+$is_pdf = ($name === "pdf");
 
 if($is_pdf) {
 	$project = $test->getProject();
 
-	$customers = Core::$user->getCustomers();
+	$customers = $user->getCustomers();
 	$customer_ids = array_column($customers, "id");
 
-	if(!in_array($project->customer_id, $customer_ids)) throw new Exception(__("You do not have permission to access this resource."));
-}
-
-if(!$is_pdf) {
-	if(!Core::$user->canVerifyTests()) {
-		if(Core::$user->id != $test->tester_id) throw new Exception(__("You do not have permission to access this resource."));
+	if(!in_array($project->customer_id, $customer_ids)) {
+		throw new RuntimeException(__("You do not have permission to access this resource."));
 	}
 }
 
+if(!$is_pdf && !$user->canVerifyTests() && $user->is($test->tester_id)) {
+	throw new RuntimeException(__("You do not have permission to access this resource."));
+}
 
 $directory = $test->getDirectory();
 $path_jpg = $directory . "/" . $name . ".jpg";

+ 12 - 6
controllers/test_pdf.php

@@ -1,11 +1,17 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var string $id
+ */
 
-use \Mpdf\Mpdf;
+use Mpdf\Mpdf;
 
 // Controller may be included directly and $pdf can be accessed.
 
 $test = Test::get($id);
-if(!$test) throw new Exception(__("Test not found."));
+if(!$test) {
+	throw new RuntimeException(__("Test not found."));
+}
 
 $project = $test->getProject();
 $customer = $project->getCustomer();
@@ -36,12 +42,12 @@ foreach($test_values as $value) {
 	$values[$value->name] = $value->value;
 }
 
-$values = array_filter($values, function($value) {
+$values = array_filter($values, static function($value) {
 	return strlen($value) !== 0;
 });
 
-list($date_start, $time_start) = explode(" ", $test->dts_start);
-list($date_end, $time_end) = explode(" ", $test->dts_end);
+[$date_start, $time_start] = explode(" ", $test->dts_start);
+[$date_end, $time_end] = explode(" ", $test->dts_end);
 
 $dt_start = new DateTime($test->dts_start . " UTC");
 $dt_start->setTimezone(new DateTimeZone(TIMEZONE));

+ 14 - 0
engine/model.php

@@ -549,8 +549,22 @@ abstract class Model {
 		return $this->modelData->$id_field ?? null;
 	}
 
+	final public function getIdString(): string {
+		$id = $this->getId();
+		return is_array($id) ? implode("-", $id) : (string) $id;
+	}
+
 	final public function getModelData() {
 		return clone $this->modelData;
 	}
 
+	final public function is($id_or_model): bool {
+		if($id_or_model instanceof self) {
+			$model = $id_or_model;
+			return $model->getIdString() === $this->getIdString();
+		}
+
+		return $this->getIdString() === $id_or_model;
+	}
+
 }