From 7b723d63185632d556242f72a4676472ee32d55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Thu, 10 Nov 2022 00:22:37 -0300 Subject: Refactor SavedSearches class to use Exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Database/QueryByExampleController.php | 49 +++++++++++++--- .../classes/Exceptions/SavedSearchesException.php | 11 ++++ libraries/classes/SavedSearches.php | 67 +++++----------------- 3 files changed, 67 insertions(+), 60 deletions(-) create mode 100644 libraries/classes/Exceptions/SavedSearchesException.php diff --git a/libraries/classes/Controllers/Database/QueryByExampleController.php b/libraries/classes/Controllers/Database/QueryByExampleController.php index 06019397c6..7f0ca927ae 100644 --- a/libraries/classes/Controllers/Database/QueryByExampleController.php +++ b/libraries/classes/Controllers/Database/QueryByExampleController.php @@ -9,7 +9,9 @@ use PhpMyAdmin\ConfigStorage\RelationCleanup; use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\Database\Qbe; use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\Exceptions\SavedSearchesException; use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Message; use PhpMyAdmin\Operations; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\SavedSearches; @@ -75,14 +77,39 @@ class QueryByExampleController extends AbstractController if (isset($_POST['action'])) { $GLOBALS['savedSearch']->setSearchName($_POST['searchName']); if ($_POST['action'] === 'create') { - $GLOBALS['savedSearch']->setId(null) - ->setCriterias($_POST) - ->save($savedQbeSearchesFeature); + try { + $GLOBALS['savedSearch']->setId(null) + ->setCriterias($_POST) + ->save($savedQbeSearchesFeature); + } catch (SavedSearchesException $exception) { + $this->response->setRequestStatus(false); + $this->response->addJSON('fieldWithError', 'searchName'); + $this->response->addJSON('message', Message::error($exception->getMessage())->getDisplay()); + + return; + } } elseif ($_POST['action'] === 'update') { - $GLOBALS['savedSearch']->setCriterias($_POST) - ->save($savedQbeSearchesFeature); + try { + $GLOBALS['savedSearch']->setCriterias($_POST) + ->save($savedQbeSearchesFeature); + } catch (SavedSearchesException $exception) { + $this->response->setRequestStatus(false); + $this->response->addJSON('fieldWithError', 'searchName'); + $this->response->addJSON('message', Message::error($exception->getMessage())->getDisplay()); + + return; + } } elseif ($_POST['action'] === 'delete') { - $GLOBALS['savedSearch']->delete($savedQbeSearchesFeature); + try { + $GLOBALS['savedSearch']->delete($savedQbeSearchesFeature); + } catch (SavedSearchesException $exception) { + $this->response->setRequestStatus(false); + $this->response->addJSON('fieldWithError', 'searchId'); + $this->response->addJSON('message', Message::error($exception->getMessage())->getDisplay()); + + return; + } + //After deletion, reset search. $GLOBALS['savedSearch'] = new SavedSearches(); $GLOBALS['savedSearch']->setUsername($GLOBALS['cfg']['Server']['user']) @@ -96,7 +123,15 @@ class QueryByExampleController extends AbstractController ->setDbname($GLOBALS['db']); $_POST = []; } else { - $GLOBALS['savedSearch']->load($savedQbeSearchesFeature); + try { + $GLOBALS['savedSearch']->load($savedQbeSearchesFeature); + } catch (SavedSearchesException $exception) { + $this->response->setRequestStatus(false); + $this->response->addJSON('fieldWithError', 'searchId'); + $this->response->addJSON('message', Message::error($exception->getMessage())->getDisplay()); + + return; + } } } //Else, it's an "update query" diff --git a/libraries/classes/Exceptions/SavedSearchesException.php b/libraries/classes/Exceptions/SavedSearchesException.php new file mode 100644 index 0000000000..6331fad670 --- /dev/null +++ b/libraries/classes/Exceptions/SavedSearchesException.php @@ -0,0 +1,11 @@ +getSearchName() == null) { - $message = Message::error( - __('Please provide a name for this bookmarked search.') - ); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('fieldWithError', 'searchName'); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('Please provide a name for this bookmarked search.')); } if ( @@ -249,13 +245,7 @@ class SavedSearches || $this->getSearchName() == null || $this->getCriterias() == null ) { - $message = Message::error( - __('Missing information to save the bookmarked search.') - ); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('Missing information to save the bookmarked search.')); } $savedSearchesTbl = Util::backquote($savedQueryByExampleSearchesFeature->database) . '.' @@ -269,14 +259,7 @@ class SavedSearches $existingSearches = $this->getList($savedQueryByExampleSearchesFeature, $wheres); if (! empty($existingSearches)) { - $message = Message::error( - __('An entry with this name already exists.') - ); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('fieldWithError', 'searchName'); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('An entry with this name already exists.')); } $sqlQuery = 'INSERT INTO ' . $savedSearchesTbl @@ -303,14 +286,7 @@ class SavedSearches $existingSearches = $this->getList($savedQueryByExampleSearchesFeature, $wheres); if (! empty($existingSearches)) { - $message = Message::error( - __('An entry with this name already exists.') - ); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('fieldWithError', 'searchName'); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('An entry with this name already exists.')); } $sqlQuery = 'UPDATE ' . $savedSearchesTbl @@ -325,18 +301,13 @@ class SavedSearches /** * Delete the search + * + * @throws SavedSearchesException */ public function delete(SavedQueryByExampleSearchesFeature $savedQueryByExampleSearchesFeature): bool { if ($this->getId() == null) { - $message = Message::error( - __('Missing information to delete the search.') - ); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('fieldWithError', 'searchId'); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('Missing information to delete the search.')); } $savedSearchesTbl = Util::backquote($savedQueryByExampleSearchesFeature->database) . '.' @@ -350,18 +321,13 @@ class SavedSearches /** * Load the current search from an id. + * + * @throws SavedSearchesException */ public function load(SavedQueryByExampleSearchesFeature $savedQueryByExampleSearchesFeature): bool { if ($this->getId() == null) { - $message = Message::error( - __('Missing information to load the search.') - ); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('fieldWithError', 'searchId'); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('Missing information to load the search.')); } $savedSearchesTbl = Util::backquote($savedQueryByExampleSearchesFeature->database) @@ -375,12 +341,7 @@ class SavedSearches $oneResult = $resList->fetchAssoc(); if ($oneResult === []) { - $message = Message::error(__('Error while loading the search.')); - $response = ResponseRenderer::getInstance(); - $response->setRequestStatus($message->isSuccess()); - $response->addJSON('fieldWithError', 'searchId'); - $response->addJSON('message', $message); - exit; + throw new SavedSearchesException(__('Error while loading the search.')); } $this->setSearchName($oneResult['search_name']) -- cgit v1.2.3