[DB] Refactor findOneBy method

This commit is contained in:
Hugo Sales 2021-07-28 21:09:12 +00:00
parent 8e743eabb9
commit 55710aa33d
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -176,14 +176,13 @@ abstract class DB
public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null)
{
$res = self::findBy($table, $criteria, $orderBy, 2, $offset);
if (count($res) == 1) {
switch (count($res)) {
case 0:
throw new NotFoundException("No value in table {$table} matches the requested criteria");
case 1:
return $res[0];
} else {
if (count($res) == 0) {
throw new NotFoundException("No value in table {$table} matches the requested criteria");
} else {
throw new DuplicateFoundException("Multiple values in table {$table} match the requested criteria");
}
default:
throw new DuplicateFoundException("Multiple values in table {$table} match the requested criteria");
}
}