diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-11-10 06:22:37 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-11-10 06:22:37 +0300 |
commit | 7b723d63185632d556242f72a4676472ee32d55f (patch) | |
tree | 98aebcc164a06908dfd8ce6bdb35141275e9dc3e | |
parent | 10a998bc8f73429a2cc72880291c692b55c32c8d (diff) |
Refactor SavedSearches class to use Exceptions
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | libraries/classes/Controllers/Database/QueryByExampleController.php | 49 | ||||
-rw-r--r-- | libraries/classes/Exceptions/SavedSearchesException.php | 11 | ||||
-rw-r--r-- | libraries/classes/SavedSearches.php | 67 |
3 files changed, 67 insertions, 60 deletions
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 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Exceptions; + +use Exception; + +class SavedSearchesException extends Exception +{ +} diff --git a/libraries/classes/SavedSearches.php b/libraries/classes/SavedSearches.php index f6a352ec5e..22823f8396 100644 --- a/libraries/classes/SavedSearches.php +++ b/libraries/classes/SavedSearches.php @@ -8,6 +8,7 @@ declare(strict_types=1); namespace PhpMyAdmin; use PhpMyAdmin\ConfigStorage\Features\SavedQueryByExampleSearchesFeature; +use PhpMyAdmin\Exceptions\SavedSearchesException; use function __; use function count; @@ -229,18 +230,13 @@ class SavedSearches /** * Save the search + * + * @throws SavedSearchesException */ public function save(SavedQueryByExampleSearchesFeature $savedQueryByExampleSearchesFeature): bool { if ($this->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']) |