From af5526d720d0a9bde50f1f2d92320c226482b434 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 28 Jul 2021 21:09:12 +0000 Subject: [PATCH] [DB] Refactor findOneBy method --- src/Core/DB/DB.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Core/DB/DB.php b/src/Core/DB/DB.php index 5e88c6339e..b85f943c7c 100644 --- a/src/Core/DB/DB.php +++ b/src/Core/DB/DB.php @@ -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"); } }