Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
require_once __DIR__ . "/lib/exceptions/EnsureException.php";
require_once __DIR__ . "/lib/exceptions/EncodingUnknownException.php";
require_once __DIR__ . "/lib/exceptions/EncodingConversionException.php";
require_once __DIR__ . "/lib/exceptions/RecordNotFoundException.php";
require_once __DIR__ . "/lib/exceptions/RecordNotUniqueException.php";
require_once __DIR__ . "/lib/exceptions/UnityHTTPDMessageNotFoundException.php";

require_once __DIR__ . "/config.php";
Expand Down
27 changes: 19 additions & 8 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace UnityWebPortal\lib;

use PDO;
use UnityWebPortal\lib\exceptions\RecordNotUniqueException;
use UnityWebPortal\lib\exceptions\RecordNotFoundException;

class UnitySQL
{
Expand Down Expand Up @@ -31,6 +33,16 @@ public function getConn(): PDO
return $this->conn;
}

private function throwIfEmptyOrNotUnique(array $x, string $description): void
{
if (count($x) === 0) {
throw new RecordNotFoundException($description);
}
if (count($x) > 1) {
throw new RecordNotUniqueException($description);
}
}

//
// requests table methods
//
Expand Down Expand Up @@ -80,12 +92,7 @@ public function getRequest(string $user, string $dest): array
$stmt->bindParam(":request_for", $dest);
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) == 0) {
throw new \Exception("no such request: uid='$user' request_for='$dest'");
}
if (count($result) > 1) {
throw new \Exception("multiple requests for uid='$user' request_for='$dest'");
}
$this->throwIfEmptyOrNotUnique($result, "uid='$user' request_for='$dest'");
return $result[0];
}

Expand Down Expand Up @@ -170,7 +177,9 @@ public function getNotice(string $id): array
$stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_NOTICES . " WHERE id=:id");
$stmt->bindParam(":id", $id);
$stmt->execute();
return $stmt->fetchAll()[0];
$output = $stmt->fetchAll();
$this->throwIfEmptyOrNotUnique($output, "id='$id'");
return $output[0];
}

public function getNotices(): array
Expand All @@ -194,7 +203,9 @@ public function getPage(string $id): array
$stmt = $this->conn->prepare("SELECT * FROM " . self::TABLE_PAGES . " WHERE page=:id");
$stmt->bindParam(":id", $id);
$stmt->execute();
return $stmt->fetchAll()[0];
$output = $stmt->fetchAll();
$this->throwIfEmptyOrNotUnique($output, "id='$id'");
return $output[0];
}

public function editPage(string $id, string $content): void
Expand Down
5 changes: 5 additions & 0 deletions resources/lib/exceptions/RecordNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace UnityWebPortal\lib\exceptions;

class RecordNotFoundException extends \Exception {}
5 changes: 5 additions & 0 deletions resources/lib/exceptions/RecordNotUniqueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace UnityWebPortal\lib\exceptions;

class RecordNotUniqueException extends \Exception {}
2 changes: 2 additions & 0 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
require_once __DIR__ . "/../resources/lib/exceptions/EnsureException.php";
require_once __DIR__ . "/../resources/lib/exceptions/EncodingUnknownException.php";
require_once __DIR__ . "/../resources/lib/exceptions/EncodingConversionException.php";
require_once __DIR__ . "/../resources/lib/exceptions/RecordNotFoundException.php";
require_once __DIR__ . "/../resources/lib/exceptions/RecordNotUnique.php";
require_once __DIR__ . "/../resources/lib/exceptions/UnityHTTPDMessageNotFoundException.php";

use PHPStan\DependencyInjection\ValidateExcludePathsExtension;
Expand Down
Loading