diff --git a/app/modules/api/Init.php b/app/modules/api/Init.php index fdd20a01..ea5643ac 100644 --- a/app/modules/api/Init.php +++ b/app/modules/api/Init.php @@ -33,8 +33,8 @@ use SP\Core\ModuleBase; use SP\Services\Upgrade\UpgradeAppService; use SP\Services\Upgrade\UpgradeDatabaseService; use SP\Services\Upgrade\UpgradeUtil; -use SP\Storage\Database; -use SP\Storage\DBUtil; +use SP\Storage\Database\Database; +use SP\Storage\Database\DBUtil; use SP\Util\HttpUtil; /** diff --git a/app/modules/web/Controllers/AccessManagerController.php b/app/modules/web/Controllers/AccessManagerController.php index d9d1dfb3..6234f9e2 100644 --- a/app/modules/web/Controllers/AccessManagerController.php +++ b/app/modules/web/Controllers/AccessManagerController.php @@ -148,8 +148,8 @@ class AccessManagerController extends ControllerBase * Returns API tokens data tab * * @return \SP\Html\DataGrid\DataGridTab - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getApiTokensList() { diff --git a/app/modules/web/Controllers/AccountFileController.php b/app/modules/web/Controllers/AccountFileController.php index 18a0ce6f..b94a044a 100644 --- a/app/modules/web/Controllers/AccountFileController.php +++ b/app/modules/web/Controllers/AccountFileController.php @@ -227,8 +227,8 @@ class AccountFileController extends ControllerBase implements CrudControllerInte /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -247,6 +247,8 @@ class AccountFileController extends ControllerBase implements CrudControllerInte * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { diff --git a/app/modules/web/Controllers/AccountHistoryManagerController.php b/app/modules/web/Controllers/AccountHistoryManagerController.php index f14d7731..1c0de71e 100644 --- a/app/modules/web/Controllers/AccountHistoryManagerController.php +++ b/app/modules/web/Controllers/AccountHistoryManagerController.php @@ -67,6 +67,8 @@ class AccountHistoryManagerController extends ControllerBase * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { diff --git a/app/modules/web/Controllers/AccountManagerController.php b/app/modules/web/Controllers/AccountManagerController.php index 84b1de6e..df6ae91f 100644 --- a/app/modules/web/Controllers/AccountManagerController.php +++ b/app/modules/web/Controllers/AccountManagerController.php @@ -67,6 +67,8 @@ class AccountManagerController extends ControllerBase * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { diff --git a/app/modules/web/Controllers/AuthTokenController.php b/app/modules/web/Controllers/AuthTokenController.php index 227b9c28..c08f5e7f 100644 --- a/app/modules/web/Controllers/AuthTokenController.php +++ b/app/modules/web/Controllers/AuthTokenController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -58,8 +58,8 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -78,6 +78,8 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -117,10 +119,12 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf } /** - * Sets view data for displaying user's data + * Sets view data for displaying auth token's data * * @param $authTokenId - * @throws \Psr\Container\ContainerExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($authTokenId = null) { @@ -151,6 +155,7 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -180,6 +185,7 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -258,6 +264,7 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf * Saves edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -307,6 +314,7 @@ class AuthTokenController extends ControllerBase implements CrudControllerInterf * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/CategoryController.php b/app/modules/web/Controllers/CategoryController.php index ee6fc215..69060da7 100644 --- a/app/modules/web/Controllers/CategoryController.php +++ b/app/modules/web/Controllers/CategoryController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -56,8 +56,8 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -76,6 +76,8 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -115,10 +117,12 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa } /** - * Sets view data for displaying user's data + * Sets view data for displaying category's data * * @param $categoryId - * @throws \Psr\Container\ContainerExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($categoryId = null) { @@ -146,6 +150,7 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -175,6 +180,7 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -261,6 +267,7 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa * Saves edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface * @@ -302,6 +309,7 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/ClientController.php b/app/modules/web/Controllers/ClientController.php index e3ae1808..95b11ea1 100644 --- a/app/modules/web/Controllers/ClientController.php +++ b/app/modules/web/Controllers/ClientController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -57,8 +57,8 @@ class ClientController extends ControllerBase implements CrudControllerInterface /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -77,6 +77,8 @@ class ClientController extends ControllerBase implements CrudControllerInterface * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -116,10 +118,12 @@ class ClientController extends ControllerBase implements CrudControllerInterface } /** - * Sets view data for displaying user's data + * Sets view data for displaying client's data * * @param $clientId - * @throws \Psr\Container\ContainerExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($clientId = null) { @@ -147,6 +151,7 @@ class ClientController extends ControllerBase implements CrudControllerInterface * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -176,6 +181,7 @@ class ClientController extends ControllerBase implements CrudControllerInterface * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -289,6 +295,7 @@ class ClientController extends ControllerBase implements CrudControllerInterface * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/ConfigManagerController.php b/app/modules/web/Controllers/ConfigManagerController.php index b9efb526..51f617ee 100644 --- a/app/modules/web/Controllers/ConfigManagerController.php +++ b/app/modules/web/Controllers/ConfigManagerController.php @@ -45,8 +45,8 @@ use SP\Services\Task\Task; use SP\Services\User\UserService; use SP\Services\UserGroup\UserGroupService; use SP\Services\UserProfile\UserProfileService; -use SP\Storage\DBUtil; -use SP\Storage\MySQLHandler; +use SP\Storage\Database\DBUtil; +use SP\Storage\Database\MySQLHandler; use SP\Util\Checks; use SP\Util\Util; diff --git a/app/modules/web/Controllers/CustomFieldController.php b/app/modules/web/Controllers/CustomFieldController.php index 684ffaf1..302b74e0 100644 --- a/app/modules/web/Controllers/CustomFieldController.php +++ b/app/modules/web/Controllers/CustomFieldController.php @@ -58,8 +58,8 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -78,6 +78,8 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -117,10 +119,12 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte } /** - * Sets view data for displaying user's data + * Sets view data for displaying custom field's data * * @param $customFieldId - * @throws \Psr\Container\ContainerExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($customFieldId = null) { @@ -148,6 +152,7 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -177,6 +182,7 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -281,6 +287,7 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/EventlogController.php b/app/modules/web/Controllers/EventlogController.php index da72182b..7ebf1795 100644 --- a/app/modules/web/Controllers/EventlogController.php +++ b/app/modules/web/Controllers/EventlogController.php @@ -49,6 +49,9 @@ class EventlogController extends ControllerBase /** * indexAction + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function indexAction() { @@ -67,6 +70,8 @@ class EventlogController extends ControllerBase * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -78,6 +83,9 @@ class EventlogController extends ControllerBase /** * searchAction + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { diff --git a/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php index c23744de..9750d016 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountSearchHelper.php @@ -84,6 +84,7 @@ class AccountSearchHelper extends HelperBase * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\QueryException */ public function getSearchBox() { diff --git a/app/modules/web/Controllers/Helpers/ItemsGridHelper.php b/app/modules/web/Controllers/Helpers/ItemsGridHelper.php index e1d9626a..f4078630 100644 --- a/app/modules/web/Controllers/Helpers/ItemsGridHelper.php +++ b/app/modules/web/Controllers/Helpers/ItemsGridHelper.php @@ -41,6 +41,7 @@ use SP\Html\DataGrid\DataGridInterface; use SP\Html\DataGrid\DataGridPager; use SP\Html\DataGrid\DataGridTab; use SP\Services\CustomField\CustomFieldDefService; +use SP\Storage\Database\QueryResult; use SP\Util\DateUtil; /** @@ -61,10 +62,11 @@ class ItemsGridHelper extends HelperBase protected $acl; /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getCategoriesGrid(array $data) + public function getCategoriesGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -76,7 +78,7 @@ class ItemsGridHelper extends HelperBase $GridData->setDataRowSourceId('id'); $GridData->addDataRowSource('name'); $GridData->addDataRowSource('description'); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -144,6 +146,7 @@ class ItemsGridHelper extends HelperBase * Devolver el paginador por defecto * * @param DataGridActionSearch $sourceAction + * * @return DataGridPager */ protected function getPager(DataGridActionSearch $sourceAction) @@ -162,10 +165,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getClientsGrid(array $data) + public function getClientsGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -181,7 +185,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSource('isGlobal', false, function ($value) { return $value ? __('SI') : __('NO'); }); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -245,10 +249,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getCustomFieldsGrid(array $data) + public function getCustomFieldsGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -266,7 +271,7 @@ class ItemsGridHelper extends HelperBase }); $GridData->addDataRowSource('typeName'); $GridData->addDataRowSourceWithIcon('isEncrypted', $this->icons->getIconEditPass()->setTitle(__('Encriptado'))); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -330,10 +335,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getFilesGrid(array $data) + public function getFilesGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -351,7 +357,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSource('name'); $GridData->addDataRowSource('type'); $GridData->addDataRowSource('size'); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -415,10 +421,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getAccountsGrid(array $data) + public function getAccountsGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -432,7 +439,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSource('name'); $GridData->addDataRowSource('clientName'); $GridData->addDataRowSource('categoryName'); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -473,10 +480,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getAccountsHistoryGrid(array $data) + public function getAccountsHistoryGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -498,7 +506,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSource('date'); $GridData->addDataRowSourceWithIcon('isModify', $iconEdit->setTitle(__('Modificada'))->setClass('opacity50')); $GridData->addDataRowSourceWithIcon('isDeleted', $iconDelete->setTitle(__('Eliminada'))->setClass('opacity50')); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -549,10 +557,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getUsersGrid(array $data) + public function getUsersGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -573,7 +582,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSourceWithIcon('isAdminAcc', $this->icons->getIconAccAdmin()); $GridData->addDataRowSourceWithIcon('isLdap', $this->icons->getIconLdapUser()); $GridData->addDataRowSourceWithIcon('isDisabled', $this->icons->getIconDisabled()); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -676,10 +685,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getUserGroupsGrid(array $data) + public function getUserGroupsGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -691,7 +701,7 @@ class ItemsGridHelper extends HelperBase $GridData->setDataRowSourceId('id'); $GridData->addDataRowSource('name'); $GridData->addDataRowSource('description'); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -766,10 +776,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getUserProfilesGrid(array $data) + public function getUserProfilesGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -779,7 +790,7 @@ class ItemsGridHelper extends HelperBase $GridData = new DataGridData(); $GridData->setDataRowSourceId('id'); $GridData->addDataRowSource('name'); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -854,10 +865,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getAuthTokensGrid(array $data) + public function getAuthTokensGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -868,8 +880,10 @@ class ItemsGridHelper extends HelperBase $GridData = new DataGridData(); $GridData->setDataRowSourceId('id'); $GridData->addDataRowSource('userLogin'); - $GridData->addDataRowSource('actionId'); - $GridData->setData($data); + $GridData->addDataRowSource('actionId', false, function ($value) { + return Acl::getActionInfo($value); + }); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -944,10 +958,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getPublicLinksGrid(array $data) + public function getPublicLinksGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -969,7 +984,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSource('userLogin'); $GridData->addDataRowSource('getNotifyString', true); $GridData->addDataRowSource('getCountViewsString', true); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -1043,10 +1058,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getTagsGrid(array $data) + public function getTagsGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -1056,7 +1072,7 @@ class ItemsGridHelper extends HelperBase $GridData = new DataGridData(); $GridData->setDataRowSourceId('id'); $GridData->addDataRowSource('name'); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -1120,10 +1136,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGridTab */ - public function getPluginsGrid(array $data) + public function getPluginsGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -1137,7 +1154,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSourceWithIcon('enabled', $this->icons->getIconEnabled()); $GridData->addDataRowSourceWithIcon('enabled', $this->icons->getIconDisabled(), 0); $GridData->addDataRowSourceWithIcon('available', $this->icons->getIconDelete()->setTitle(__('No disponible')), 0); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGridTab($this->view->getTheme()); @@ -1216,10 +1233,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGrid */ - public function getEventLogGrid(array $data) + public function getEventLogGrid(QueryResult $queryResult) { // Grid Header $GridHeaders = new DataGridHeader(); @@ -1267,7 +1285,7 @@ class ItemsGridHelper extends HelperBase return str_replace(PHP_EOL, '
', $text); }); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGrid($this->view->getTheme()); @@ -1323,10 +1341,11 @@ class ItemsGridHelper extends HelperBase } /** - * @param array $data + * @param QueryResult $queryResult + * * @return DataGrid */ - public function getNotificationsGrid(array $data) + public function getNotificationsGrid(QueryResult $queryResult) { $isAdminApp = $this->context->getUserData()->getIsAdminApp(); @@ -1351,7 +1370,7 @@ class ItemsGridHelper extends HelperBase $GridData->addDataRowSourceWithIcon('checked', $this->icons->getIconEnabled()->setTitle(__('Leída'))); $GridData->addDataRowSourceWithIcon('onlyAdmin', $this->icons->getIconAppAdmin()->setTitle(__('Sólo Admins'))); $GridData->addDataRowSourceWithIcon('sticky', $this->icons->getIconGroup()->setTitle(__('Global'))); - $GridData->setData($data); + $GridData->setData($queryResult); // Grid $Grid = new DataGrid($this->view->getTheme()); @@ -1450,6 +1469,7 @@ class ItemsGridHelper extends HelperBase * * @param DataGridInterface $dataGrid * @param ItemSearchData $itemSearchData + * * @return DataGridInterface */ public function updatePager(DataGridInterface $dataGrid, ItemSearchData $itemSearchData) diff --git a/app/modules/web/Controllers/NotificationController.php b/app/modules/web/Controllers/NotificationController.php index 2c0308a4..8e35faf3 100644 --- a/app/modules/web/Controllers/NotificationController.php +++ b/app/modules/web/Controllers/NotificationController.php @@ -57,6 +57,9 @@ class NotificationController extends ControllerBase implements CrudControllerInt /** * indexAction + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function indexAction() { @@ -75,6 +78,8 @@ class NotificationController extends ControllerBase implements CrudControllerInt * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -114,11 +119,12 @@ class NotificationController extends ControllerBase implements CrudControllerInt } /** - * Sets view data for displaying user's data + * Sets view data for displaying notification's data * * @param $notificationId - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($notificationId = null) { diff --git a/app/modules/web/Controllers/PluginController.php b/app/modules/web/Controllers/PluginController.php index f64e3563..80f0051a 100644 --- a/app/modules/web/Controllers/PluginController.php +++ b/app/modules/web/Controllers/PluginController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -40,6 +40,7 @@ use SP\Services\Plugin\PluginService; /** * Class PluginController + * * @package web\Controllers */ class PluginController extends ControllerBase @@ -53,6 +54,9 @@ class PluginController extends ControllerBase /** * indexAction + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function indexAction() { @@ -71,6 +75,8 @@ class PluginController extends ControllerBase * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -83,8 +89,8 @@ class PluginController extends ControllerBase /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -103,6 +109,7 @@ class PluginController extends ControllerBase * View action * * @param $id + * * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ @@ -132,8 +139,9 @@ class PluginController extends ControllerBase * Sets view data for displaying items's data * * @param $pluginId - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($pluginId = null) { diff --git a/app/modules/web/Controllers/PublicLinkController.php b/app/modules/web/Controllers/PublicLinkController.php index fc3854cb..df6719dc 100644 --- a/app/modules/web/Controllers/PublicLinkController.php +++ b/app/modules/web/Controllers/PublicLinkController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -61,8 +61,8 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -81,6 +81,8 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -120,9 +122,10 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter } /** - * Sets view data for displaying user's data + * Sets view data for displaying public link's data * * @param $publicLinkId + * * @throws \Psr\Container\ContainerExceptionInterface * @throws SPException */ @@ -153,6 +156,7 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * Create action * * @param int $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -179,6 +183,7 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -208,6 +213,7 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -284,6 +290,7 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * * @param int $accountId * @param int $notify + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -326,6 +333,7 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/TagController.php b/app/modules/web/Controllers/TagController.php index ec34646a..f62c129c 100644 --- a/app/modules/web/Controllers/TagController.php +++ b/app/modules/web/Controllers/TagController.php @@ -55,8 +55,8 @@ class TagController extends ControllerBase implements CrudControllerInterface /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -75,6 +75,8 @@ class TagController extends ControllerBase implements CrudControllerInterface * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -114,10 +116,12 @@ class TagController extends ControllerBase implements CrudControllerInterface } /** - * Sets view data for displaying user's data + * Sets view data for displaying tag's data * * @param $tagId - * @throws \Psr\Container\ContainerExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($tagId = null) { diff --git a/app/modules/web/Controllers/UserController.php b/app/modules/web/Controllers/UserController.php index 70a74c76..d15cf40e 100644 --- a/app/modules/web/Controllers/UserController.php +++ b/app/modules/web/Controllers/UserController.php @@ -62,8 +62,8 @@ class UserController extends ControllerBase implements CrudControllerInterface /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -82,6 +82,8 @@ class UserController extends ControllerBase implements CrudControllerInterface * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -124,6 +126,7 @@ class UserController extends ControllerBase implements CrudControllerInterface * Sets view data for displaying user's data * * @param $userId + * * @throws \SP\Core\Exceptions\SPException * @throws \Psr\Container\ContainerExceptionInterface */ @@ -175,6 +178,7 @@ class UserController extends ControllerBase implements CrudControllerInterface * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -205,6 +209,7 @@ class UserController extends ControllerBase implements CrudControllerInterface * Edit user's pass action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editPassAction($id) @@ -241,6 +246,7 @@ class UserController extends ControllerBase implements CrudControllerInterface * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -322,6 +328,7 @@ class UserController extends ControllerBase implements CrudControllerInterface /** * @param UserData $userData + * * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -345,6 +352,7 @@ class UserController extends ControllerBase implements CrudControllerInterface * Saves edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -356,7 +364,6 @@ class UserController extends ControllerBase implements CrudControllerInterface try { $form = new UserForm($id); - $form->setIsLdap(Request::analyzeInt('isLdap', 0)); $form->validate(ActionsInterface::USER_EDIT); $itemData = $form->getItemData(); @@ -422,6 +429,7 @@ class UserController extends ControllerBase implements CrudControllerInterface * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/UserGroupController.php b/app/modules/web/Controllers/UserGroupController.php index fd488d1d..65f79dd5 100644 --- a/app/modules/web/Controllers/UserGroupController.php +++ b/app/modules/web/Controllers/UserGroupController.php @@ -63,8 +63,8 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -83,6 +83,8 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -122,11 +124,12 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf } /** - * Sets view data for displaying user's data + * Sets view data for displaying user group's data * * @param $userGroupId - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($userGroupId = null) { @@ -156,6 +159,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -185,6 +189,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -264,6 +269,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf * Saves edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -303,6 +309,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Controllers/UserProfileController.php b/app/modules/web/Controllers/UserProfileController.php index 42f13c8e..c92f7369 100644 --- a/app/modules/web/Controllers/UserProfileController.php +++ b/app/modules/web/Controllers/UserProfileController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -57,8 +57,8 @@ class UserProfileController extends ControllerBase implements CrudControllerInte /** * Search action * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchAction() { @@ -77,6 +77,8 @@ class UserProfileController extends ControllerBase implements CrudControllerInte * getSearchGrid * * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function getSearchGrid() { @@ -116,10 +118,12 @@ class UserProfileController extends ControllerBase implements CrudControllerInte } /** - * Sets view data for displaying user's data + * Sets view data for displaying user profile's data * * @param $profileId - * @throws \Psr\Container\ContainerExceptionInterface + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function setViewData($profileId = null) { @@ -150,6 +154,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte * Edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function editAction($id) @@ -179,6 +184,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte * Delete action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -254,6 +260,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte * Saves edit action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ @@ -290,6 +297,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte * View action * * @param $id + * * @throws \Psr\Container\ContainerExceptionInterface */ public function viewAction($id) diff --git a/app/modules/web/Forms/FormBase.php b/app/modules/web/Forms/FormBase.php index ebd4e1e2..b11e9af3 100644 --- a/app/modules/web/Forms/FormBase.php +++ b/app/modules/web/Forms/FormBase.php @@ -64,6 +64,7 @@ abstract class FormBase */ public function __construct($itemId = null) { + // FIXME $this->injectDependencies(); $this->itemId = $itemId; diff --git a/app/modules/web/Forms/UserForm.php b/app/modules/web/Forms/UserForm.php index 978ae5b8..63f66da8 100644 --- a/app/modules/web/Forms/UserForm.php +++ b/app/modules/web/Forms/UserForm.php @@ -49,6 +49,7 @@ class UserForm extends FormBase implements FormInterface * Validar el formulario * * @param $action + * * @return bool * @throws ValidationException */ @@ -83,6 +84,8 @@ class UserForm extends FormBase implements FormInterface */ protected function analyzeRequestData() { + $this->isLdap = Request::analyzeInt('isLdap', 0); + $this->userData = new UserData(); $this->userData->setId($this->itemId); $this->userData->setName(Request::analyzeString('name')); @@ -97,6 +100,7 @@ class UserForm extends FormBase implements FormInterface $this->userData->setIsDisabled(Request::analyzeBool('disabled', false)); $this->userData->setIsChangePass(Request::analyzeBool('changepass_enabled', false)); $this->userData->setPass(Request::analyzeEncrypted('password')); + $this->userData->setIsLdap($this->isLdap); } /** @@ -129,6 +133,16 @@ class UserForm extends FormBase implements FormInterface } } + /** + * @return bool + */ + private function isDemo() + { + return $this->configData->isDemoEnabled() + && ($this->userData->getLogin() === 'demo' + && $this->userData->isAdminApp() === 0); + } + /** * @throws ValidationException */ @@ -167,16 +181,6 @@ class UserForm extends FormBase implements FormInterface } } - /** - * @return bool - */ - private function isDemo() - { - return $this->configData->isDemoEnabled() - && ($this->userData->getLogin() === 'demo' - && $this->userData->isAdminApp() === 0); - } - /** * @return UserData */ @@ -192,12 +196,4 @@ class UserForm extends FormBase implements FormInterface { return $this->isLdap; } - - /** - * @param int $isLdap - */ - public function setIsLdap($isLdap) - { - $this->isLdap = $isLdap; - } } \ No newline at end of file diff --git a/app/modules/web/Init.php b/app/modules/web/Init.php index f8db2860..cf0164d2 100644 --- a/app/modules/web/Init.php +++ b/app/modules/web/Init.php @@ -41,8 +41,8 @@ use SP\Services\Upgrade\UpgradeAppService; use SP\Services\Upgrade\UpgradeDatabaseService; use SP\Services\Upgrade\UpgradeUtil; use SP\Services\UserProfile\UserProfileService; -use SP\Storage\Database; -use SP\Storage\DBUtil; +use SP\Storage\Database\Database; +use SP\Storage\Database\DBUtil; use SP\Util\HttpUtil; /** diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc index 266309df..2054632a 100644 --- a/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc @@ -10,10 +10,8 @@ use SP\Html\Html; getData()->getDataCount() > 0): - foreach ($data->getData()->getData() as $dataIndex => $dataItem): - if ($dataIndex === 'count'): continue; endif; - $numFields = count($data->getData()->getDataRowSources()); ?> - + $numFields = count($data->getData()->getDataRowSources()); + foreach ($data->getData()->getData() as $dataIndex => $dataItem): ?> getData()->getDataRowSources() as $rowSrc): ?> {$rowSrc['name']}() : $dataItem->{$rowSrc['name']}; ?> diff --git a/lib/Definitions.php b/lib/Definitions.php index 240e45e5..cb6f5f35 100644 --- a/lib/Definitions.php +++ b/lib/Definitions.php @@ -43,12 +43,12 @@ return [ \SP\Config\ConfigData::class => function (\SP\Config\Config $config) { return $config->getConfigData(); }, - \SP\Storage\DatabaseConnectionData::class => function (\SP\Config\ConfigData $configData) { - return \SP\Storage\DatabaseConnectionData::getFromConfig($configData); + \SP\Storage\Database\DatabaseConnectionData::class => function (\SP\Config\ConfigData $configData) { + return \SP\Storage\Database\DatabaseConnectionData::getFromConfig($configData); }, - \SP\Storage\Database::class => object(\SP\Storage\Database::class) - ->constructor(object(\SP\Storage\MySQLHandler::class) - ->constructor(get(\SP\Storage\DatabaseConnectionData::class))), + \SP\Storage\Database\Database::class => object(\SP\Storage\Database\Database::class) + ->constructor(object(\SP\Storage\Database\MySQLHandler::class) + ->constructor(get(\SP\Storage\Database\DatabaseConnectionData::class))), \SP\Core\Acl\Actions::class => object(\SP\Core\Acl\Actions::class) ->constructor(object(\SP\Storage\FileCache::class), object(\SP\Storage\XmlHandler::class) ->constructor(object(\SP\Storage\FileHandler::class) diff --git a/lib/SP/Core/Crypt/Vault.php b/lib/SP/Core/Crypt/Vault.php index 9e5d3c14..06b054db 100644 --- a/lib/SP/Core/Crypt/Vault.php +++ b/lib/SP/Core/Crypt/Vault.php @@ -48,11 +48,20 @@ class Vault */ private $timeUpdated = 0; + /** + * @return static + */ + public static function getInstance() + { + return new static(); + } + /** * Regenerar la clave de sesión * * @param string $newSeed * @param string $oldSeed + * * @return Vault * @throws \Defuse\Crypto\Exception\CryptoException */ @@ -70,6 +79,7 @@ class Vault * Devolver la clave maestra de la sesión * * @param string $key + * * @return string * @throws \Defuse\Crypto\Exception\CryptoException */ @@ -81,8 +91,9 @@ class Vault /** * Guardar la clave maestra en la sesión * - * @param mixed $data + * @param mixed $data * @param string $key + * * @return $this * @throws \Defuse\Crypto\Exception\CryptoException */ @@ -113,4 +124,12 @@ class Vault { return $this->timeUpdated; } + + /** + * Serializaes the current object + */ + public function getSerialized() + { + return serialize($this); + } } \ No newline at end of file diff --git a/lib/SP/Html/DataGrid/DataGridDataBase.php b/lib/SP/Html/DataGrid/DataGridDataBase.php index 8a06a862..08cfd3aa 100644 --- a/lib/SP/Html/DataGrid/DataGridDataBase.php +++ b/lib/SP/Html/DataGrid/DataGridDataBase.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -25,6 +25,7 @@ namespace SP\Html\DataGrid; use SP\Html\Assets\IconInterface; +use SP\Storage\Database\QueryResult; defined('APP_ROOT') || die(); @@ -119,12 +120,12 @@ abstract class DataGridDataBase implements DataGridDataInterface } /** - * @param $data array + * @param QueryResult $queryResult */ - public function setData(array $data) + public function setData(QueryResult $queryResult) { - $this->_dataCount = isset($data['count']) ? $data['count'] : count($data); - $this->_data = $data; + $this->_dataCount = $queryResult->getNumRows(); + $this->_data = $queryResult->getDataAsArray(); } /** diff --git a/lib/SP/Html/DataGrid/DataGridDataInterface.php b/lib/SP/Html/DataGrid/DataGridDataInterface.php index 47ba1d68..1eeab63c 100644 --- a/lib/SP/Html/DataGrid/DataGridDataInterface.php +++ b/lib/SP/Html/DataGrid/DataGridDataInterface.php @@ -25,6 +25,7 @@ namespace SP\Html\DataGrid; use SP\Html\Assets\IconInterface; +use SP\Storage\Database\QueryResult; defined('APP_ROOT') || die(); @@ -68,9 +69,9 @@ interface DataGridDataInterface /** * Establecer los datos de la consulta * - * @param $data array + * @param QueryResult $queryResult */ - public function setData(array $data); + public function setData(QueryResult $queryResult); /** * Devolver los datos de la consulta diff --git a/lib/SP/Log/ActionLog.php b/lib/SP/Log/ActionLog.php index 2a963d18..25afcefa 100644 --- a/lib/SP/Log/ActionLog.php +++ b/lib/SP/Log/ActionLog.php @@ -29,7 +29,7 @@ use SP\Config\ConfigData; use SP\Core\Context\SessionContext; use SP\Core\Language; use SP\Core\Messages\LogMessage; -use SP\Storage\Database; +use SP\Storage\Database\Database; /** * Clase abstracta ActionLog para la gestión de mensajes de eventos @@ -69,7 +69,7 @@ abstract class ActionLog extends LogLevel /** @var \SP\Core\Context\SessionContext */ protected $session; /** - * @var Database + * @var \SP\Storage\Database\Database */ protected $db; @@ -90,10 +90,10 @@ abstract class ActionLog extends LogLevel } /** - * @param Config $config - * @param Language $language - * @param SessionContext $session - * @param Database $database + * @param Config $config + * @param Language $language + * @param SessionContext $session + * @param \SP\Storage\Database\Database $database */ public function inject(Config $config, Language $language, SessionContext $session, Database $database) { diff --git a/lib/SP/Log/Log.php b/lib/SP/Log/Log.php index 73cd9903..b20e13bf 100644 --- a/lib/SP/Log/Log.php +++ b/lib/SP/Log/Log.php @@ -26,8 +26,8 @@ namespace SP\Log; use SP\Core\Exceptions\SPException; use SP\Core\Messages\LogMessage; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\DbWrapper; +use SP\Storage\Database\QueryData; use SP\Util\HttpUtil; use SP\Util\Util; diff --git a/lib/SP/Repositories/Account/AccountFavoriteRepository.php b/lib/SP/Repositories/Account/AccountFavoriteRepository.php index a5c3c020..cbbd2eb0 100644 --- a/lib/SP/Repositories/Account/AccountFavoriteRepository.php +++ b/lib/SP/Repositories/Account/AccountFavoriteRepository.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -25,8 +25,7 @@ namespace SP\Repositories\Account; use SP\Repositories\Repository; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class AccountFavoriteRepository @@ -39,7 +38,10 @@ class AccountFavoriteRepository extends Repository * Obtener un array con los Ids de cuentas favoritas * * @param $id int El Id de usuario + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getForUserId($id) { @@ -48,7 +50,7 @@ class AccountFavoriteRepository extends Repository $queryData->addParam($id); $queryData->setUseKeyPair(true); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doQuery($queryData)->getDataAsArray(); } /** @@ -56,8 +58,10 @@ class AccountFavoriteRepository extends Repository * * @param $accountId int El Id de la cuenta * @param $userId int El Id del usuario - * @return bool - * @throws \SP\Core\Exceptions\SPException + * + * @return int + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function add($accountId, $userId) { @@ -66,7 +70,7 @@ class AccountFavoriteRepository extends Repository $queryData->setParams([$accountId, $userId]); $queryData->setOnErrorMessage(__u('Error al añadir favorito')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -74,6 +78,7 @@ class AccountFavoriteRepository extends Repository * * @param $accountId int El Id de la cuenta * @param $userId int El Id del usuario + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -85,8 +90,6 @@ class AccountFavoriteRepository extends Repository $queryData->setParams([$accountId, $userId]); $queryData->setOnErrorMessage(__u('Error al eliminar favorito')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountFileRepository.php b/lib/SP/Repositories/Account/AccountFileRepository.php index 6ea36d79..b30a01a0 100644 --- a/lib/SP/Repositories/Account/AccountFileRepository.php +++ b/lib/SP/Repositories/Account/AccountFileRepository.php @@ -31,8 +31,7 @@ use SP\Repositories\NoSuchItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class AccountFileRepository @@ -47,6 +46,7 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac * Creates an item * * @param FileData $itemData + * * @return mixed * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -76,15 +76,14 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac ]); $queryData->setOnErrorMessage(__u('No se pudo guardar el archivo')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** * Updates an item * * @param mixed $itemData + * * @return mixed */ public function update($itemData) @@ -94,7 +93,10 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac /** * @param $id + * * @return FileExtData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getInfoById($id) { @@ -116,14 +118,17 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns the item for given id * * @param int $id + * * @return FileExtData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -148,14 +153,17 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns the item for given id * * @param int $id + * * @return FileData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByAccountId($id) { @@ -176,13 +184,15 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->addParam($id); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * Returns all the items * * @return FileExtData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -205,14 +215,17 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->setMapClassName(FileExtData::class); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * Returns all the items for given ids * * @param array $ids + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { @@ -237,7 +250,7 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doQuery($queryData)->getDataAsArray(); } /** @@ -260,9 +273,7 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el archivo')); - DbWrapper::getQuery($queryData, $this->db); - - if ($queryData->getQueryNumRows() === 0) { + if ($this->db->doQuery($queryData)->getAffectedNumRows() === 0) { throw new NoSuchItemException(__u('Archivo no encontrado')); } @@ -273,6 +284,7 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac * Deletes all the items for given ids * * @param array $ids + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -284,9 +296,7 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar los archivos')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -323,7 +333,10 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac * Searches for items by a given filter * * @param ItemSearchData $itemSearchData + * * @return mixed + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -347,12 +360,6 @@ class AccountFileRepository extends Repository implements RepositoryItemInterfac $queryData->addParam($itemSearchData->getLimitStart()); $queryData->addParam($itemSearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountHistoryRepository.php b/lib/SP/Repositories/Account/AccountHistoryRepository.php index 3282ad42..63e49048 100644 --- a/lib/SP/Repositories/Account/AccountHistoryRepository.php +++ b/lib/SP/Repositories/Account/AccountHistoryRepository.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -25,6 +25,7 @@ namespace SP\Repositories\Account; use SP\Account\AccountUtil; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\AccountHistoryData; @@ -34,8 +35,8 @@ use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; use SP\Services\Account\AccountPasswordRequest; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class AccountHistoryRepository @@ -50,7 +51,10 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Obtiene el listado del histórico de una cuenta. * * @param $id + * * @return array|false Con los registros con id como clave y fecha - usuario como valor + * @throws QueryException + * @throws ConstraintException */ public function getHistoryForAccount($id) { @@ -70,12 +74,15 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->setQuery($query); $queryData->addParam($id); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * @param $id + * * @return AccountPassData + * @throws QueryException + * @throws ConstraintException */ public function getPasswordForHistoryId($id) { @@ -92,13 +99,14 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->setWhere($queryWhere); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * @param array $items array of ['id' => , 'isDelete' => ] + * * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function createBatch(array $items) { @@ -111,9 +119,10 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Crea una nueva cuenta en la BBDD * * @param array $itemData ['id' => , 'isModify' => ,'isDelete' => , 'masterPassHash' => ] + * * @return bool * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function create($itemData) { @@ -172,15 +181,17 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->addParam($itemData['id']); $queryData->setOnErrorMessage(__u('Error al actualizar el historial')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getLastId(); } /** * Elimina los datos de una cuenta en la BBDD. * * @param array|int $id + * * @return bool Los ids de las cuentas eliminadas - * @throws SPException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -192,15 +203,14 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() === 1; + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } /** * Updates an item * * @param mixed $itemData + * * @return mixed */ public function update($itemData) @@ -212,6 +222,7 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Returns the item for given id * * @param int $id + * * @return AccountHistoryData * @throws SPException */ @@ -261,19 +272,21 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->setMapClassName(AccountHistoryData::class); $queryData->addParam($id); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $results = $this->db->doSelect($queryData); - if ($queryRes === false) { + if ($results->getNumRows() === false) { throw new SPException(__u('No se pudieron obtener los datos de la cuenta'), SPException::CRITICAL); } - return $queryRes; + return $results->getData(); } /** * Returns all the items * * @return array + * @throws QueryException + * @throws ConstraintException */ public function getAll() { @@ -291,13 +304,14 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData = new QueryData(); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * Returns all the items for given ids * * @param array $ids + * * @return void */ public function getByIdBatch(array $ids) @@ -309,9 +323,10 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Deletes all the items for given ids * * @param array $ids + * * @return int + * @throws ConstraintException * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException */ public function deleteByIdBatch(array $ids) { @@ -321,15 +336,14 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar las cuentas')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Checks whether the item is in use or not * * @param $id int + * * @return void */ public function checkInUse($id) @@ -341,6 +355,7 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Checks whether the item is duplicated on updating * * @param mixed $itemData + * * @return void */ public function checkDuplicatedOnUpdate($itemData) @@ -352,6 +367,7 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Checks whether the item is duplicated on adding * * @param mixed $itemData + * * @return void */ public function checkDuplicatedOnAdd($itemData) @@ -363,7 +379,10 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter * Searches for items by a given filter * * @param ItemSearchData $SearchData - * @return mixed + * + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $SearchData) { @@ -387,19 +406,15 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** * Obtener los datos relativos a la clave de todas las cuentas. * * @return array Con los datos de la clave + * @throws ConstraintException + * @throws QueryException */ public function getAccountsPassData() { @@ -410,16 +425,17 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter $queryData = new QueryData(); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * Actualiza la clave de una cuenta en la BBDD. * * @param AccountPasswordRequest $request + * * @return bool + * @throws ConstraintException * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException */ public function updatePassword(AccountPasswordRequest $request) { @@ -440,6 +456,6 @@ class AccountHistoryRepository extends Repository implements RepositoryItemInter ]); $queryData->setOnErrorMessage(__u('Error al actualizar la clave')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } } \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountRepository.php b/lib/SP/Repositories/Account/AccountRepository.php index 60024083..0f6933aa 100644 --- a/lib/SP/Repositories/Account/AccountRepository.php +++ b/lib/SP/Repositories/Account/AccountRepository.php @@ -27,6 +27,7 @@ namespace SP\Repositories\Account; use SP\Account\AccountRequest; use SP\Account\AccountSearchFilter; use SP\Account\AccountUtil; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\AccountData; @@ -45,8 +46,8 @@ use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; use SP\Services\Account\AccountPasswordRequest; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class AccountRepository @@ -61,6 +62,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * Devolver el número total de cuentas * * @return \stdClass + * @throws QueryException + * @throws ConstraintException */ public function getTotalNumAccounts() { @@ -71,13 +74,15 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData = new QueryData(); $queryData->setQuery($query); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * @param $id * * @return AccountPassData + * @throws ConstraintException + * @throws QueryException */ public function getPasswordForId($id) { @@ -92,13 +97,15 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setWhere($queryFilter->getFilters()); $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * @param QueryCondition $queryCondition * * @return AccountPassData + * @throws ConstraintException + * @throws QueryException */ public function getPasswordHistoryForId(QueryCondition $queryCondition) { @@ -112,7 +119,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($queryCondition->getParams()); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -121,8 +128,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return bool - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException + * @throws QueryException */ public function incrementDecryptCounter($id) { @@ -133,7 +140,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->addParam($id); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } /** @@ -142,9 +149,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param AccountRequest $itemData * * @return int + * @throws ConstraintException * @throws QueryException - * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException */ public function create($itemData) { @@ -193,9 +199,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -203,9 +207,9 @@ class AccountRepository extends Repository implements RepositoryItemInterface * * @param AccountRequest $accountRequest * - * @return bool - * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function editPassword(AccountRequest $accountRequest) { @@ -230,7 +234,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar la clave')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -239,8 +243,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param AccountPasswordRequest $request * * @return bool + * @throws ConstraintException * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException */ public function updatePassword(AccountPasswordRequest $request) { @@ -255,7 +259,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$request->pass, $request->key, $request->id]); $queryData->setOnErrorMessage(__u('Error al actualizar la clave')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } /** @@ -265,8 +269,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param int $userId User's Id * * @return bool + * @throws ConstraintException * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException */ public function editRestore($historyId, $userId) { @@ -298,7 +302,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$historyId, $userId]); $queryData->setOnErrorMessage(__u('Error al restaurar cuenta')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } /** @@ -307,7 +311,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return int EL número de cuentas eliminadas - * @throws SPException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -317,9 +322,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -381,7 +384,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->addParam($itemData->id); $queryData->setOnErrorMessage(__u('Error al modificar la cuenta')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -392,6 +395,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @return AccountVData * @throws NoSuchItemException * @throws QueryException + * @throws ConstraintException */ public function getById($id) { @@ -399,25 +403,23 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT * FROM account_data_v WHERE id = ? LIMIT 1'); $queryData->setMapClassName(AccountVData::class); $queryData->addParam($id); + $queryData->setOnErrorMessage(__u('No se pudieron obtener los datos de la cuenta')); - /** @var AccountVData|array $queryRes */ - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - if ($queryRes === false) { - throw new QueryException(__u('No se pudieron obtener los datos de la cuenta')); - } - - if (is_array($queryRes) && count($queryRes) === 0) { + if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('La cuenta no existe')); } - return $queryRes; + return $result->getData(); } /** * Returns all the items * * @return AccountData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -425,7 +427,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(AccountData::class); $queryData->setQuery('SELECT * FROM Account ORDER BY id'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -444,8 +446,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return int + * @throws ConstraintException * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException */ public function deleteByIdBatch(array $ids) { @@ -455,9 +457,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar las cuentas')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -495,7 +495,9 @@ class AccountRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $SearchData * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $SearchData) { @@ -518,13 +520,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -533,8 +529,8 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return bool - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException + * @throws QueryException */ public function incrementViewCounter($id) { @@ -542,7 +538,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('UPDATE Account SET countView = (countView + 1) WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } /** @@ -553,6 +549,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @return AccountExtData * @throws NoSuchItemException * @throws QueryException + * @throws ConstraintException */ public function getDataForLink($id) { @@ -574,19 +571,15 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setMapClassName(AccountExtData::class); $queryData->addParam($id); + $queryData->setOnErrorMessage(__u('No se pudieron obtener los datos de la cuenta')); - /** @var AccountExtData|array $queryRes */ - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData, true); - if ($queryRes === false) { - throw new QueryException(__u('No se pudieron obtener los datos de la cuenta')); - } - - if (is_array($queryRes) && count($queryRes) === 0) { + if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('La cuenta no existe')); } - return $queryRes; + return $result->getData(); } /** @@ -595,6 +588,9 @@ class AccountRepository extends Repository implements RepositoryItemInterface * @param AccountSearchFilter $accountSearchFilter * * @return AccountSearchResponse + * @throws ConstraintException + * @throws QueryException + * @throws SPException */ public function getByFilter(AccountSearchFilter $accountSearchFilter) { @@ -666,13 +662,15 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(AccountSearchVData::class); - return new AccountSearchResponse($this->db->getFullRowCount($queryData), DbWrapper::getResultsArray($queryData, $this->db)); + return new AccountSearchResponse($this->db->getFullRowCount($queryData), $this->db->doSelect($queryData)->getDataAsArray()); } /** * @param QueryCondition $queryFilter * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getForUser(QueryCondition $queryFilter) { @@ -687,13 +685,15 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * @param QueryCondition $queryFilter * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getLinked(QueryCondition $queryFilter) { @@ -707,19 +707,21 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * Obtener los datos relativos a la clave de todas las cuentas. * * @return array Con los datos de la clave + * @throws ConstraintException + * @throws QueryException */ public function getAccountsPassData() { $queryData = new QueryData(); $queryData->setQuery('SELECT id, `name`, pass, `key` FROM Account WHERE BIT_LENGTH(pass) > 0'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountToTagRepository.php b/lib/SP/Repositories/Account/AccountToTagRepository.php index 3ae619d9..ab27737b 100644 --- a/lib/SP/Repositories/Account/AccountToTagRepository.php +++ b/lib/SP/Repositories/Account/AccountToTagRepository.php @@ -28,8 +28,7 @@ use SP\Account\AccountRequest; use SP\DataModel\ItemData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class AccountToTagRepository @@ -46,6 +45,8 @@ class AccountToTagRepository extends Repository * @param int $id * * @return ItemData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getTagsByAccountId($id) { @@ -61,7 +62,7 @@ class AccountToTagRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -81,7 +82,7 @@ class AccountToTagRepository extends Repository * * @param int $id * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -92,7 +93,7 @@ class AccountToTagRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar las etiquetas de la cuenta')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -100,7 +101,7 @@ class AccountToTagRepository extends Repository * * @param AccountRequest $accountRequest * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -118,7 +119,7 @@ class AccountToTagRepository extends Repository $queryData->addParam($tag); } - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountToUserGroupRepository.php b/lib/SP/Repositories/Account/AccountToUserGroupRepository.php index fb64ffdb..0515dcb2 100644 --- a/lib/SP/Repositories/Account/AccountToUserGroupRepository.php +++ b/lib/SP/Repositories/Account/AccountToUserGroupRepository.php @@ -28,8 +28,7 @@ use SP\Account\AccountRequest; use SP\DataModel\ItemData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class AccountToUserGroupRepository @@ -46,6 +45,8 @@ class AccountToUserGroupRepository extends Repository * @param int $id con el Id de la cuenta * * @return ItemData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUserGroupsByAccountId($id) { @@ -61,7 +62,7 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -70,6 +71,8 @@ class AccountToUserGroupRepository extends Repository * @param $id * * @return ItemData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUserGroupsByUserGroupId($id) { @@ -85,13 +88,13 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * @param $id int * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -102,9 +105,7 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar grupos asociados a la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -135,9 +136,7 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar grupos asociados a la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -156,17 +155,14 @@ class AccountToUserGroupRepository extends Repository $queryData = new QueryData(); $queryData->setQuery($query); + $queryData->setOnErrorMessage(__u('Error al actualizar los grupos secundarios')); foreach ($accountRequest->userGroupsView as $userGroup) { $queryData->addParam($accountRequest->id); $queryData->addParam($userGroup); } - $queryData->setOnErrorMessage(__u('Error al actualizar los grupos secundarios')); - - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -197,9 +193,7 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar grupos asociados a la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -218,16 +212,13 @@ class AccountToUserGroupRepository extends Repository $queryData = new QueryData(); $queryData->setQuery($query); + $queryData->setOnErrorMessage(__u('Error al actualizar los grupos secundarios')); foreach ($accountRequest->userGroupsEdit as $userGroup) { $queryData->addParam($accountRequest->id); $queryData->addParam($userGroup); } - $queryData->setOnErrorMessage(__u('Error al actualizar los grupos secundarios')); - - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountToUserRepository.php b/lib/SP/Repositories/Account/AccountToUserRepository.php index 46548341..cdd195c4 100644 --- a/lib/SP/Repositories/Account/AccountToUserRepository.php +++ b/lib/SP/Repositories/Account/AccountToUserRepository.php @@ -28,8 +28,7 @@ use SP\Account\AccountRequest; use SP\DataModel\ItemData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class AccountToUserRepository @@ -63,8 +62,8 @@ class AccountToUserRepository extends Repository * @param int $id con el Id de la cuenta * * @return int - * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function deleteByAccountId($id) { @@ -73,9 +72,7 @@ class AccountToUserRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar usuarios asociados a la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -103,7 +100,7 @@ class AccountToUserRepository extends Repository $queryData->addParam($user); } - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -129,8 +126,8 @@ class AccountToUserRepository extends Repository * @param int $id con el Id de la cuenta * * @return int - * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function deleteEditByAccountId($id) { @@ -139,9 +136,7 @@ class AccountToUserRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar usuarios asociados a la cuenta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -169,7 +164,7 @@ class AccountToUserRepository extends Repository $queryData->addParam($user); } - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -178,6 +173,8 @@ class AccountToUserRepository extends Repository * @param int $id con el id de la cuenta * * @return ItemData[] con los id de usuarios de la cuenta + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUsersByAccountId($id) { @@ -193,6 +190,6 @@ class AccountToUserRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/AuthToken/AuthTokenRepository.php b/lib/SP/Repositories/AuthToken/AuthTokenRepository.php index c059dd83..d2c338f1 100644 --- a/lib/SP/Repositories/AuthToken/AuthTokenRepository.php +++ b/lib/SP/Repositories/AuthToken/AuthTokenRepository.php @@ -24,15 +24,14 @@ namespace SP\Repositories\AuthToken; -use SP\Core\Acl\Acl; -use SP\Core\Exceptions\SPException; use SP\DataModel\AuthTokenData; use SP\DataModel\ItemSearchData; +use SP\Repositories\DuplicatedItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class AuthTokenRepository @@ -59,9 +58,7 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error interno')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -69,7 +66,9 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * * @param int $id * - * @return mixed + * @return AuthTokenData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -79,16 +78,17 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface actionId, createdBy, startDate, - token + token, + `hash` FROM AuthToken WHERE id = ? LIMIT 1'; $queryData = new QueryData(); + $queryData->setMapClassName(AuthTokenData::class); $queryData->setQuery($query); $queryData->addParam($id); - $queryData->setMapClassName(AuthTokenData::class); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -118,20 +118,22 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * * @param array $ids * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function deleteByIdBatch(array $ids) { + if (count($ids) === 0) { + return 0; + } + $queryData = new QueryData(); $queryData->setQuery('DELETE FROM AuthToken WHERE id IN (' . $this->getParamsFromArray($ids) . ')'); $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error interno')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -151,7 +153,9 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $SearchData * - * @return mixed + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $SearchData) { @@ -173,25 +177,11 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->addParam($search); } - $query .= ' ORDER BY U.login'; - $query .= ' LIMIT ?, ?'; - - $queryData->addParam($SearchData->getLimitStart()); - $queryData->addParam($SearchData->getLimitCount()); - $queryData->setQuery($query); + $queryData->setOrder('U.login, AT.actionId'); + $queryData->setLimit('?, ?', [$SearchData->getLimitStart(), $SearchData->getLimitCount()]); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - foreach ($queryRes as $token) { - $token->actionId = Acl::getActionInfo($token->actionId); - } - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -199,15 +189,15 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * * @param AuthTokenData $itemData * - * @return mixed - * @throws SPException + * @return int + * @throws DuplicatedItemException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function create($itemData) { if ($this->checkDuplicatedOnAdd($itemData)) { - throw new SPException(__u('La autorización ya existe'), SPException::WARNING); + throw new DuplicatedItemException(__u('La autorización ya existe')); } $query = /** @lang SQL */ @@ -232,9 +222,7 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error interno')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -243,22 +231,25 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * @param AuthTokenData $itemData * * @return bool + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function checkDuplicatedOnAdd($itemData) { $query = /** @lang SQL */ 'SELECT id FROM AuthToken - WHERE userId = ? + WHERE (userId = ? OR token = ?) AND actionId = ? LIMIT 1'; $queryData = new QueryData(); $queryData->setQuery($query); - $queryData->addParam($itemData->getUserId()); - $queryData->addParam($itemData->getActionId()); + $queryData->setParams([ + $itemData->getUserId(), + $itemData->getToken(), + $itemData->getActionId() + ]); - DbWrapper::getResults($queryData, $this->db); - - return $queryData->getQueryNumRows() === 1; + return $this->db->doSelect($queryData)->getNumRows() === 1; } /** @@ -267,6 +258,8 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * @param $id * * @return string + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getTokenByUserId($id) { @@ -274,9 +267,9 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT token FROM AuthToken WHERE userId = ? AND token <> \'\' LIMIT 1'); $queryData->addParam($id); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - return $queryData->getQueryNumRows() === 1 ? $queryRes->token : null; + return $result->getNumRows() === 1 ? $result->getData()->token : null; } /** @@ -284,15 +277,15 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * * @param AuthTokenData $itemData * - * @return mixed - * @throws SPException + * @return int + * @throws DuplicatedItemException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function update($itemData) { if ($this->checkDuplicatedOnUpdate($itemData)) { - throw new SPException(__u('La autorización ya existe'), SPException::WARNING); + throw new DuplicatedItemException(__u('La autorización ya existe')); } $query = /** @lang SQL */ @@ -319,7 +312,7 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error interno')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -328,26 +321,27 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * @param AuthTokenData $itemData * * @return bool + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function checkDuplicatedOnUpdate($itemData) { $query = /** @lang SQL */ 'SELECT id FROM AuthToken - WHERE userId = ? - AND actionId = ? + WHERE (userId = ? OR token = ?) + AND actionId = ? AND id <> ? LIMIT 1'; $queryData = new QueryData(); $queryData->setQuery($query); $queryData->setParams([ $itemData->getUserId(), + $itemData->getToken(), $itemData->getActionId(), $itemData->getId() ]); - DbWrapper::getResults($queryData, $this->db); - - return $queryData->getQueryNumRows() === 1; + return $this->db->doSelect($queryData)->getNumRows() === 1; } /** @@ -373,7 +367,7 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$token, $id]); $queryData->setOnErrorMessage(__u('Error interno')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -401,7 +395,7 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$vault, $hash, $id]); $queryData->setOnErrorMessage(__u('Error interno')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -409,7 +403,9 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * * @param $token string El token de autorización * - * @return bool|mixed + * @return false|int + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUserIdForToken($token) { @@ -417,9 +413,9 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT userId FROM AuthToken WHERE token = ? LIMIT 1'); $queryData->addParam($token); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - return $queryData->getQueryNumRows() === 1 ? $queryRes->userId : false; + return $result->getNumRows() === 1 ? (int)$result->getData()->userId : false; } /** @@ -429,11 +425,13 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface * @param $token string El token de seguridad * * @return false|AuthTokenData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getTokenByToken($actionId, $token) { $query = /** @lang SQL */ - 'SELECT actionId, userId, vault, `hash` + 'SELECT id, actionId, userId, vault, `hash` FROM AuthToken WHERE actionId = ? AND token = ? LIMIT 1'; @@ -443,8 +441,8 @@ class AuthTokenRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams([$actionId, $token]); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - return $queryData->getQueryNumRows() === 1 ? $queryRes : false; + return $result->getNumRows() === 1 ? $result->getData() : false; } } \ No newline at end of file diff --git a/lib/SP/Repositories/Category/CategoryRepository.php b/lib/SP/Repositories/Category/CategoryRepository.php index d6320777..9da502f5 100644 --- a/lib/SP/Repositories/Category/CategoryRepository.php +++ b/lib/SP/Repositories/Category/CategoryRepository.php @@ -31,8 +31,8 @@ use SP\Repositories\DuplicatedItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class CategoryRepository @@ -67,9 +67,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear la categoría')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -90,9 +88,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $itemData->getName() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doQuery($queryData)->getNumRows() > 0; } /** @@ -100,8 +96,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface * * @param CategoryData $itemData * - * @return mixed - * @throws SPException + * @return int * @throws DuplicatedItemException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -129,9 +124,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar la categoría')); - DbWrapper::getQuery($queryData, $this->db); - - return $this; + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -153,9 +146,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $itemData->getId() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doQuery($queryData)->getNumRows() > 0; } /** @@ -164,6 +155,8 @@ class CategoryRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return CategoryData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -172,7 +165,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id, `name`, description FROM Category WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -181,6 +174,8 @@ class CategoryRepository extends Repository implements RepositoryItemInterface * @param string $name * * @return CategoryData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByName($name) { @@ -192,13 +187,15 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $this->makeItemHash($name, $this->db->getDbHandler()) ]); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return CategoryData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -206,7 +203,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(CategoryData::class); $queryData->setQuery('SELECT id, `name`, description, `hash` FROM Category ORDER BY `name`'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -215,9 +212,15 @@ class CategoryRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { + if (count($ids) === 0) { + return []; + } + $query = /** @lang SQL */ 'SELECT id, `name`, description FROM Category WHERE id IN (' . $this->getParamsFromArray($ids) . ')'; @@ -226,7 +229,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -245,9 +248,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar la categorías')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -269,9 +270,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar la categoría')); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -291,7 +290,9 @@ class CategoryRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $SearchData * - * @return mixed + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $SearchData) { @@ -312,13 +313,6 @@ class CategoryRepository extends Repository implements RepositoryItemInterface $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - /** @var array $queryRes */ - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Client/ClientRepository.php b/lib/SP/Repositories/Client/ClientRepository.php index ba91a75c..a4c72067 100644 --- a/lib/SP/Repositories/Client/ClientRepository.php +++ b/lib/SP/Repositories/Client/ClientRepository.php @@ -24,6 +24,7 @@ namespace SP\Repositories\Client; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ClientData; @@ -34,8 +35,8 @@ use SP\Repositories\DuplicatedItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class ClientRepository @@ -78,9 +79,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear el cliente')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -89,8 +88,8 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param ClientData $itemData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnAdd($itemData) { @@ -98,9 +97,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id FROM Client WHERE `hash` = ? LIMIT 1'); $queryData->addParam($this->makeItemHash($itemData->getName(), $this->db->getDbHandler())); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doQuery($queryData)->getNumRows() > 0; } /** @@ -108,9 +105,9 @@ class ClientRepository extends Repository implements RepositoryItemInterface * * @param ClientData $itemData * - * @return mixed - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException * @throws DuplicatedItemException */ public function update($itemData) @@ -138,9 +135,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar el cliente')); - DbWrapper::getQuery($queryData, $this->db); - - return $this; + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -149,8 +144,8 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param ClientData $itemData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnUpdate($itemData) { @@ -162,9 +157,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $itemData->getId() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doQuery($queryData)->getNumRows() > 0; } /** @@ -173,6 +166,8 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return ClientData + * @throws QueryException + * @throws ConstraintException */ public function getById($id) { @@ -181,7 +176,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id, `name`, description, isGlobal FROM Client WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -190,6 +185,8 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param string $name * * @return ClientData + * @throws QueryException + * @throws ConstraintException */ public function getByName($name) { @@ -201,13 +198,15 @@ class ClientRepository extends Repository implements RepositoryItemInterface $this->makeItemHash($name, $this->db->getDbHandler()) ]); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return ClientData[] + * @throws QueryException + * @throws ConstraintException */ public function getAll() { @@ -215,7 +214,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id, `name`, description, isGlobal FROM Client ORDER BY `name`'); $queryData->setMapClassName(ClientData::class); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -224,9 +223,15 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return array + * @throws QueryException + * @throws ConstraintException */ public function getByIdBatch(array $ids) { + if (count($ids) === 0) { + return []; + } + $query = /** @lang SQL */ 'SELECT id, `name`, description, isGlobal FROM Client WHERE id IN (' . $this->getParamsFromArray($ids) . ')'; @@ -235,7 +240,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -244,8 +249,8 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -254,9 +259,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar los clientes')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -265,8 +268,8 @@ class ClientRepository extends Repository implements RepositoryItemInterface * @param $id * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -275,9 +278,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el cliente')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -297,7 +298,9 @@ class ClientRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $SearchData * - * @return ClientData[] + * @return QueryResult + * @throws QueryException + * @throws ConstraintException */ public function search(ItemSearchData $SearchData) { @@ -319,13 +322,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -335,6 +332,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface * * @return ItemData[] * @throws QueryException + * @throws ConstraintException */ public function getAllForFilter(QueryCondition $queryFilter) { @@ -357,6 +355,6 @@ class ClientRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Config/ConfigRepository.php b/lib/SP/Repositories/Config/ConfigRepository.php index 62795b29..8333dc80 100644 --- a/lib/SP/Repositories/Config/ConfigRepository.php +++ b/lib/SP/Repositories/Config/ConfigRepository.php @@ -28,8 +28,8 @@ use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\DataModel\ConfigData; use SP\Repositories\Repository; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\DbWrapper; +use SP\Storage\Database\QueryData; /** * Class ConfigRepository @@ -65,8 +65,8 @@ class ConfigRepository extends Repository * @param ConfigData $configData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function update(ConfigData $configData) { @@ -74,13 +74,13 @@ class ConfigRepository extends Repository $queryData->setQuery('UPDATE Config SET `value` = ? WHERE parameter = ?'); $queryData->setParams([$configData->getValue(), $configData->getParam()]); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * @param ConfigData $configData * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -90,26 +90,30 @@ class ConfigRepository extends Repository $queryData->setQuery('INSERT INTO Config SET parameter = ?, value = ?'); $queryData->setParams([$configData->getParam(), $configData->getValue()]); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Obtener un array con la configuración almacenada en la BBDD. * * @return ConfigData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { $queryData = new QueryData(); $queryData->setQuery('SELECT parameter, value FROM Config'); - return DbWrapper::getResults($queryData); + return $this->db->doSelect($queryData)->getData(); } /** * @param string $param * * @return mixed + * @throws ConstraintException + * @throws QueryException */ public function getByParam($param) { @@ -117,7 +121,7 @@ class ConfigRepository extends Repository $queryData->setQuery('SELECT value FROM Config WHERE parameter = ? LIMIT 1'); $queryData->addParam($param); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -133,9 +137,7 @@ class ConfigRepository extends Repository $queryData->setQuery('SELECT parameter FROM Config WHERE parameter = ? LIMIT 1'); $queryData->addParam($param); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows() === 1; + return $this->db->doSelect($queryData)->getNumRows() === 1; } /** @@ -151,6 +153,6 @@ class ConfigRepository extends Repository $queryData->setQuery('DELETE FROM Config WHERE parameter = ? LIMIT 1'); $queryData->addParam($param); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/CustomField/CustomFieldDefCollection.php b/lib/SP/Repositories/CustomField/CustomFieldDefCollection.php index e926b58d..8962dceb 100644 --- a/lib/SP/Repositories/CustomField/CustomFieldDefCollection.php +++ b/lib/SP/Repositories/CustomField/CustomFieldDefCollection.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. diff --git a/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php b/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php index 5fae0329..11db35c4 100644 --- a/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php +++ b/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php @@ -30,8 +30,7 @@ use SP\DataModel\ItemSearchData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class CustomFieldDefRepository @@ -73,9 +72,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter ]); $queryData->setOnErrorMessage(__u('Error al crear el campo personalizado')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -83,7 +80,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter * * @param CustomFieldDefinitionData $itemData * - * @return mixed + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -108,7 +105,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter ]); $queryData->setOnErrorMessage(__u('Error al actualizar el campo personalizado')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -117,6 +114,8 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter * @param int $id * * @return CustomFieldDefinitionData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -134,16 +133,18 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->setQuery($query); $queryData->addParam($id); - $cfd = DbWrapper::getResults($queryData, $this->db); - $this->customFieldDefCollection->set($id, $cfd); + $result = $this->db->doSelect($queryData)->getData(); + $this->customFieldDefCollection->set($id, $result); - return $cfd; + return $result; } /** * Returns all the items * * @return CustomFieldDefinitionData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -156,7 +157,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->setMapClassName(CustomFieldDefinitionData::class); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -165,6 +166,8 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter * @param array $ids * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { @@ -178,7 +181,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -201,9 +204,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->addParam($ids); $queryData->setOnErrorMessage(__u('Error al eliminar los campos personalizados')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -211,7 +212,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter * * @param $id * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\SPException @@ -223,7 +224,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el campo personalizado')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -261,7 +262,9 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter * * @param ItemSearchData $SearchData * - * @return CustomFieldDefinitionData[] + * @return \SP\Storage\Database\QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $SearchData) { @@ -275,14 +278,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - /** @var CustomFieldDefinitionData[] $queryRes */ - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } protected function initialize() diff --git a/lib/SP/Repositories/CustomField/CustomFieldRepository.php b/lib/SP/Repositories/CustomField/CustomFieldRepository.php index c70c73cf..e9f80d6e 100644 --- a/lib/SP/Repositories/CustomField/CustomFieldRepository.php +++ b/lib/SP/Repositories/CustomField/CustomFieldRepository.php @@ -24,14 +24,14 @@ namespace SP\Repositories\CustomField; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\DataModel\CustomFieldData; use SP\DataModel\ItemSearchData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class CustomFieldRepository @@ -49,7 +49,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return bool * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function update($itemData) { @@ -71,7 +71,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $itemData->getDefinitionId() ]); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -81,7 +81,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return bool * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function checkExists($itemData) { @@ -100,9 +100,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $itemData->getDefinitionId() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() >= 1; + return $this->db->doSelect($queryData)->getNumRows() >= 1; } /** @@ -124,7 +122,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return bool * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function create($itemData) { @@ -141,9 +139,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $itemData->getKey() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -154,7 +150,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return bool * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function deleteCustomFieldData($id, $moduleId) { @@ -167,9 +163,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->setParams([$id, $moduleId]); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -181,7 +175,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return bool * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function deleteCustomFieldDataForDefinition($id, $moduleId, $definitionId) { @@ -195,9 +189,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->setParams([$id, $moduleId, $definitionId]); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -207,7 +199,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return int * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function deleteCustomFieldDefinitionData($definitionId) { @@ -215,9 +207,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('DELETE FROM CustomFieldData WHERE definitionId = ?'); $queryData->addParam($definitionId); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -226,8 +216,8 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * @param array $definitionIds * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteCustomFieldDefinitionDataBatch(array $definitionIds) { @@ -235,9 +225,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('DELETE FROM CustomFieldData WHERE definitionId IN (' . $this->getParamsFromArray($definitionIds) . ')'); $queryData->setParams($definitionIds); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -248,7 +236,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * * @return int * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function deleteCustomFieldDataBatch(array $ids, $moduleId) { @@ -262,9 +250,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setParams($ids); $queryData->addParam($moduleId); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -283,6 +269,8 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * Returns all the items * * @return CustomFieldData[] + * @throws QueryException + * @throws ConstraintException */ public function getAll() { @@ -290,13 +278,15 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setMapClassName(CustomFieldData::class); $queryData->setQuery('SELECT * FROM CustomFieldData'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * Returns all the items * * @return CustomFieldData[] + * @throws QueryException + * @throws ConstraintException */ public function getAllEncrypted() { @@ -304,7 +294,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setMapClassName(CustomFieldData::class); $queryData->setQuery('SELECT * FROM CustomFieldData WHERE `key` IS NOT NULL'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -362,6 +352,8 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac * @param $itemId * * @return array + * @throws QueryException + * @throws ConstraintException */ public function getForModuleById($moduleId, $itemId) { @@ -388,7 +380,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->setParams([$itemId, $moduleId]); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** diff --git a/lib/SP/Repositories/CustomField/CustomFieldTypeRepository.php b/lib/SP/Repositories/CustomField/CustomFieldTypeRepository.php index 136df5ef..76e5db78 100644 --- a/lib/SP/Repositories/CustomField/CustomFieldTypeRepository.php +++ b/lib/SP/Repositories/CustomField/CustomFieldTypeRepository.php @@ -24,14 +24,15 @@ namespace SP\Repositories\CustomField; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\CustomFieldTypeData; use SP\DataModel\ItemSearchData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class CustomFieldTypeRepository @@ -49,8 +50,8 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte * * @return mixed * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function create($itemData) { @@ -59,9 +60,7 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte $queryData->setParams([$itemData->getName(), $itemData->getText()]); $queryData->setOnErrorMessage(__u('Error al crear el tipo de campo')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -83,8 +82,8 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte * * @return mixed * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function update($itemData) { @@ -97,9 +96,7 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte ]); $queryData->setOnErrorMessage(__u('Error al actualizar el tipo de campo')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -120,6 +117,8 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte * @param int $id * * @return CustomFieldTypeData + * @throws ConstraintException + * @throws QueryException */ public function getById($id) { @@ -128,13 +127,15 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte $queryData->setQuery('SELECT id, `name`, `text` FROM CustomFieldType WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return CustomFieldTypeData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -142,7 +143,7 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte $queryData->setMapClassName(CustomFieldTypeData::class); $queryData->setQuery('SELECT id, `name`, `text` FROM CustomFieldType'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -163,8 +164,8 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte * @param array $ids * * @return void - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -179,8 +180,8 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte * @param $id * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -189,7 +190,7 @@ class CustomFieldTypeRepository extends Repository implements RepositoryItemInte $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el tipo de campo')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** diff --git a/lib/SP/Repositories/DuplicatedItemException.php b/lib/SP/Repositories/DuplicatedItemException.php index 15636589..1b06500a 100644 --- a/lib/SP/Repositories/DuplicatedItemException.php +++ b/lib/SP/Repositories/DuplicatedItemException.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. diff --git a/lib/SP/Repositories/EventLog/EventlogRepository.php b/lib/SP/Repositories/EventLog/EventlogRepository.php index f0b8de34..8ce377fb 100644 --- a/lib/SP/Repositories/EventLog/EventlogRepository.php +++ b/lib/SP/Repositories/EventLog/EventlogRepository.php @@ -24,11 +24,13 @@ namespace SP\Repositories\EventLog; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; use SP\DataModel\EventlogData; use SP\DataModel\ItemSearchData; use SP\Repositories\Repository; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class EventlogRepository @@ -41,8 +43,8 @@ class EventlogRepository extends Repository * Clears the event log * * @return bool con el resultado - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws QueryException + * @throws ConstraintException */ public function clear() { @@ -50,14 +52,17 @@ class EventlogRepository extends Repository $queryData->setQuery('TRUNCATE TABLE EventLog'); $queryData->setOnErrorMessage(__u('Error al vaciar el registro de eventos')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows() > 0; } /** * Searches for items by a given filter * * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -77,22 +82,16 @@ class EventlogRepository extends Repository $queryData->addParam($itemSearchData->getLimitStart()); $queryData->addParam($itemSearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - /** @var array $queryRes */ - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** * @param EventlogData $eventlogData + * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function create(EventlogData $eventlogData) { @@ -116,8 +115,6 @@ class EventlogRepository extends Repository $eventlogData->getLevel()] ); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/NoSuchItemException.php b/lib/SP/Repositories/NoSuchItemException.php index f035dabd..638dbe3f 100644 --- a/lib/SP/Repositories/NoSuchItemException.php +++ b/lib/SP/Repositories/NoSuchItemException.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. diff --git a/lib/SP/Repositories/Notification/NotificationRepository.php b/lib/SP/Repositories/Notification/NotificationRepository.php index fce01fce..c7c27cf9 100644 --- a/lib/SP/Repositories/Notification/NotificationRepository.php +++ b/lib/SP/Repositories/Notification/NotificationRepository.php @@ -24,13 +24,15 @@ namespace SP\Repositories\Notification; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; use SP\DataModel\ItemSearchData; use SP\DataModel\NotificationData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class NotificationRepository @@ -47,8 +49,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param NotificationData $itemData * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function create($itemData) { @@ -75,9 +77,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa ]); $queryData->setOnErrorMessage(__u('Error al crear la notificación')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -86,8 +86,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param NotificationData $itemData * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function update($itemData) { @@ -117,9 +117,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa ]); $queryData->setOnErrorMessage(__u('Error al modificar la notificación')); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -128,8 +126,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param $id * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -138,9 +136,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar la notificación')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -149,8 +145,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param $id * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteAdmin($id) { @@ -159,9 +155,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar la notificación')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -170,8 +164,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param array $ids * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteAdminBatch(array $ids) { @@ -180,9 +174,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar las notificaciones')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -191,6 +183,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param int $id * * @return NotificationData + * @throws ConstraintException + * @throws QueryException */ public function getById($id) { @@ -213,13 +207,15 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al obtener la notificación')); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return NotificationData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -240,7 +236,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->setQuery($query); $queryData->setOnErrorMessage(__u('Error al obtener las notificaciones')); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -249,6 +245,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param array $ids * * @return NotificationData[] + * @throws ConstraintException + * @throws QueryException */ public function getByIdBatch(array $ids) { @@ -270,7 +268,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -279,8 +277,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param array $ids * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -289,9 +287,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar las notificaciones')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -329,7 +325,9 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * * @param ItemSearchData $itemSearchData * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -351,14 +349,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($itemSearchData->getLimitStart()); $queryData->addParam($itemSearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - /** @var array $queryRes */ - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -367,7 +358,9 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param ItemSearchData $itemSearchData * @param int $userId * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function searchForUserId(ItemSearchData $itemSearchData, $userId) { @@ -395,14 +388,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($itemSearchData->getLimitStart()); $queryData->addParam($itemSearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - /** @var array $queryRes */ - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -411,8 +397,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param $id * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function setCheckedById($id) { @@ -424,9 +410,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al modificar la notificación')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -436,6 +420,8 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa * @param $id * * @return NotificationData[] + * @throws ConstraintException + * @throws QueryException */ public function getForUserIdByDate($component, $id) { @@ -459,13 +445,15 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->setParams([$component, $id]); $queryData->setOnErrorMessage(__u('Error al obtener las notificaciones')); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * @param $id * * @return NotificationData[] + * @throws ConstraintException + * @throws QueryException */ public function getAllForUserId($id) { @@ -490,13 +478,15 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al obtener las notificaciones')); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** * @param $id * * @return NotificationData[] + * @throws ConstraintException + * @throws QueryException */ public function getAllActiveForUserId($id) { @@ -522,6 +512,6 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al obtener las notificaciones')); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Plugin/PluginRepository.php b/lib/SP/Repositories/Plugin/PluginRepository.php index fb046554..42001141 100644 --- a/lib/SP/Repositories/Plugin/PluginRepository.php +++ b/lib/SP/Repositories/Plugin/PluginRepository.php @@ -24,6 +24,8 @@ namespace SP\Repositories\Plugin; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemData; use SP\DataModel\ItemSearchData; @@ -31,8 +33,8 @@ use SP\DataModel\PluginData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class PluginRepository @@ -49,8 +51,8 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param PluginData $itemData * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function create($itemData) { @@ -67,9 +69,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear el plugin')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -77,9 +77,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * * @param PluginData $itemData * - * @return mixed - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function update($itemData) { @@ -102,7 +102,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar el plugin')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -111,6 +111,8 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return PluginData + * @throws ConstraintException + * @throws QueryException */ public function getById($id) { @@ -128,13 +130,15 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return PluginData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -150,7 +154,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(PluginData::class); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -159,6 +163,8 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return PluginData[] + * @throws ConstraintException + * @throws QueryException */ public function getByIdBatch(array $ids) { @@ -175,7 +181,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -185,8 +191,8 @@ class PluginRepository extends Repository implements RepositoryItemInterface * * @return void * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -202,8 +208,8 @@ class PluginRepository extends Repository implements RepositoryItemInterface * * @return int * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -212,9 +218,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el plugin')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -258,7 +262,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $itemSearchData * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -278,14 +284,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->addParam($itemSearchData->getLimitStart()); $queryData->addParam($itemSearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - /** @var array $queryRes */ - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -294,6 +293,8 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param string $name * * @return PluginData + * @throws ConstraintException + * @throws QueryException */ public function getByName($name) { @@ -311,7 +312,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->addParam($name); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -320,9 +321,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param $id * @param $enabled * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function toggleEnabled($id, $enabled) { @@ -331,7 +332,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$enabled, $id]); $queryData->setOnErrorMessage(__u('Error al actualizar el plugin')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -340,9 +341,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param $name * @param $enabled * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function toggleEnabledByName($name, $enabled) { @@ -351,7 +352,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$enabled, $name]); $queryData->setOnErrorMessage(__u('Error al actualizar el plugin')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -360,9 +361,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param $id * @param $available * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function toggleAvailable($id, $available) { @@ -371,7 +372,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$id, $available]); $queryData->setOnErrorMessage(__u('Error al actualizar el plugin')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -380,9 +381,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * @param $name * @param $available * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function toggleAvailableByName($name, $available) { @@ -391,7 +392,7 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$available, $name]); $queryData->setOnErrorMessage(__u('Error al actualizar el plugin')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -399,9 +400,9 @@ class PluginRepository extends Repository implements RepositoryItemInterface * * @param int $id Id del plugin * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function resetById($id) { @@ -410,13 +411,15 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al actualizar el plugin')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Devolver los plugins activados * * @return ItemData[] + * @throws ConstraintException + * @throws QueryException */ public function getEnabled() { @@ -424,6 +427,6 @@ class PluginRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(ItemData::class); $queryData->setQuery('SELECT id, name FROM Plugin WHERE enabled = 1'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/PublicLink/PublicLinkRepository.php b/lib/SP/Repositories/PublicLink/PublicLinkRepository.php index b2fd42c6..2badefbb 100644 --- a/lib/SP/Repositories/PublicLink/PublicLinkRepository.php +++ b/lib/SP/Repositories/PublicLink/PublicLinkRepository.php @@ -24,6 +24,7 @@ namespace SP\Repositories\PublicLink; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; @@ -34,8 +35,7 @@ use SP\Repositories\NoSuchItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class PublicLinkRepository @@ -54,7 +54,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * * @return int * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function delete($id) @@ -64,15 +64,15 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar enlace')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * Returns all the items * * @return PublicLinkData[] + * @throws QueryException + * @throws ConstraintException */ public function getAll() { @@ -102,7 +102,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(PublicLinkListData::class); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -111,6 +111,8 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return PublicLinkData[] + * @throws QueryException + * @throws ConstraintException */ public function getByIdBatch(array $ids) { @@ -142,7 +144,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -151,7 +153,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function deleteByIdBatch(array $ids) @@ -160,9 +162,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('DELETE FROM PublicLink WHERE id IN (' . $this->getParamsFromArray($ids) . ')'); $queryData->setParams($ids); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -183,6 +183,8 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @param ItemSearchData $SearchData * * @return mixed + * @throws QueryException + * @throws ConstraintException */ public function search(ItemSearchData $SearchData) { @@ -225,13 +227,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -242,7 +238,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @return int * @throws DuplicatedItemException * @throws QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException */ public function create($itemData) { @@ -276,9 +272,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear enlace')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -287,6 +281,8 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @param PublicLinkData $itemData * * @return bool + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnAdd($itemData) { @@ -294,9 +290,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id FROM PublicLink WHERE itemId = ? LIMIT 1'); $queryData->addParam($itemData->getItemId()); - DbWrapper::getResults($queryData, $this->db); - - return ($queryData->getQueryNumRows() === 1); + return $this->db->doQuery($queryData)->getNumRows() === 1; } /** @@ -316,8 +310,8 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * * @param PublicLinkData $publicLinkData * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException + * @return int + * @throws ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function addLinkView(PublicLinkData $publicLinkData) @@ -337,7 +331,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar enlace')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -345,9 +339,9 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * * @param PublicLinkData $itemData * - * @return mixed + * @return int * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function update($itemData) @@ -385,7 +379,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar enlace')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -393,9 +387,9 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * * @param PublicLinkData $publicLinkData * - * @return bool + * @return int * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function refresh(PublicLinkData $publicLinkData) @@ -420,7 +414,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al renovar enlace')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -431,6 +425,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @return PublicLinkData * @throws NoSuchItemException * @throws QueryException + * @throws ConstraintException */ public function getById($id) { @@ -460,25 +455,24 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(PublicLinkListData::class); $queryData->setQuery($query); $queryData->addParam($id); + $queryData->setOnErrorMessage(__u('Error al obtener enlace')); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - if ($queryRes === false) { - throw new QueryException(__u('Error al obtener enlace')); - } - - if ($queryData->getQueryNumRows() === 0) { + if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('El enlace no existe')); } - return $queryRes; + return $result->getData(); } /** * @param $hash string * - * @return bool|PublicLinkData + * @return PublicLinkData + * @throws ConstraintException * @throws NoSuchItemException + * @throws QueryException */ public function getByHash($hash) { @@ -511,14 +505,13 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->addParam($hash); $queryData->setOnErrorMessage(__u('Error al obtener enlace')); - /** @var PublicLinkData $queryRes */ - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - if ($queryData->getQueryNumRows() === 0) { + if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('El enlace no existe')); } - return $queryRes; + return $result->getData(); } /** @@ -527,6 +520,8 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * @param int $itemId * * @return PublicLinkData + * @throws ConstraintException + * @throws NoSuchItemException * @throws QueryException */ public function getHashForItem($itemId) @@ -535,13 +530,14 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(PublicLinkData::class); $queryData->setQuery('SELECT id, `hash` FROM PublicLink WHERE itemId = ? LIMIT 1'); $queryData->addParam($itemId); + $queryData->setOnErrorMessage(__u('Error al obtener enlace')); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - if ($queryRes === false) { - throw new QueryException(__u('Error al obtener enlace')); + if ($result->getNumRows() === 0) { + throw new NoSuchItemException(__u('El enlace no existe')); } - return $queryRes; + return $result->getData(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Repository.php b/lib/SP/Repositories/Repository.php index 9f824412..b40de027 100644 --- a/lib/SP/Repositories/Repository.php +++ b/lib/SP/Repositories/Repository.php @@ -26,10 +26,8 @@ namespace SP\Repositories; use SP\Config\Config; use SP\Core\Context\ContextInterface; -use SP\Core\Dic\Container; use SP\Core\Events\EventDispatcher; -use SP\Storage\Database; -use SP\Storage\DatabaseInterface; +use SP\Storage\Database\Database; /** * Class Repository @@ -51,26 +49,20 @@ abstract class Repository */ protected $eventDispatcher; /** - * @var DatabaseInterface + * @var Database */ protected $db; - /** - * @var Container - */ - private $dic; /** * Repository constructor. * - * @param Container $dic * @param Config $config * @param Database $database * @param ContextInterface $session * @param EventDispatcher $eventDispatcher */ - final public function __construct(Container $dic, Config $config, Database $database, ContextInterface $session, EventDispatcher $eventDispatcher) + final public function __construct(Config $config, Database $database, ContextInterface $session, EventDispatcher $eventDispatcher) { - $this->dic = $dic; $this->config = $config; $this->db = $database; $this->context = $session; diff --git a/lib/SP/Repositories/RepositoryItemInterface.php b/lib/SP/Repositories/RepositoryItemInterface.php index 429a5dba..472c0fff 100644 --- a/lib/SP/Repositories/RepositoryItemInterface.php +++ b/lib/SP/Repositories/RepositoryItemInterface.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -37,6 +37,7 @@ interface RepositoryItemInterface * Creates an item * * @param mixed $itemData + * * @return mixed */ public function create($itemData); @@ -45,6 +46,7 @@ interface RepositoryItemInterface * Updates an item * * @param mixed $itemData + * * @return mixed */ public function update($itemData); @@ -60,6 +62,7 @@ interface RepositoryItemInterface * Returns the item for given id * * @param int $id + * * @return mixed */ public function getById($id); @@ -75,6 +78,7 @@ interface RepositoryItemInterface * Returns all the items for given ids * * @param array $ids + * * @return array */ public function getByIdBatch(array $ids); @@ -83,6 +87,7 @@ interface RepositoryItemInterface * Deletes all the items for given ids * * @param array $ids + * * @return $this */ public function deleteByIdBatch(array $ids); @@ -91,6 +96,7 @@ interface RepositoryItemInterface * Checks whether the item is in use or not * * @param $id int + * * @return bool */ public function checkInUse($id); @@ -99,6 +105,7 @@ interface RepositoryItemInterface * Checks whether the item is duplicated on updating * * @param mixed $itemData + * * @return bool */ public function checkDuplicatedOnUpdate($itemData); @@ -107,6 +114,7 @@ interface RepositoryItemInterface * Checks whether the item is duplicated on adding * * @param mixed $itemData + * * @return bool */ public function checkDuplicatedOnAdd($itemData); @@ -115,6 +123,7 @@ interface RepositoryItemInterface * Searches for items by a given filter * * @param ItemSearchData $SearchData + * * @return mixed */ public function search(ItemSearchData $SearchData); diff --git a/lib/SP/Repositories/RepositoryItemTrait.php b/lib/SP/Repositories/RepositoryItemTrait.php index fa5f0a8a..8ca44c45 100644 --- a/lib/SP/Repositories/RepositoryItemTrait.php +++ b/lib/SP/Repositories/RepositoryItemTrait.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -26,8 +26,8 @@ namespace SP\Repositories; use SP\Core\Exceptions\SPException; use SP\DataModel\DataModelInterface; -use SP\Storage\DBStorageInterface; -use SP\Storage\DBUtil; +use SP\Storage\Database\DBStorageInterface; +use SP\Storage\Database\DBUtil; /** * Trait RepositoryItemTrait @@ -40,6 +40,7 @@ trait RepositoryItemTrait * Eliminar elementos en lotes * * @param $ids + * * @return array */ public function deleteBatch(array $ids) @@ -67,6 +68,7 @@ trait RepositoryItemTrait * * @param string $name * @param DBStorageInterface $DBStorage + * * @return string con el hash generado */ protected function makeItemHash($name, DBStorageInterface $DBStorage) @@ -81,6 +83,7 @@ trait RepositoryItemTrait * * @param array $items * @param string $string Cadena a utilizar para los parámetros + * * @return string */ protected function getParamsFromArray(array $items, $string = '?') diff --git a/lib/SP/Repositories/Tag/TagRepository.php b/lib/SP/Repositories/Tag/TagRepository.php index 6e77c99d..f51746f2 100644 --- a/lib/SP/Repositories/Tag/TagRepository.php +++ b/lib/SP/Repositories/Tag/TagRepository.php @@ -24,14 +24,16 @@ namespace SP\Repositories\Tag; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; use SP\DataModel\ItemSearchData; use SP\DataModel\TagData; use SP\Repositories\DuplicatedItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class TagRepository @@ -47,9 +49,9 @@ class TagRepository extends Repository implements RepositoryItemInterface * * @param TagData $itemData * - * @return mixed - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException * @throws DuplicatedItemException */ public function create($itemData) @@ -66,9 +68,7 @@ class TagRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear etiqueta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -77,8 +77,8 @@ class TagRepository extends Repository implements RepositoryItemInterface * @param TagData $itemData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnAdd($itemData) { @@ -89,9 +89,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $this->makeItemHash($itemData->getName(), $this->db->getDbHandler()) ]); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -99,9 +97,9 @@ class TagRepository extends Repository implements RepositoryItemInterface * * @param TagData $itemData * - * @return mixed - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException * @throws DuplicatedItemException */ public function update($itemData) @@ -119,7 +117,7 @@ class TagRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar etiqueta')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -128,8 +126,8 @@ class TagRepository extends Repository implements RepositoryItemInterface * @param TagData $itemData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnUpdate($itemData) { @@ -141,9 +139,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $itemData->getId() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -151,7 +147,9 @@ class TagRepository extends Repository implements RepositoryItemInterface * * @param int $id * - * @return mixed + * @return TagData + * @throws ConstraintException + * @throws QueryException */ public function getById($id) { @@ -160,13 +158,15 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id, `name` FROM Tag WHERE id = ? ORDER BY `name` LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return TagData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -174,7 +174,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(TagData::class); $queryData->setQuery('SELECT id, `name`, `hash` FROM Tag ORDER BY `name`'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -183,9 +183,15 @@ class TagRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return TagData[] + * @throws ConstraintException + * @throws QueryException */ public function getByIdBatch(array $ids) { + if (count($ids) === 0) { + return []; + } + $query = /** @lang SQL */ 'SELECT id, `name` FROM Tag WHERE id IN (' . $this->getParamsFromArray($ids) . ')'; @@ -194,7 +200,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -203,8 +209,8 @@ class TagRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -213,9 +219,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar etiquetas')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -224,8 +228,8 @@ class TagRepository extends Repository implements RepositoryItemInterface * @param $id * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -234,9 +238,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar etiqueta')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -245,8 +247,8 @@ class TagRepository extends Repository implements RepositoryItemInterface * @param $id int * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkInUse($id) { @@ -254,9 +256,7 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT tagId FROM AccountToTag WHERE tagId = ?'); $queryData->addParam($id); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -264,7 +264,9 @@ class TagRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $itemSearchData * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -284,12 +286,6 @@ class TagRepository extends Repository implements RepositoryItemInterface $queryData->addParam($itemSearchData->getLimitStart()); $queryData->addParam($itemSearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Track/TrackRepository.php b/lib/SP/Repositories/Track/TrackRepository.php index ab908716..0a72c855 100644 --- a/lib/SP/Repositories/Track/TrackRepository.php +++ b/lib/SP/Repositories/Track/TrackRepository.php @@ -26,8 +26,7 @@ namespace SP\Repositories\Track; use SP\DataModel\TrackData; use SP\Repositories\Repository; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class TrackRepository @@ -63,15 +62,13 @@ class TrackRepository extends Repository ]); $queryData->setOnErrorMessage(__u('Error al crear track')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** * @param $id int|array * - * @return mixed + * @return int * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\ConstraintException */ @@ -82,15 +79,13 @@ class TrackRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar track')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * @param TrackData $itemData * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -116,13 +111,15 @@ class TrackRepository extends Repository ]); $queryData->setOnErrorMessage(__u('Error al actualizar track')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * @param $id int * * @return TrackData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -142,11 +139,13 @@ class TrackRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al obtener track')); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * @return TrackData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -163,7 +162,7 @@ class TrackRepository extends Repository $queryData->setQuery($query); $queryData->setOnErrorMessage(__u('Error al obtener tracks')); - return DbWrapper::getResultsArray($queryData); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -172,6 +171,8 @@ class TrackRepository extends Repository * @param TrackRequest $trackRequest * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getTracksForClientFromTime(TrackRequest $trackRequest) { @@ -193,6 +194,6 @@ class TrackRepository extends Repository ]); $queryData->setOnErrorMessage(__u('Error al obtener tracks')); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/Track/TrackRequest.php b/lib/SP/Repositories/Track/TrackRequest.php index 35d03ab9..01b453a9 100644 --- a/lib/SP/Repositories/Track/TrackRequest.php +++ b/lib/SP/Repositories/Track/TrackRequest.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -28,6 +28,7 @@ use SP\Core\Exceptions\InvalidArgumentException; /** * Class TrackRequest + * * @package SP\Repositories\Track */ class TrackRequest @@ -40,6 +41,7 @@ class TrackRequest /** * @param string $address + * * @throws InvalidArgumentException */ public function setTrackIp($address) diff --git a/lib/SP/Repositories/UserPassRecover/UserPassRecoverRepository.php b/lib/SP/Repositories/User/UserPassRecoverRepository.php similarity index 88% rename from lib/SP/Repositories/UserPassRecover/UserPassRecoverRepository.php rename to lib/SP/Repositories/User/UserPassRecoverRepository.php index 83057db1..b3d1cc0f 100644 --- a/lib/SP/Repositories/UserPassRecover/UserPassRecoverRepository.php +++ b/lib/SP/Repositories/User/UserPassRecoverRepository.php @@ -22,12 +22,11 @@ * along with sysPass. If not, see . */ -namespace SP\Repositories\UserPassRecover; +namespace SP\Repositories\User; use SP\Core\Exceptions\SPException; use SP\Repositories\Repository; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class UserPassRecoverRepository @@ -59,9 +58,7 @@ class UserPassRecoverRepository extends Repository $queryData->setQuery($query); $queryData->setParams([$userId, $time]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows(); + return $this->db->doSelect($queryData)->getNumRows(); } /** @@ -70,7 +67,7 @@ class UserPassRecoverRepository extends Repository * @param $userId * @param $hash * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -88,7 +85,7 @@ class UserPassRecoverRepository extends Repository $queryData->setParams([$userId, $hash]); $queryData->setOnErrorMessage(__u('Error al generar el hash de recuperación')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -114,9 +111,7 @@ class UserPassRecoverRepository extends Repository $queryData->setParams([$hash, $time]); $queryData->setOnErrorMessage(__u('Error en comprobación de hash')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -126,6 +121,8 @@ class UserPassRecoverRepository extends Repository * @param $time * * @return mixed + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUserIdForHash($hash, $time) { @@ -141,6 +138,6 @@ class UserPassRecoverRepository extends Repository $queryData->setQuery($query); $queryData->setParams([$hash, $time]); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/User/UserRepository.php b/lib/SP/Repositories/User/UserRepository.php index c111c248..e8ea21de 100644 --- a/lib/SP/Repositories/User/UserRepository.php +++ b/lib/SP/Repositories/User/UserRepository.php @@ -24,6 +24,7 @@ namespace SP\Repositories\User; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; @@ -35,8 +36,8 @@ use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; use SP\Services\User\UpdatePassRequest; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class UserRepository @@ -53,9 +54,9 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param UserData $itemData * * @return int - * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException + * @throws DuplicatedItemException */ public function update($itemData) { @@ -99,9 +100,7 @@ class UserRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar el usuario')); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -110,8 +109,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param UserData $itemData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnUpdate($itemData) { @@ -131,9 +130,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $itemData->getEmail() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -142,9 +139,9 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param int $id * @param UpdatePassRequest $passRequest * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updatePassById($id, UpdatePassRequest $passRequest) { @@ -168,7 +165,7 @@ class UserRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al modificar la clave')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -177,8 +174,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param $id * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -187,9 +184,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el usuario')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -200,6 +195,7 @@ class UserRepository extends Repository implements RepositoryItemInterface * @return UserData * @throws NoSuchItemException * @throws QueryException + * @throws ConstraintException */ public function getById($id) { @@ -237,24 +233,23 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(UserData::class); $queryData->setQuery($query); $queryData->addParam($id); + $queryData->setOnErrorMessage(__u('Error al obtener los datos del usuario')); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - if ($queryRes === false) { - throw new QueryException(__u('Error al obtener los datos del usuario')); - } - - if (!($queryRes instanceof UserData)) { + if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('El usuario no existe')); } - return $queryRes; + return $result->getData(); } /** * Returns all the items * * @return UserData[] + * @throws QueryException + * @throws ConstraintException */ public function getAll() { @@ -289,7 +284,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(UserData::class); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -298,6 +293,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return UserData[] + * @throws QueryException + * @throws ConstraintException */ public function getByIdBatch(array $ids) { @@ -340,7 +337,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -349,8 +346,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return int - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -359,9 +356,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar los usuarios')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -381,7 +376,9 @@ class UserRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $SearchData * - * @return array + * @return QueryResult + * @throws QueryException + * @throws ConstraintException */ public function search(ItemSearchData $SearchData) { @@ -417,13 +414,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -483,9 +474,7 @@ class UserRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al crear el usuario')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -494,8 +483,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param UserData $itemData * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkDuplicatedOnAdd($itemData) { @@ -514,15 +503,14 @@ class UserRepository extends Repository implements RepositoryItemInterface $itemData->getEmail() ]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** * @param $login string * * @return UserData + * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ @@ -562,24 +550,23 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(UserData::class); $queryData->setQuery($query); $queryData->setParams([$login, $login]); + $queryData->setOnErrorMessage(__u('Error al obtener los datos del usuario')); - $queryRes = DbWrapper::getResults($queryData, $this->db); + $result = $this->db->doSelect($queryData); - if ($queryRes === false) { - throw new QueryException(__u('Error al obtener los datos del usuario')); - } - - if ($queryData->getQueryNumRows() === 0) { + if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('El usuario no existe')); } - return $queryRes; + return $result->getData(); } /** * Returns items' basic information * * @return UserData[] + * @throws ConstraintException + * @throws QueryException */ public function getBasicInfo() { @@ -600,7 +587,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(UserData::class); $queryData->setQuery($query); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -610,9 +597,9 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param $pass * @param $key * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updateMasterPassById($id, $pass, $key) { @@ -629,7 +616,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams([$pass, $key, $id]); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -637,9 +624,9 @@ class UserRepository extends Repository implements RepositoryItemInterface * * @param $id int El id del usuario * - * @return bool - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @return int + * @throws QueryException + * @throws ConstraintException */ public function updateLastLoginById($id) { @@ -647,15 +634,15 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('UPDATE User SET lastLogin = NOW(), loginCount = loginCount + 1 WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** * @param $login * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkExistsByLogin($login) { @@ -663,17 +650,15 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id FROM User WHERE UPPER(login) = UPPER(?) OR UPPER(ssoLogin) = UPPER(?) LIMIT 1'); $queryData->setParams([$login, $login]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** * @param UserData $itemData * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updateOnLogin(UserData $itemData) { @@ -699,7 +684,7 @@ class UserRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar el usuario')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -708,9 +693,9 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param int $id * @param UserPreferencesData $userPreferencesData * - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updatePreferencesById($id, UserPreferencesData $userPreferencesData) { @@ -719,7 +704,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setParams([serialize($userPreferencesData), $id]); $queryData->setOnErrorMessage(__u('Error al actualizar preferencias')); - return DbWrapper::getQuery($queryData, $this->db); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -728,6 +713,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param $groupId * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUserEmailForGroup($groupId) { @@ -745,7 +732,7 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams([$groupId, $groupId]); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -754,6 +741,8 @@ class UserRepository extends Repository implements RepositoryItemInterface * @param int $id * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUsageForUser($id) { @@ -798,6 +787,6 @@ class UserRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams(array_fill(0, 5, (int)$id)); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/UserGroup/UserGroupRepository.php b/lib/SP/Repositories/UserGroup/UserGroupRepository.php index 36a71f6d..fa4819af 100644 --- a/lib/SP/Repositories/UserGroup/UserGroupRepository.php +++ b/lib/SP/Repositories/UserGroup/UserGroupRepository.php @@ -33,8 +33,8 @@ use SP\Repositories\DuplicatedItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class UserGroupRepository @@ -51,7 +51,8 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * @param $id * * @return int - * @throws SPException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -60,9 +61,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar el grupo')); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -87,9 +86,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams([(int)$id, (int)$id]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -98,6 +95,8 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * @param $id int * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUsage($id) { @@ -118,7 +117,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->addParams(array_fill(0, 4, (int)$id)); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -127,6 +126,8 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * @param $id int * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUsageByUsers($id) { @@ -151,7 +152,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->addParams([(int)$id, (int)$id]); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -159,7 +160,9 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * * @param int $id * - * @return mixed + * @return UserGroupData + * @throws ConstraintException + * @throws QueryException */ public function getById($id) { @@ -168,7 +171,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id, `name`, description FROM UserGroup WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** @@ -177,6 +180,8 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * @param string $name * * @return UserGroupData + * @throws ConstraintException + * @throws QueryException */ public function getByName($name) { @@ -185,13 +190,15 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT id, `name`, description FROM UserGroup WHERE name = ? LIMIT 1'); $queryData->addParam($name); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return UserGroupData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -199,7 +206,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setMapClassName(UserGroupData::class); $queryData->setQuery('SELECT id, `name`, description FROM UserGroup ORDER BY name'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -208,6 +215,8 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * @param array $ids * * @return UserGroupData[] + * @throws ConstraintException + * @throws QueryException */ public function getByIdBatch(array $ids) { @@ -223,7 +232,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -241,9 +250,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('DELETE FROM UserGroup WHERE id IN (' . $this->getParamsFromArray($ids) . ')'); $queryData->setParams($ids); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -251,7 +258,9 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * * @param ItemSearchData $SearchData * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $SearchData) { @@ -273,13 +282,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -306,9 +309,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setParams([$itemData->getName(), $itemData->getDescription()]); $queryData->setOnErrorMessage(__u('Error al crear el grupo')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -326,9 +327,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT `name` FROM UserGroup WHERE UPPER(`name`) = UPPER(?)'); $queryData->addParam($itemData->getName()); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -356,9 +355,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface ]); $queryData->setOnErrorMessage(__u('Error al actualizar el grupo')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -376,8 +373,6 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface $queryData->setQuery('SELECT `name` FROM UserGroup WHERE UPPER(`name`) = UPPER(?) AND id <> ?'); $queryData->setParams([$itemData->getName(), $itemData->getId()]); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } } \ No newline at end of file diff --git a/lib/SP/Repositories/UserGroup/UserToUserGroupRepository.php b/lib/SP/Repositories/UserGroup/UserToUserGroupRepository.php index cd5abd9f..08dae784 100644 --- a/lib/SP/Repositories/UserGroup/UserToUserGroupRepository.php +++ b/lib/SP/Repositories/UserGroup/UserToUserGroupRepository.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -27,8 +27,7 @@ namespace SP\Repositories\UserGroup; use SP\DataModel\UserToUserGroupData; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; /** * Class UserToUserGroupRepository @@ -42,9 +41,12 @@ class UserToUserGroupRepository extends Repository /** * Checks whether the user is included in the group * - * @param $userId * @param $groupId + * @param $userId + * * @return bool + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function checkUserInGroup($groupId, $userId) { @@ -52,16 +54,17 @@ class UserToUserGroupRepository extends Repository $queryData->setQuery('SELECT userGroupId FROM UserToUserGroup WHERE userGroupId = ? AND userId = ?'); $queryData->setParams([$groupId, $userId]); - DbWrapper::getResults($queryData, $this->db); - - return ($queryData->getQueryNumRows() === 1); + return $this->db->doSelect($queryData)->getNumRows() === 1; } /** * Returns the groups which the user belongs to * * @param $userId + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getGroupsForUser($userId) { @@ -69,7 +72,7 @@ class UserToUserGroupRepository extends Repository $queryData->setQuery('SELECT userGroupId FROM UserToUserGroup WHERE userId = ?'); $queryData->addParam($userId); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -77,6 +80,7 @@ class UserToUserGroupRepository extends Repository * * @param int $id * @param array $users + * * @return UserToUserGroupRepository * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -91,6 +95,7 @@ class UserToUserGroupRepository extends Repository * Deletes users from a group * * @param $id int + * * @return $this * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -102,7 +107,7 @@ class UserToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar los usuarios del grupo')); - DbWrapper::getQuery($queryData, $this->db); + $this->db->doQuery($queryData); return $this; } @@ -112,6 +117,7 @@ class UserToUserGroupRepository extends Repository * * @param int $groupId * @param array $users + * * @return UserToUserGroupRepository * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -131,7 +137,7 @@ class UserToUserGroupRepository extends Repository $queryData->setOnErrorMessage(__u('Error al asignar los usuarios al grupo')); - DbWrapper::getQuery($queryData, $this->db); + $this->db->doQuery($queryData); return $this; } @@ -140,7 +146,10 @@ class UserToUserGroupRepository extends Repository * Returns users from a group by group Id * * @param $id int + * * @return UserToUserGroupData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -149,6 +158,6 @@ class UserToUserGroupRepository extends Repository $queryData->setQuery('SELECT userGroupId, userId FROM UserToUserGroup WHERE userGroupId = ?'); $queryData->addParam($id); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } } \ No newline at end of file diff --git a/lib/SP/Repositories/UserProfile/UserProfileRepository.php b/lib/SP/Repositories/UserProfile/UserProfileRepository.php index 683590d4..18fb858a 100644 --- a/lib/SP/Repositories/UserProfile/UserProfileRepository.php +++ b/lib/SP/Repositories/UserProfile/UserProfileRepository.php @@ -32,8 +32,8 @@ use SP\Repositories\DuplicatedItemException; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class UserProfileRepository @@ -50,6 +50,8 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * @param $id int El id del perfil * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUsersForProfile($id) { @@ -57,7 +59,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('SELECT login FROM User WHERE userProfileId = ?'); $queryData->addParam($id); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -76,9 +78,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->addParam($id); $queryData->setOnErrorMessage(__u('Error al eliminar perfil')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -96,9 +96,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('SELECT userProfileId FROM User WHERE userProfileId = ?'); $queryData->addParam($id); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doSelect($queryData)->getNumRows() > 0; } /** @@ -107,6 +105,8 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * @param int $id * * @return UserProfileData + * @throws ConstraintException + * @throws QueryException */ public function getById($id) { @@ -115,13 +115,15 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('SELECT id, `name`, `profile` FROM UserProfile WHERE id = ? LIMIT 1'); $queryData->addParam($id); - return DbWrapper::getResults($queryData, $this->db); + return $this->db->doSelect($queryData)->getData(); } /** * Returns all the items * * @return UserProfileData[] + * @throws ConstraintException + * @throws QueryException */ public function getAll() { @@ -129,7 +131,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setMapClassName(UserProfileData::class); $queryData->setQuery('SELECT id, `name` FROM UserProfile ORDER BY `name`'); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -138,6 +140,8 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * @param array $ids * * @return UserProfileData[] + * @throws ConstraintException + * @throws QueryException */ public function getByIdBatch(array $ids) { @@ -153,7 +157,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery($query); $queryData->setParams($ids); - return DbWrapper::getResultsArray($queryData, $this->db); + return $this->db->doSelect($queryData)->getDataAsArray(); } /** @@ -172,9 +176,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setParams($ids); $queryData->setOnErrorMessage(__u('Error al eliminar los perfiles')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -182,7 +184,9 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * * @param ItemSearchData $SearchData * - * @return mixed + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $SearchData) { @@ -202,13 +206,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->addParam($SearchData->getLimitStart()); $queryData->addParam($SearchData->getLimitCount()); - DbWrapper::setFullRowCount(); - - $queryRes = DbWrapper::getResultsArray($queryData, $this->db); - - $queryRes['count'] = $queryData->getQueryNumRows(); - - return $queryRes; + return $this->db->doSelect($queryData, true); } /** @@ -235,9 +233,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac ]); $queryData->setOnErrorMessage(__u('Error al crear perfil')); - DbWrapper::getQuery($queryData, $this->db); - - return $this->db->getLastId(); + return $this->db->doQuery($queryData)->getLastId(); } /** @@ -255,9 +251,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac $queryData->setQuery('SELECT `name` FROM UserProfile WHERE UPPER(`name`) = ?'); $queryData->addParam($itemData->getName()); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doQuery($queryData)->getNumRows() > 0; } /** @@ -288,9 +282,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac ]); $queryData->setOnErrorMessage(__u('Error al modificar perfil')); - DbWrapper::getQuery($queryData, $this->db); - - return $queryData->getQueryNumRows() > 0; + return $this->db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -317,8 +309,6 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac ]); $queryData->setQuery($query); - DbWrapper::getQuery($queryData, $this->db); - - return ($queryData->getQueryNumRows() > 0); + return $this->db->doSelect($queryData)->getNumRows() > 0; } } \ No newline at end of file diff --git a/lib/SP/Services/Account/AccountFileService.php b/lib/SP/Services/Account/AccountFileService.php index 8328b515..8a714857 100644 --- a/lib/SP/Services/Account/AccountFileService.php +++ b/lib/SP/Services/Account/AccountFileService.php @@ -31,6 +31,7 @@ use SP\DataModel\ItemSearchData; use SP\Repositories\Account\AccountFileRepository; use SP\Services\Service; use SP\Services\ServiceException; +use SP\Storage\Database\QueryResult; use SP\Util\FileUtil; use SP\Util\ImageUtil; @@ -77,7 +78,10 @@ class AccountFileService extends Service /** * @param $id + * * @return FileExtData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getInfoById($id) { @@ -88,7 +92,10 @@ class AccountFileService extends Service * Returns the item for given id * * @param int $id + * * @return FileExtData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -99,6 +106,8 @@ class AccountFileService extends Service * Returns all the items * * @return FileExtData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -109,7 +118,10 @@ class AccountFileService extends Service * Returns all the items for given ids * * @param array $ids + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { @@ -155,7 +167,10 @@ class AccountFileService extends Service * Searches for items by a given filter * * @param ItemSearchData $searchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $searchData) { @@ -166,7 +181,10 @@ class AccountFileService extends Service * Returns the item for given id * * @param int $id + * * @return FileData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByAccountId($id) { diff --git a/lib/SP/Services/Account/AccountHistoryService.php b/lib/SP/Services/Account/AccountHistoryService.php index 380c9771..8f39a8c4 100644 --- a/lib/SP/Services/Account/AccountHistoryService.php +++ b/lib/SP/Services/Account/AccountHistoryService.php @@ -33,6 +33,7 @@ use SP\Repositories\Account\AccountHistoryRepository; use SP\Repositories\Account\AccountToUserGroupRepository; use SP\Repositories\Account\AccountToUserRepository; use SP\Services\Service; +use SP\Storage\Database\QueryResult; /** * Class AccountHistoryService @@ -85,6 +86,8 @@ class AccountHistoryService extends Service * @param $id * * @return array Con los registros con id como clave y fecha - usuario como valor + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getHistoryForAccount($id) { @@ -120,6 +123,8 @@ class AccountHistoryService extends Service * @param $id * * @return ItemData[] + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getUsersByAccountId($id) { @@ -130,6 +135,8 @@ class AccountHistoryService extends Service * @param $id * * @return ItemData[] + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getUserGroupsByAccountId($id) { @@ -139,7 +146,9 @@ class AccountHistoryService extends Service /** * @param ItemSearchData $itemSearchData * - * @return mixed + * @return QueryResult + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function search(ItemSearchData $itemSearchData) { @@ -163,6 +172,8 @@ class AccountHistoryService extends Service /** * @return array + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getAccountsPassData() { @@ -212,6 +223,8 @@ class AccountHistoryService extends Service * Returns all the items * * @return array + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getAll() { diff --git a/lib/SP/Services/Account/AccountSearchService.php b/lib/SP/Services/Account/AccountSearchService.php index fb2a59bf..fd50c791 100644 --- a/lib/SP/Services/Account/AccountSearchService.php +++ b/lib/SP/Services/Account/AccountSearchService.php @@ -39,6 +39,7 @@ use SP\Repositories\Account\AccountToUserRepository; use SP\Services\Service; use SP\Services\User\UserService; use SP\Services\UserGroup\UserGroupService; +use SP\Storage\Database\QueryResult; use SP\Storage\FileCache; use SP\Storage\FileException; @@ -127,9 +128,11 @@ class AccountSearchService extends Service * a mostrar. * * @param AccountSearchFilter $accountSearchFilter - * @return array - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException */ public function processSearchResults(AccountSearchFilter $accountSearchFilter) { @@ -147,6 +150,8 @@ class AccountSearchService extends Service $accountAclService = $this->dic->get(AccountAclService::class); + $accountsData = []; + foreach ($accountSearchResponse->getData() as $accountSearchData) { $cache = $this->getCacheForAccount($accountSearchData); @@ -178,9 +183,7 @@ class AccountSearchService extends Service $accountsData[] = $accountsSearchItem; } - $accountsData['count'] = $accountSearchResponse->getCount(); - - return $accountsData; + return (new QueryResult($accountsData))->setTotalNumRows($accountSearchResponse->getCount()); } /** @@ -188,6 +191,7 @@ class AccountSearchService extends Service * QueryCondition con los filtros * * @param $string + * * @return QueryCondition * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface @@ -290,6 +294,7 @@ class AccountSearchService extends Service * Devolver los accesos desde la caché * * @param AccountSearchVData $accountSearchData + * * @return AccountCache */ protected function getCacheForAccount(AccountSearchVData $accountSearchData) @@ -317,6 +322,7 @@ class AccountSearchService extends Service * Seleccionar un color para la cuenta * * @param int $id El id del elemento a asignar + * * @return string */ private function pickAccountColor($id) diff --git a/lib/SP/Services/Account/AccountService.php b/lib/SP/Services/Account/AccountService.php index 45166954..4be1c4a8 100644 --- a/lib/SP/Services/Account/AccountService.php +++ b/lib/SP/Services/Account/AccountService.php @@ -45,6 +45,7 @@ use SP\Services\Config\ConfigService; use SP\Services\Service; use SP\Services\ServiceException; use SP\Services\ServiceItemTrait; +use SP\Storage\Database\QueryResult; /** * Class AccountService @@ -90,6 +91,7 @@ class AccountService extends Service implements AccountServiceInterface * @return AccountDetailsResponse * @throws QueryException * @throws \SP\Repositories\NoSuchItemException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getById($id) { @@ -98,9 +100,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param AccountDetailsResponse $accountDetailsResponse + * * @return AccountService - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function withUsersById(AccountDetailsResponse $accountDetailsResponse) { @@ -111,7 +114,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param AccountDetailsResponse $accountDetailsResponse + * * @return AccountService + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function withUserGroupsById(AccountDetailsResponse $accountDetailsResponse) { @@ -122,7 +128,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param AccountDetailsResponse $accountDetailsResponse + * * @return AccountService + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function withTagsById(AccountDetailsResponse $accountDetailsResponse) { @@ -133,6 +142,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $id + * * @return bool * @throws QueryException * @throws \SP\Core\Exceptions\ConstraintException @@ -144,6 +154,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $id + * * @return bool * @throws QueryException * @throws \SP\Core\Exceptions\ConstraintException @@ -155,7 +166,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $id + * * @return \SP\DataModel\AccountPassData + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getPasswordForId($id) { @@ -164,6 +178,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param AccountRequest $accountRequest + * * @return int * @throws QueryException * @throws SPException @@ -193,6 +208,7 @@ class AccountService extends Service implements AccountServiceInterface * * @param string $pass * @param string $masterPass Clave maestra a utilizar + * * @return array * @throws ServiceException */ @@ -252,6 +268,7 @@ class AccountService extends Service implements AccountServiceInterface * Updates external items for the account * * @param AccountRequest $accountRequest + * * @throws QueryException * @throws SPException * @throws \Psr\Container\ContainerExceptionInterface @@ -277,6 +294,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param int $accountId * @param bool $isDelete + * * @return bool * @throws QueryException * @throws \Psr\Container\ContainerExceptionInterface @@ -351,6 +369,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param AccountRequest $accountRequest * @param bool $addHistory + * * @throws SPException * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface @@ -373,6 +392,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param AccountPasswordRequest $accountRequest + * * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException */ @@ -385,6 +405,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $historyId * @param $accountId + * * @throws QueryException * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface @@ -400,6 +421,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $id + * * @return AccountService * @throws SPException * @throws ServiceException @@ -415,6 +437,7 @@ class AccountService extends Service implements AccountServiceInterface /** * @param array $ids + * * @return AccountService * @throws SPException * @throws ServiceException @@ -430,7 +453,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $accountId + * * @return array + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getForUser($accountId = null) { @@ -445,7 +471,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $accountId + * * @return array + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getLinked($accountId) { @@ -457,7 +486,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param $id + * * @return AccountPassData + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getPasswordHistoryForId($id) { @@ -469,6 +501,8 @@ class AccountService extends Service implements AccountServiceInterface /** * @return AccountData[] + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getAllBasic() { @@ -477,7 +511,10 @@ class AccountService extends Service implements AccountServiceInterface /** * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function search(ItemSearchData $itemSearchData) { @@ -488,6 +525,8 @@ class AccountService extends Service implements AccountServiceInterface * Devolver el número total de cuentas * * @return \stdClass + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getTotalNumAccounts() { @@ -502,6 +541,7 @@ class AccountService extends Service implements AccountServiceInterface * @return \SP\DataModel\AccountExtData * @throws QueryException * @throws \SP\Repositories\NoSuchItemException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getDataForLink($id) { @@ -512,6 +552,8 @@ class AccountService extends Service implements AccountServiceInterface * Obtener los datos relativos a la clave de todas las cuentas. * * @return array Con los datos de la clave + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getAccountsPassData() { @@ -522,7 +564,11 @@ class AccountService extends Service implements AccountServiceInterface * Obtener las cuentas de una búsqueda. * * @param AccountSearchFilter $accountSearchFilter + * * @return AccountSearchResponse + * @throws QueryException + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getByFilter(AccountSearchFilter $accountSearchFilter) { diff --git a/lib/SP/Services/AuthToken/AuthTokenService.php b/lib/SP/Services/AuthToken/AuthTokenService.php index f552a61e..d30dd921 100644 --- a/lib/SP/Services/AuthToken/AuthTokenService.php +++ b/lib/SP/Services/AuthToken/AuthTokenService.php @@ -36,6 +36,7 @@ use SP\Repositories\AuthToken\AuthTokenRepository; use SP\Services\Service; use SP\Services\ServiceException; use SP\Services\ServiceItemTrait; +use SP\Storage\Database\QueryResult; use SP\Util\Util; /** @@ -89,7 +90,10 @@ class AuthTokenService extends Service /** * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -98,7 +102,10 @@ class AuthTokenService extends Service /** * @param $id + * * @return mixed + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -107,6 +114,7 @@ class AuthTokenService extends Service /** * @param $id + * * @return AuthTokenService * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException @@ -125,6 +133,7 @@ class AuthTokenService extends Service * Deletes all the items for given ids * * @param array $ids + * * @return bool * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -141,6 +150,7 @@ class AuthTokenService extends Service /** * @param $itemData + * * @return mixed * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException @@ -150,18 +160,20 @@ class AuthTokenService extends Service */ public function create($itemData) { - $this->injectSecureData($itemData); - - return $this->authTokenRepository->create($itemData); + return $this->authTokenRepository->create($this->injectSecureData($itemData)); } /** * Injects secure data for token * * @param AuthTokenData $authTokenData - * @param string $token + * @param string $token + * + * @return AuthTokenData * @throws \Defuse\Crypto\Exception\CryptoException * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ private function injectSecureData(AuthTokenData $authTokenData, $token = null) { @@ -182,6 +194,8 @@ class AuthTokenService extends Service $authTokenData->setToken($token); $authTokenData->setCreatedBy($this->context->getUserData()->getId()); + + return $authTokenData; } /** @@ -199,18 +213,20 @@ class AuthTokenService extends Service * Generar la llave segura del token * * @param string $token - * @param string $hash + * @param string $key + * * @return Vault * @throws \Defuse\Crypto\Exception\CryptoException */ - private function getSecureData($token, $hash) + private function getSecureData($token, $key) { return (new Vault()) - ->saveData(CryptSession::getSessionKey($this->context), $hash . $token); + ->saveData(CryptSession::getSessionKey($this->context), $key . $token); } /** * @param AuthTokenData $itemData + * * @return mixed * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException @@ -232,6 +248,7 @@ class AuthTokenService extends Service /** * @param AuthTokenData $itemData * @param string $token + * * @return mixed * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException @@ -240,9 +257,7 @@ class AuthTokenService extends Service */ public function update(AuthTokenData $itemData, $token = null) { - $this->injectSecureData($itemData, $token); - - return $this->authTokenRepository->update($itemData); + return $this->authTokenRepository->update($this->injectSecureData($itemData, $token)); } /** @@ -250,7 +265,10 @@ class AuthTokenService extends Service * * @param $actionId int El id de la accion * @param $token string El token de seguridad + * * @return false|AuthTokenData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getTokenByToken($actionId, $token) { diff --git a/lib/SP/Services/Backup/FileBackupService.php b/lib/SP/Services/Backup/FileBackupService.php index 919a20d0..6a083483 100644 --- a/lib/SP/Services/Backup/FileBackupService.php +++ b/lib/SP/Services/Backup/FileBackupService.php @@ -33,11 +33,10 @@ use SP\Core\Events\EventMessage; use SP\Core\Exceptions\SPException; use SP\Services\Service; use SP\Services\ServiceException; -use SP\Storage\Database; -use SP\Storage\DBUtil; -use SP\Storage\DbWrapper; +use SP\Storage\Database\Database; +use SP\Storage\Database\DBUtil; +use SP\Storage\Database\QueryData; use SP\Storage\FileHandler; -use SP\Storage\QueryData; use SP\Util\Checks; use SP\Util\Util; @@ -175,7 +174,7 @@ class FileBackupService extends Service $queryData->setQuery('SHOW CREATE TABLE ' . $tableName); // Consulta para crear la tabla - $txtCreate = DbWrapper::getResults($queryData, $db); + $txtCreate = $db->doQuery($queryData); if (isset($txtCreate->{'Create Table'})) { $sqlOut = '-- ' . PHP_EOL; @@ -209,7 +208,7 @@ class FileBackupService extends Service $queryData->setQuery('SELECT * FROM `' . $tableName . '`'); // Consulta para obtener los registros de la tabla - $queryRes = DbWrapper::getResultsRaw($queryData, $db); + $queryRes = $db->doQueryRaw($queryData); $numColumns = $queryRes->columnCount(); diff --git a/lib/SP/Services/Category/CategoryService.php b/lib/SP/Services/Category/CategoryService.php index 6971a8f4..c7f39739 100644 --- a/lib/SP/Services/Category/CategoryService.php +++ b/lib/SP/Services/Category/CategoryService.php @@ -32,6 +32,7 @@ use SP\Repositories\DuplicatedItemException; use SP\Services\Service; use SP\Services\ServiceException; use SP\Services\ServiceItemTrait; +use SP\Storage\Database\QueryResult; /** * Class CategoryService @@ -58,7 +59,10 @@ class CategoryService extends Service /** * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -67,7 +71,10 @@ class CategoryService extends Service /** * @param int $id + * * @return CategoryData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -78,7 +85,10 @@ class CategoryService extends Service * Returns the item for given id * * @param string $name + * * @return CategoryData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByName($name) { @@ -87,6 +97,7 @@ class CategoryService extends Service /** * @param $id + * * @return $this * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException @@ -105,6 +116,7 @@ class CategoryService extends Service * Deletes all the items for given ids * * @param array $ids + * * @return int * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -121,6 +133,7 @@ class CategoryService extends Service /** * @param $itemData + * * @return int * @throws SPException * @throws DuplicatedItemException @@ -132,6 +145,7 @@ class CategoryService extends Service /** * @param $itemData + * * @return mixed * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException @@ -146,6 +160,8 @@ class CategoryService extends Service * Get all items from the service's repository * * @return CategoryData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { diff --git a/lib/SP/Services/Client/ClientService.php b/lib/SP/Services/Client/ClientService.php index 12a78e67..12d6a5a7 100644 --- a/lib/SP/Services/Client/ClientService.php +++ b/lib/SP/Services/Client/ClientService.php @@ -51,7 +51,10 @@ class ClientService extends Service /** * @param ItemSearchData $itemSearchData - * @return \SP\DataModel\ClientData[] + * + * @return \SP\Storage\Database\QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -60,7 +63,10 @@ class ClientService extends Service /** * @param int $id + * * @return ClientData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -71,7 +77,10 @@ class ClientService extends Service * Returns the item for given name * * @param string $name + * * @return ClientData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByName($name) { @@ -135,6 +144,8 @@ class ClientService extends Service * Get all items from the service's repository * * @return ClientData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { @@ -146,6 +157,7 @@ class ClientService extends Service * * @return ItemData[] * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getAllForUser() { diff --git a/lib/SP/Services/Crypt/MasterPassService.php b/lib/SP/Services/Crypt/MasterPassService.php index 1fd1d6df..cc63758a 100644 --- a/lib/SP/Services/Crypt/MasterPassService.php +++ b/lib/SP/Services/Crypt/MasterPassService.php @@ -32,8 +32,8 @@ use SP\Services\Config\ConfigService; use SP\Services\CustomField\CustomFieldCryptService; use SP\Services\Service; use SP\Services\ServiceException; -use SP\Storage\Database; -use SP\Storage\DbWrapper; +use SP\Storage\Database\Database; +use SP\Storage\Database\DbWrapper; /** * Class MasterPassService diff --git a/lib/SP/Services/CustomField/CustomFieldDefService.php b/lib/SP/Services/CustomField/CustomFieldDefService.php index 58169b07..4156c60c 100644 --- a/lib/SP/Services/CustomField/CustomFieldDefService.php +++ b/lib/SP/Services/CustomField/CustomFieldDefService.php @@ -48,6 +48,7 @@ class CustomFieldDefService extends Service /** * @param $id + * * @return mixed */ public static function getFieldModuleById($id) @@ -77,7 +78,10 @@ class CustomFieldDefService extends Service /** * @param ItemSearchData $itemSearchData - * @return \SP\DataModel\CustomFieldDefinitionData[] + * + * @return \SP\Storage\Database\QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -86,7 +90,10 @@ class CustomFieldDefService extends Service /** * @param $id + * * @return \SP\DataModel\CustomFieldDefinitionData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -95,6 +102,7 @@ class CustomFieldDefService extends Service /** * @param $id + * * @return CustomFieldDefService * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -115,6 +123,7 @@ class CustomFieldDefService extends Service * Deletes all the items for given ids * * @param array $ids + * * @return int * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -135,6 +144,7 @@ class CustomFieldDefService extends Service /** * @param $itemData + * * @return mixed * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -146,6 +156,7 @@ class CustomFieldDefService extends Service /** * @param $itemData + * * @return mixed * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -159,6 +170,8 @@ class CustomFieldDefService extends Service * Get all items from the service's repository * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { diff --git a/lib/SP/Services/EventLog/EventlogService.php b/lib/SP/Services/EventLog/EventlogService.php index cd45cf61..6bfa5a3f 100644 --- a/lib/SP/Services/EventLog/EventlogService.php +++ b/lib/SP/Services/EventLog/EventlogService.php @@ -28,6 +28,7 @@ use SP\DataModel\EventlogData; use SP\DataModel\ItemSearchData; use SP\Repositories\EventLog\EventlogRepository; use SP\Services\Service; +use SP\Storage\Database\QueryResult; use SP\Util\HttpUtil; /** @@ -44,7 +45,10 @@ class EventlogService extends Service /** * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { diff --git a/lib/SP/Services/Import/ImportService.php b/lib/SP/Services/Import/ImportService.php index 07c14474..1b20d3a2 100644 --- a/lib/SP/Services/Import/ImportService.php +++ b/lib/SP/Services/Import/ImportService.php @@ -28,8 +28,8 @@ namespace SP\Services\Import; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Services\Service; -use SP\Storage\Database; -use SP\Storage\DbWrapper; +use SP\Storage\Database\Database; +use SP\Storage\Database\DbWrapper; defined('APP_ROOT') || die(); diff --git a/lib/SP/Services/Install/Installer.php b/lib/SP/Services/Install/Installer.php index 86b51c31..8bc78ae2 100644 --- a/lib/SP/Services/Install/Installer.php +++ b/lib/SP/Services/Install/Installer.php @@ -39,7 +39,7 @@ use SP\Services\Service; use SP\Services\User\UserService; use SP\Services\UserGroup\UserGroupService; use SP\Services\UserProfile\UserProfileService; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; use SP\Util\Util; defined('APP_ROOT') || die(); @@ -56,7 +56,7 @@ class Installer extends Service */ const VERSION = [3, 0, 0]; const VERSION_TEXT = '3.0-beta'; - const BUILD = 18060701; + const BUILD = 18061201; /** * @var ConfigService diff --git a/lib/SP/Services/Install/MySQL.php b/lib/SP/Services/Install/MySQL.php index 890af3a9..9846b421 100644 --- a/lib/SP/Services/Install/MySQL.php +++ b/lib/SP/Services/Install/MySQL.php @@ -27,11 +27,11 @@ namespace SP\Services\Install; use PDOException; use SP\Config\ConfigData; use SP\Core\Exceptions\SPException; -use SP\Storage\DatabaseConnectionData; -use SP\Storage\DBUtil; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Storage\Database\DBUtil; +use SP\Storage\Database\MySQLFileParser; +use SP\Storage\Database\MySQLHandler; use SP\Storage\FileHandler; -use SP\Storage\MySQLFileParser; -use SP\Storage\MySQLHandler; use SP\Util\Util; /** @@ -46,7 +46,7 @@ class MySQL implements DatabaseSetupInterface */ protected $installData; /** - * @var MySQLHandler + * @var \SP\Storage\Database\MySQLHandler */ protected $dbs; /** diff --git a/lib/SP/Services/Notification/NotificationService.php b/lib/SP/Services/Notification/NotificationService.php index e635d9e5..a6630b47 100644 --- a/lib/SP/Services/Notification/NotificationService.php +++ b/lib/SP/Services/Notification/NotificationService.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -29,6 +29,7 @@ use SP\DataModel\NotificationData; use SP\Repositories\Notification\NotificationRepository; use SP\Services\Service; use SP\Services\ServiceException; +use SP\Storage\Database\QueryResult; /** * Class NotificationService @@ -46,6 +47,7 @@ class NotificationService extends Service * Creates an item * * @param NotificationData $itemData + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -59,6 +61,7 @@ class NotificationService extends Service * Updates an item * * @param NotificationData $itemData + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -72,7 +75,10 @@ class NotificationService extends Service * Devolver los elementos con los ids especificados * * @param array $ids - * @return mixed + * + * @return NotificationData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { @@ -83,6 +89,7 @@ class NotificationService extends Service * Deletes an item preserving the sticky ones * * @param $id + * * @return NotificationService * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -101,6 +108,7 @@ class NotificationService extends Service * Deletes an item * * @param $id + * * @return NotificationService * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -119,6 +127,7 @@ class NotificationService extends Service * Deletes an item * * @param array $ids + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -137,6 +146,7 @@ class NotificationService extends Service * Deletes all the items for given ids * * @param array $ids + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -155,7 +165,10 @@ class NotificationService extends Service * Returns the item for given id * * @param int $id + * * @return NotificationData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -166,6 +179,8 @@ class NotificationService extends Service * Returns all the items * * @return NotificationData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -176,6 +191,7 @@ class NotificationService extends Service * Marcar una notificación como leída * * @param $id + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -190,7 +206,10 @@ class NotificationService extends Service * * @param $component * @param $id + * * @return NotificationData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getForUserIdByDate($component, $id) { @@ -199,7 +218,10 @@ class NotificationService extends Service /** * @param $id + * * @return NotificationData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllForUserId($id) { @@ -208,7 +230,10 @@ class NotificationService extends Service /** * @param $id + * * @return NotificationData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllActiveForUserId($id) { @@ -219,7 +244,10 @@ class NotificationService extends Service * Searches for items by a given filter * * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -237,7 +265,10 @@ class NotificationService extends Service * * @param ItemSearchData $itemSearchData * @param int $userId + * * @return mixed + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function searchForUserId(ItemSearchData $itemSearchData, $userId) { diff --git a/lib/SP/Services/Plugin/PluginService.php b/lib/SP/Services/Plugin/PluginService.php index 2d9f433c..4b74cf89 100644 --- a/lib/SP/Services/Plugin/PluginService.php +++ b/lib/SP/Services/Plugin/PluginService.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -31,9 +31,11 @@ use SP\DataModel\PluginData; use SP\Repositories\Plugin\PluginRepository; use SP\Services\Service; use SP\Services\ServiceException; +use SP\Storage\Database\QueryResult; /** * Class PluginService + * * @package SP\Services\Plugin */ class PluginService extends Service @@ -47,6 +49,7 @@ class PluginService extends Service * Creates an item * * @param PluginData $itemData + * * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -60,6 +63,7 @@ class PluginService extends Service * Updates an item * * @param PluginData $itemData + * * @return mixed * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -73,7 +77,10 @@ class PluginService extends Service * Returns the item for given id * * @param int $id + * * @return PluginData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -84,6 +91,8 @@ class PluginService extends Service * Returns all the items * * @return PluginData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAll() { @@ -94,7 +103,10 @@ class PluginService extends Service * Returns all the items for given ids * * @param array $ids + * * @return PluginData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { @@ -105,6 +117,7 @@ class PluginService extends Service * Deletes all the items for given ids * * @param array $ids + * * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -119,6 +132,7 @@ class PluginService extends Service * Deletes an item * * @param $id + * * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -134,7 +148,10 @@ class PluginService extends Service * Searches for items by a given filter * * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -145,7 +162,10 @@ class PluginService extends Service * Devuelve los datos de un plugin por su nombre * * @param string $name + * * @return PluginData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByName($name) { @@ -157,6 +177,7 @@ class PluginService extends Service * * @param $id * @param $enabled + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -171,6 +192,7 @@ class PluginService extends Service * * @param $name * @param $enabled + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -185,6 +207,7 @@ class PluginService extends Service * * @param $id * @param $available + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -199,6 +222,7 @@ class PluginService extends Service * * @param $name * @param $available + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -212,6 +236,7 @@ class PluginService extends Service * Restablecer los datos de un plugin * * @param int $id Id del plugin + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -225,6 +250,8 @@ class PluginService extends Service * Devolver los plugins activados * * @return ItemData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getEnabled() { diff --git a/lib/SP/Services/PublicLink/PublicLinkService.php b/lib/SP/Services/PublicLink/PublicLinkService.php index ff807eb0..2123328e 100644 --- a/lib/SP/Services/PublicLink/PublicLinkService.php +++ b/lib/SP/Services/PublicLink/PublicLinkService.php @@ -38,6 +38,7 @@ use SP\Services\Account\AccountService; use SP\Services\Service; use SP\Services\ServiceException; use SP\Services\ServiceItemTrait; +use SP\Storage\Database\QueryResult; use SP\Util\Checks; use SP\Util\HttpUtil; use SP\Util\Util; @@ -64,6 +65,7 @@ class PublicLinkService extends Service * Returns an HTTP URL for given hash * * @param $hash + * * @return string */ public static function getLinkForHash($hash) @@ -83,7 +85,10 @@ class PublicLinkService extends Service /** * @param ItemSearchData $itemSearchData - * @return mixed + * + * @return QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -92,6 +97,7 @@ class PublicLinkService extends Service /** * @param $id + * * @return \SP\DataModel\PublicLinkData * @throws \SP\Core\Exceptions\SPException */ @@ -102,6 +108,7 @@ class PublicLinkService extends Service /** * @param $id + * * @return bool * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException @@ -127,6 +134,7 @@ class PublicLinkService extends Service /** * @param string $salt + * * @return string * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ @@ -140,6 +148,7 @@ class PublicLinkService extends Service * * @param string $key * @param string $salt + * * @return mixed */ public static function getHashForKey($key, $salt) @@ -152,6 +161,7 @@ class PublicLinkService extends Service * * @param int $itemId * @param string $linkKey + * * @return Vault * @throws \Defuse\Crypto\Exception\CryptoException * @throws \Psr\Container\ContainerExceptionInterface @@ -175,6 +185,7 @@ class PublicLinkService extends Service * Devolver el tiempo de caducidad del enlace * * @param Config $config + * * @return int */ public static function calcDateExpire(Config $config) @@ -184,6 +195,7 @@ class PublicLinkService extends Service /** * @param $id + * * @return $this * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -202,6 +214,7 @@ class PublicLinkService extends Service * Deletes all the items for given ids * * @param array $ids + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -218,6 +231,7 @@ class PublicLinkService extends Service /** * @param PublicLinkData $itemData + * * @return int * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException @@ -239,6 +253,7 @@ class PublicLinkService extends Service /** * @param string $salt * @param PublicLinkData $publicLinkData + * * @return string */ public static function getKeyForHash($salt, PublicLinkData $publicLinkData) @@ -250,6 +265,8 @@ class PublicLinkService extends Service * Get all items from the service's repository * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { @@ -260,6 +277,7 @@ class PublicLinkService extends Service * Incrementar el contador de visitas de un enlace * * @param PublicLinkData $publicLinkData + * * @return bool * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -292,6 +310,7 @@ class PublicLinkService extends Service * Actualizar la información de uso * * @param $hash + * * @return array */ public static function getUseInfo($hash) @@ -307,6 +326,7 @@ class PublicLinkService extends Service /** * @param $hash string + * * @return bool|PublicLinkData * @throws \SP\Core\Exceptions\SPException */ @@ -319,6 +339,7 @@ class PublicLinkService extends Service * Devolver el hash asociado a un elemento * * @param int $itemId + * * @return PublicLinkData * @throws SPException */ @@ -331,6 +352,7 @@ class PublicLinkService extends Service * Updates an item * * @param PublicLinkData $itemData + * * @return mixed * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException diff --git a/lib/SP/Services/Tag/TagService.php b/lib/SP/Services/Tag/TagService.php index 0a5c4ad2..da2679dd 100644 --- a/lib/SP/Services/Tag/TagService.php +++ b/lib/SP/Services/Tag/TagService.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -48,7 +48,10 @@ class TagService extends Service /** * @param ItemSearchData $itemSearchData - * @return \SP\DataModel\ClientData[] + * + * @return \SP\Storage\Database\QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -57,7 +60,10 @@ class TagService extends Service /** * @param $id - * @return mixed + * + * @return TagData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -66,8 +72,11 @@ class TagService extends Service /** * @param $id + * * @return $this - * @throws SPException + * @throws ServiceException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function delete($id) { @@ -80,6 +89,7 @@ class TagService extends Service /** * @param array $ids + * * @return $this * @throws SPException */ @@ -94,7 +104,8 @@ class TagService extends Service /** * @param $itemData - * @return mixed + * + * @return int * @throws SPException */ public function create($itemData) @@ -104,7 +115,8 @@ class TagService extends Service /** * @param $itemData - * @return mixed + * + * @return int * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -118,6 +130,8 @@ class TagService extends Service * Get all items from the service's repository * * @return TagData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { diff --git a/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php b/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php index a827a3ce..6206abb1 100644 --- a/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php +++ b/lib/SP/Services/Upgrade/UpgradeCustomFieldDefinition.php @@ -31,9 +31,9 @@ use SP\DataModel\CustomFieldDefinitionData; use SP\Services\CustomField\CustomFieldDefService; use SP\Services\Service; use SP\Services\ServiceException; -use SP\Storage\Database; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\Database; +use SP\Storage\Database\DbWrapper; +use SP\Storage\Database\QueryData; use SP\Util\Util; /** @@ -44,7 +44,7 @@ use SP\Util\Util; class UpgradeCustomFieldDefinition extends Service { /** - * @var Database + * @var \SP\Storage\Database\Database */ private $db; @@ -71,7 +71,7 @@ class UpgradeCustomFieldDefinition extends Service throw new ServiceException(__u('No es posible iniciar una transacción')); } - foreach (DbWrapper::getResultsArray($queryData, $this->db) as $item) { + foreach ($this->db->doSelect($queryData)->getDataAsArray() as $item) { /** @var CustomFieldDefDataOld $data */ $data = Util::unserialize(CustomFieldDefDataOld::class, $item->field, 'SP\DataModel\CustomFieldDefData'); diff --git a/lib/SP/Services/Upgrade/UpgradeDatabaseService.php b/lib/SP/Services/Upgrade/UpgradeDatabaseService.php index 25cba74e..b42cca85 100644 --- a/lib/SP/Services/Upgrade/UpgradeDatabaseService.php +++ b/lib/SP/Services/Upgrade/UpgradeDatabaseService.php @@ -28,12 +28,11 @@ use SP\Config\ConfigData; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Services\Service; -use SP\Storage\Database; -use SP\Storage\DbWrapper; +use SP\Storage\Database\Database; +use SP\Storage\Database\MySQLFileParser; +use SP\Storage\Database\QueryData; use SP\Storage\FileException; use SP\Storage\FileHandler; -use SP\Storage\MySQLFileParser; -use SP\Storage\QueryData; use SP\Util\Util; /** @@ -57,6 +56,7 @@ class UpgradeDatabaseService extends Service implements UpgradeInterface * Check if it needs to be upgraded * * @param $version + * * @return bool */ public static function needsUpgrade($version) @@ -67,8 +67,9 @@ class UpgradeDatabaseService extends Service implements UpgradeInterface /** * Inicia el proceso de actualización de la BBDD. * - * @param int $version con la versión de la BBDD actual + * @param int $version con la versión de la BBDD actual * @param ConfigData $configData + * * @return bool * @throws UpgradeException */ @@ -129,6 +130,7 @@ class UpgradeDatabaseService extends Service implements UpgradeInterface * Aplicar actualizaciones auxiliares antes de actualizar la BBDD * * @param $version + * * @return bool */ private function applyPreUpgrade($version) @@ -140,6 +142,7 @@ class UpgradeDatabaseService extends Service implements UpgradeInterface * Actualiza la BBDD según la versión. * * @param int $version con la versión a actualizar + * * @returns bool * @throws UpgradeException */ @@ -167,7 +170,8 @@ class UpgradeDatabaseService extends Service implements UpgradeInterface $queryData = new QueryData(); $queryData->setQuery($query); - DbWrapper::getQuery($queryData, $this->db); + + $this->db->doQuery($queryData); } catch (\Exception $e) { processException($e); debugLog('SQL: ' . $query); @@ -194,6 +198,7 @@ class UpgradeDatabaseService extends Service implements UpgradeInterface * Obtener las consultas de actualización desde un archivo * * @param $filename + * * @return array|bool * @throws UpgradeException */ diff --git a/lib/SP/Services/Upgrade/UpgradePublicLink.php b/lib/SP/Services/Upgrade/UpgradePublicLink.php index 3ad09cd6..57ac860d 100644 --- a/lib/SP/Services/Upgrade/UpgradePublicLink.php +++ b/lib/SP/Services/Upgrade/UpgradePublicLink.php @@ -31,9 +31,9 @@ use SP\DataModel\PublicLinkData; use SP\Services\PublicLink\PublicLinkService; use SP\Services\Service; use SP\Services\ServiceException; -use SP\Storage\Database; -use SP\Storage\DbWrapper; -use SP\Storage\QueryData; +use SP\Storage\Database\Database; +use SP\Storage\Database\DbWrapper; +use SP\Storage\Database\QueryData; use SP\Util\Util; /** @@ -71,7 +71,7 @@ class UpgradePublicLink extends Service throw new ServiceException(__u('No es posible iniciar una transacción')); } - foreach (DbWrapper::getResultsArray($queryData, $this->db) as $item) { + foreach ($this->db->doSelect($queryData)->getDataAsArray() as $item) { /** @var PublickLinkOldData $data */ $data = Util::unserialize(PublickLinkOldData::class, $item->data, 'SP\DataModel\PublicLinkData'); diff --git a/lib/SP/Services/User/UserService.php b/lib/SP/Services/User/UserService.php index 9e765454..65642f23 100644 --- a/lib/SP/Services/User/UserService.php +++ b/lib/SP/Services/User/UserService.php @@ -25,14 +25,18 @@ namespace SP\Services\User; use SP\Core\Crypt\Hash; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; use SP\DataModel\UserData; use SP\DataModel\UserPreferencesData; +use SP\Repositories\DuplicatedItemException; use SP\Repositories\User\UserRepository; use SP\Services\Service; use SP\Services\ServiceException; use SP\Services\ServiceItemTrait; +use SP\Storage\Database\QueryResult; use SP\Util\Util; /** @@ -55,6 +59,7 @@ class UserService extends Service /** * @param UserData $userData + * * @return UserLoginResponse */ public static function mapUserLoginResponse(UserData $userData) @@ -87,6 +92,7 @@ class UserService extends Service * Returns user's preferences object * * @param string $preferences + * * @return UserPreferencesData */ public static function getUserPreferences($preferences) @@ -102,9 +108,10 @@ class UserService extends Service * Actualiza el último inicio de sesión del usuario en la BBDD. * * @param $id int El id del usuario + * * @return bool - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\ConstraintException + * @throws QueryException + * @throws ConstraintException */ public function updateLastLoginById($id) { @@ -113,9 +120,10 @@ class UserService extends Service /** * @param $login + * * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function checkExistsByLogin($login) { @@ -126,6 +134,7 @@ class UserService extends Service * Returns the item for given id * * @param int $id + * * @return mixed * @throws SPException */ @@ -138,6 +147,7 @@ class UserService extends Service * Returns the item for given id * * @param $login + * * @return UserData * @throws SPException */ @@ -150,10 +160,11 @@ class UserService extends Service * Deletes an item * * @param $id + * * @return UserService * @throws SPException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function delete($id) { @@ -166,10 +177,11 @@ class UserService extends Service /** * @param array $ids + * * @return int * @throws ServiceException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ConstraintException + * @throws QueryException */ public function deleteByIdBatch(array $ids) { @@ -184,6 +196,7 @@ class UserService extends Service * Creates an item * * @param UserLoginRequest $userLoginRequest + * * @return mixed * @throws SPException */ @@ -213,6 +226,7 @@ class UserService extends Service * Creates an item * * @param UserData $itemData + * * @return int * @throws SPException */ @@ -229,6 +243,7 @@ class UserService extends Service * @param UserData $itemData * @param string $userPass * @param string $masterPass + * * @return int * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException @@ -249,7 +264,10 @@ class UserService extends Service * Searches for items by a given filter * * @param ItemSearchData $SearchData - * @return array + * + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function search(ItemSearchData $SearchData) { @@ -260,10 +278,11 @@ class UserService extends Service * Updates an item * * @param UserData $itemData - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * * @throws ServiceException - * @throws SPException + * @throws ConstraintException + * @throws QueryException + * @throws DuplicatedItemException */ public function update($itemData) { @@ -277,9 +296,10 @@ class UserService extends Service * * @param int $userId * @param string $pass - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updatePass($userId, $pass) { @@ -293,9 +313,10 @@ class UserService extends Service /** * @param $userId * @param UserPreferencesData $userPreferencesData - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updatePreferencesById($userId, UserPreferencesData $userPreferencesData) { @@ -304,9 +325,10 @@ class UserService extends Service /** * @param UserLoginRequest $userLoginRequest - * @return bool - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * + * @return int + * @throws ConstraintException + * @throws QueryException */ public function updateOnLogin(UserLoginRequest $userLoginRequest) { @@ -323,7 +345,9 @@ class UserService extends Service /** * Get all items from the service's repository * - * @return array + * @return UserData[] + * @throws ConstraintException + * @throws QueryException */ public function getAllBasic() { @@ -334,7 +358,10 @@ class UserService extends Service * Obtener el email de los usuarios de un grupo * * @param $groupId + * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUserEmailForGroup($groupId) { @@ -345,7 +372,10 @@ class UserService extends Service * Returns the usage of the given user's id * * @param int $id + * * @return array + * @throws ConstraintException + * @throws QueryException */ public function getUsageForUser($id) { diff --git a/lib/SP/Services/UserGroup/UserGroupService.php b/lib/SP/Services/UserGroup/UserGroupService.php index 312452c5..6ab14adc 100644 --- a/lib/SP/Services/UserGroup/UserGroupService.php +++ b/lib/SP/Services/UserGroup/UserGroupService.php @@ -53,7 +53,10 @@ class UserGroupService extends Service /** * @param ItemSearchData $itemSearchData - * @return \SP\DataModel\ClientData[] + * + * @return \SP\Storage\Database\QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -62,7 +65,10 @@ class UserGroupService extends Service /** * @param $id - * @return mixed + * + * @return UserGroupData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -71,6 +77,7 @@ class UserGroupService extends Service /** * @param $id + * * @return $this * @throws SPException */ @@ -85,6 +92,7 @@ class UserGroupService extends Service /** * @param array $ids + * * @return int * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -102,6 +110,7 @@ class UserGroupService extends Service /** * @param UserGroupData $itemData * @param array $users + * * @return int * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException @@ -120,6 +129,7 @@ class UserGroupService extends Service /** * @param UserGroupData $itemData + * * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -133,7 +143,9 @@ class UserGroupService extends Service /** * Get all items from the service's repository * - * @return array + * @return UserGroupData[] + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { @@ -144,7 +156,10 @@ class UserGroupService extends Service * Returns the item for given name * * @param string $name + * * @return UserGroupData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getByName($name) { @@ -155,7 +170,10 @@ class UserGroupService extends Service * Returns the users that are using the given group id * * @param $id int + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUsage($id) { @@ -166,7 +184,10 @@ class UserGroupService extends Service * Returns the items that are using the given group id * * @param $id int + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUsageByUsers($id) { diff --git a/lib/SP/Services/UserPassRecover/UserPassRecoverService.php b/lib/SP/Services/UserPassRecover/UserPassRecoverService.php index 75dd57d7..a775f951 100644 --- a/lib/SP/Services/UserPassRecover/UserPassRecoverService.php +++ b/lib/SP/Services/UserPassRecover/UserPassRecoverService.php @@ -27,7 +27,6 @@ namespace SP\Services\UserPassRecover; use SP\Bootstrap; use SP\Core\Messages\MailMessage; use SP\Html\Html; -use SP\Repositories\UserPassRecover\UserPassRecoverRepository; use SP\Services\Service; use SP\Services\ServiceException; use SP\Util\Util; @@ -51,7 +50,7 @@ class UserPassRecoverService extends Service const USER_MAIL_EXIST = 2; /** - * @var UserPassRecoverRepository + * @var \SP\Repositories\User\UserPassRecoverRepository */ protected $userPassRecoverRepository; @@ -157,6 +156,6 @@ class UserPassRecoverService extends Service */ protected function initialize() { - $this->userPassRecoverRepository = $this->dic->get(UserPassRecoverRepository::class); + $this->userPassRecoverRepository = $this->dic->get(\SP\Repositories\User\UserPassRecoverRepository::class); } } \ No newline at end of file diff --git a/lib/SP/Services/UserProfile/UserProfileService.php b/lib/SP/Services/UserProfile/UserProfileService.php index 9732f189..94c4faa8 100644 --- a/lib/SP/Services/UserProfile/UserProfileService.php +++ b/lib/SP/Services/UserProfile/UserProfileService.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -48,18 +48,12 @@ class UserProfileService extends Service */ protected $userProfileRepository; - /** - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface - */ - protected function initialize() - { - $this->userProfileRepository = $this->dic->get(UserProfileRepository::class); - } - /** * @param $id + * * @return UserProfileData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getById($id) { @@ -71,7 +65,10 @@ class UserProfileService extends Service /** * @param ItemSearchData $itemSearchData - * @return \SP\DataModel\ClientData[] + * + * @return \SP\Storage\Database\QueryResult + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function search(ItemSearchData $itemSearchData) { @@ -80,6 +77,7 @@ class UserProfileService extends Service /** * @param $id + * * @return $this * @throws SPException */ @@ -94,6 +92,7 @@ class UserProfileService extends Service /** * @param array $ids + * * @return int * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException @@ -110,6 +109,7 @@ class UserProfileService extends Service /** * @param $itemData + * * @return int * @throws SPException */ @@ -120,7 +120,8 @@ class UserProfileService extends Service /** * @param $itemData - * @return mixed + * + * @return bool * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -132,7 +133,10 @@ class UserProfileService extends Service /** * @param $id + * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getUsersForProfile($id) { @@ -143,9 +147,20 @@ class UserProfileService extends Service * Get all items from the service's repository * * @return array + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function getAllBasic() { return $this->userProfileRepository->getAll(); } + + /** + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + */ + protected function initialize() + { + $this->userProfileRepository = $this->dic->get(UserProfileRepository::class); + } } \ No newline at end of file diff --git a/lib/SP/Storage/DBStorageInterface.php b/lib/SP/Storage/Database/DBStorageInterface.php similarity index 97% rename from lib/SP/Storage/DBStorageInterface.php rename to lib/SP/Storage/Database/DBStorageInterface.php index 631f1a39..9d6ac3d8 100644 --- a/lib/SP/Storage/DBStorageInterface.php +++ b/lib/SP/Storage/Database/DBStorageInterface.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; use SP\Core\Exceptions\SPException; diff --git a/lib/SP/Storage/DBUtil.php b/lib/SP/Storage/Database/DBUtil.php similarity index 97% rename from lib/SP/Storage/DBUtil.php rename to lib/SP/Storage/Database/DBUtil.php index bce48cab..56e70bd9 100644 --- a/lib/SP/Storage/DBUtil.php +++ b/lib/SP/Storage/Database/DBUtil.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; use SP\Core\Exceptions\SPException; diff --git a/lib/SP/Storage/Database.php b/lib/SP/Storage/Database/Database.php similarity index 69% rename from lib/SP/Storage/Database.php rename to lib/SP/Storage/Database/Database.php index 50da5bf4..612fea34 100644 --- a/lib/SP/Storage/Database.php +++ b/lib/SP/Storage/Database/Database.php @@ -22,10 +22,11 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; use PDO; use PDOStatement; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; @@ -99,40 +100,89 @@ class Database implements DatabaseInterface return $this->lastId; } + /** + * @return DBStorageInterface + */ + public function getDbHandler() + { + return $this->dbHandler; + } + + /** + * @param QueryData $queryData + * @param bool $fullCount + * + * @return QueryResult + * @throws ConstraintException + * @throws QueryException + */ + public function doSelect(QueryData $queryData, $fullCount = false) + { + if ($queryData->getQuery() === '') { + throw new QueryException($queryData->getOnErrorMessage(), QueryException::ERROR, __u('Consulta en blanco')); + } + + try { + $queryResult = $this->doQuery($queryData); + + if ($fullCount === true) { + $queryResult->setTotalNumRows($this->getFullRowCount($queryData)); + } + + return $queryResult; + } catch (ConstraintException $e) { + processException($e); + + throw $e; + } catch (QueryException $e) { + processException($e); + + throw $e; + } catch (\Exception $e) { + processException($e); + + throw new QueryException( + $queryData->getOnErrorMessage(), + SPException::ERROR, + $e->getMessage(), + $e->getCode(), + $e + ); + } + } + /** * Realizar una consulta a la BBDD. * * @param $queryData QueryData Los datos de la consulta * @param $getRawData bool realizar la consulta para obtener registro a registro - * @return PDOStatement|array - * @throws SPException + * + * @return QueryResult + * @throws QueryException + * @throws ConstraintException */ public function doQuery(QueryData $queryData, $getRawData = false) { - $isSelect = preg_match("/^(select|show)\s/i", $queryData->getQuery()); - // Limpiar valores de caché - $this->lastResult = []; +// $this->lastResult = []; /** @var PDOStatement $stmt */ $stmt = $this->prepareQueryData($queryData); - if ($isSelect) { - if ($getRawData) { - return $stmt; - } - + if (preg_match("/^(select|show)\s/i", $queryData->getQuery())) { $this->numFields = $stmt->columnCount(); - $this->lastResult = $stmt->fetchAll(); - $this->numRows = count($this->lastResult); - $queryData->setQueryNumRows($this->numRows); - } else { - $this->numRows = $stmt->rowCount(); - $queryData->setQueryNumRows($stmt->rowCount()); + return new QueryResult($stmt->fetchAll()); + +// $this->lastResult = $stmt->fetchAll(); +// $this->numRows = count($this->lastResult); +// +// $queryData->setQueryNumRows($this->numRows); } - return $stmt; + return (new QueryResult()) + ->setAffectedNumRows($stmt->rowCount()) + ->setLastId($this->lastId); } /** @@ -140,8 +190,10 @@ class Database implements DatabaseInterface * * @param $queryData QueryData Los datos de la consulta * @param $isCount bool Indica si es una consulta de contador de registros + * * @return \PDOStatement|false * @throws QueryException + * @throws ConstraintException */ private function prepareQueryData(QueryData $queryData, $isCount = false) { @@ -204,12 +256,19 @@ class Database implements DatabaseInterface return $stmt; } catch (\Exception $e) { processException($e); -// ob_start(); -// debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); -// debugLog('Exception: ' . $e->getMessage()); -// debugLog(ob_get_clean()); - throw new QueryException($e->getMessage(), SPException::CRITICAL, $e->getCode(), 0, $e); + switch ($e->getCode()) { + case '23000': + throw new ConstraintException( + __u('Restricción de integridad'), + ConstraintException::ERROR, + $e->getMessage(), + $e->getCode(), + $e + ); + } + + throw new QueryException($e->getMessage(), QueryException::CRITICAL, $e->getCode(), 0, $e); } } @@ -217,6 +276,7 @@ class Database implements DatabaseInterface * Obtener el número de filas de una consulta realizada * * @param $queryData QueryData Los datos de la consulta + * * @return int Número de files de la consulta * @throws SPException */ @@ -229,16 +289,20 @@ class Database implements DatabaseInterface $queryRes = $this->prepareQueryData($queryData, true); $num = (int)$queryRes->fetchColumn(); $queryRes->closeCursor(); - $queryData->setQueryNumRows($num); return $num; } /** - * @return DBStorageInterface + * Don't fetch records and return prepared statement + * + * @param QueryData $queryData + * + * @return \PDOStatement + * @throws QueryException */ - public function getDbHandler() + public function doQueryRaw(QueryData $queryData) { - return $this->dbHandler; + return $this->prepareQueryData($queryData); } } \ No newline at end of file diff --git a/lib/SP/Storage/DatabaseConnectionData.php b/lib/SP/Storage/Database/DatabaseConnectionData.php similarity index 99% rename from lib/SP/Storage/DatabaseConnectionData.php rename to lib/SP/Storage/Database/DatabaseConnectionData.php index a4e086fe..df818070 100644 --- a/lib/SP/Storage/DatabaseConnectionData.php +++ b/lib/SP/Storage/Database/DatabaseConnectionData.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; use SP\Config\ConfigData; diff --git a/lib/SP/Storage/DatabaseFileInterface.php b/lib/SP/Storage/Database/DatabaseFileInterface.php similarity index 95% rename from lib/SP/Storage/DatabaseFileInterface.php rename to lib/SP/Storage/Database/DatabaseFileInterface.php index 298cfbdc..6fac48db 100644 --- a/lib/SP/Storage/DatabaseFileInterface.php +++ b/lib/SP/Storage/Database/DatabaseFileInterface.php @@ -22,7 +22,8 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; +use SP\Storage\FileHandler; /** * Interface DatabaseFileInterface diff --git a/lib/SP/Storage/DatabaseInterface.php b/lib/SP/Storage/Database/DatabaseInterface.php similarity index 75% rename from lib/SP/Storage/DatabaseInterface.php rename to lib/SP/Storage/Database/DatabaseInterface.php index 435144c1..d14dafda 100644 --- a/lib/SP/Storage/DatabaseInterface.php +++ b/lib/SP/Storage/Database/DatabaseInterface.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -22,9 +22,9 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; -use PDOStatement; +use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; /** @@ -37,17 +37,28 @@ interface DatabaseInterface /** * Performs a DB query * - * @param QueryData $queryData Query data - * @param bool $getRawData Don't fetch records and return prepared statement - * @return PDOStatement|array + * @param QueryData $queryData Query data + * + * @return QueryResult * @throws QueryException + * @throws ConstraintException */ - public function doQuery(QueryData $queryData, $getRawData = false); + public function doQuery(QueryData $queryData); + + /** + * Don't fetch records and return prepared statement + * + * @param QueryData $queryData + * + * @return \PDOStatement + */ + public function doQueryRaw(QueryData $queryData); /** * Returns the total number of records * * @param QueryData $queryData Query data + * * @return int Records count */ public function getFullRowCount(QueryData $queryData); diff --git a/lib/SP/Storage/DbWrapper.php b/lib/SP/Storage/Database/DbWrapper.php similarity index 62% rename from lib/SP/Storage/DbWrapper.php rename to lib/SP/Storage/Database/DbWrapper.php index 7f9890ed..5f016671 100644 --- a/lib/SP/Storage/DbWrapper.php +++ b/lib/SP/Storage/Database/DbWrapper.php @@ -22,9 +22,8 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; -use PDOStatement; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; @@ -58,17 +57,14 @@ class DbWrapper * * @param QueryData $queryData * @param DatabaseInterface $db - * @return array + * + * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public static function getResultsArray(QueryData $queryData, DatabaseInterface $db = null) { - $results = self::getResults($queryData, $db); - - if ($results === false) { - return []; - } - - return is_object($results) ? [$results] : $results; + return $db->doQuery($queryData); } /** @@ -76,61 +72,14 @@ class DbWrapper * * @param QueryData $queryData QueryData Los datos de la consulta * @param DatabaseInterface $db - * @return mixed devuelve bool si hay un error. Devuelve array con el array de registros devueltos + * + * @return QueryResult devuelve bool si hay un error. Devuelve array con el array de registros devueltos + * @throws ConstraintException + * @throws QueryException */ public static function getResults(QueryData $queryData, DatabaseInterface $db = null) { - if ($queryData->getQuery() === '') { - self::resetVars(); - return false; - } - - try { - $db->doQuery($queryData); - - if (self::$fullRowCount === true) { - $db->getFullRowCount($queryData); - } - } catch (\Exception $e) { - processException($e); - - $queryData->setQueryStatus($e->getCode()); - } - - self::resetVars(); - - if ($db->getNumRows() === 1 && !$queryData->isUseKeyPair()) { - return $db->getLastResult()[0]; - } - - return $db->getLastResult(); - } - - /** - * Restablecer los atributos estáticos - */ - private static function resetVars() - { - self::$fullRowCount = false; - } - - /** - * Devolver los resultados como objeto PDOStatement - * - * @param QueryData $queryData - * @param DatabaseInterface $db - * @return PDOStatement|false - * @throws \Exception - */ - public static function getResultsRaw(QueryData $queryData, DatabaseInterface $db = null) - { - try { - return $db->doQuery($queryData, true); - } catch (\Exception $e) { - processException($e); - - throw $e; - } + return $db->doQuery($queryData); } /** @@ -138,57 +87,14 @@ class DbWrapper * * @param QueryData $queryData Los datos para realizar la consulta * @param DatabaseInterface $db - * @return bool + * + * @return int * @throws QueryException * @throws ConstraintException */ public static function getQuery(QueryData $queryData, DatabaseInterface $db) { - if (null === $queryData->getOnErrorMessage()) { - $errorMessage = __u('Error en la consulta'); - } else { - $errorMessage = $queryData->getOnErrorMessage(); - } - - if ($queryData->getQuery() === '') { - throw new QueryException($errorMessage, SPException::ERROR, __u('Consulta en blanco')); - } - - try { - $db->doQuery($queryData); - - return true; - } catch (QueryException $e) { - processException($e); - - $queryData->setQueryStatus($e->getCode()); - $previous = $e->getPrevious(); - - if ($previous) { - switch ($previous->getCode()) { - case '23000': - throw new ConstraintException( - __u('Restricción de integridad'), - SPException::ERROR, - $e->getMessage(), - $e->getCode(), - $e - ); - } - } - - throw $e; - } catch (\Exception $e) { - processException($e); - - throw new QueryException( - $errorMessage, - SPException::ERROR, - $e->getMessage(), - $e->getCode(), - $e - ); - } + return $db->doQuery($queryData)->getAffectedNumRows(); } /** @@ -203,6 +109,7 @@ class DbWrapper * Iniciar una transacción * * @param DatabaseInterface $db + * * @return bool * @throws SPException */ @@ -217,6 +124,7 @@ class DbWrapper * Finalizar una transacción * * @param DatabaseInterface $db + * * @return bool * @throws SPException */ @@ -231,6 +139,7 @@ class DbWrapper * Rollback de una transacción * * @param DatabaseInterface $db + * * @return bool * @throws SPException */ @@ -241,6 +150,14 @@ class DbWrapper return $conn->inTransaction() && $conn->rollBack(); } + /** + * Restablecer los atributos estáticos + */ + private static function resetVars() + { + self::$fullRowCount = false; + } + /** * Método para registar los eventos de BD en el log * diff --git a/lib/SP/Storage/MySQLFileParser.php b/lib/SP/Storage/Database/MySQLFileParser.php similarity index 95% rename from lib/SP/Storage/MySQLFileParser.php rename to lib/SP/Storage/Database/MySQLFileParser.php index e4a61428..22a83a9d 100644 --- a/lib/SP/Storage/MySQLFileParser.php +++ b/lib/SP/Storage/Database/MySQLFileParser.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -22,7 +22,10 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; + +use SP\Storage\FileException; +use SP\Storage\FileHandler; /** * Class MysqlFileParser diff --git a/lib/SP/Storage/MySQLHandler.php b/lib/SP/Storage/Database/MySQLHandler.php similarity index 98% rename from lib/SP/Storage/MySQLHandler.php rename to lib/SP/Storage/Database/MySQLHandler.php index 7eff509f..d1388680 100644 --- a/lib/SP/Storage/MySQLHandler.php +++ b/lib/SP/Storage/Database/MySQLHandler.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; use PDO; use SP\Core\Exceptions\SPException; diff --git a/lib/SP/Storage/QueryData.php b/lib/SP/Storage/Database/QueryData.php similarity index 95% rename from lib/SP/Storage/QueryData.php rename to lib/SP/Storage/Database/QueryData.php index 516c8e91..6627d813 100644 --- a/lib/SP/Storage/QueryData.php +++ b/lib/SP/Storage/Database/QueryData.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Storage; +namespace SP\Storage\Database; use SP\DataModel\DataModelBase; @@ -109,16 +109,6 @@ class QueryData } } - /** - * Añadir parámetros a la consulta - * - * @param array $params - */ - public function addParams(array $params) - { - $this->params = array_merge($this->params, $params); - } - /** * @return array */ @@ -127,6 +117,16 @@ class QueryData return $this->params; } + /** + * Establecer los parámetros de la consulta + * + * @param array $data + */ + public function setParams(array $data) + { + $this->params = $data; + } + /** * @return string */ @@ -195,16 +195,6 @@ class QueryData $this->useKeyPair = (bool)$useKeyPair; } - /** - * Establecer los parámetros de la consulta - * - * @param array $data - */ - public function setParams(array $data) - { - $this->params = $data; - } - /** * @return string */ @@ -248,15 +238,30 @@ class QueryData } /** - * @param string $limit + * @param string $limit + * @param array|null $params */ - public function setLimit($limit) + public function setLimit($limit, array $params = null) { if (!empty($limit)) { $this->limit = 'LIMIT ' . $limit; + + if ($params !== null) { + $this->addParams($params); + } } } + /** + * Añadir parámetros a la consulta + * + * @param array $params + */ + public function addParams(array $params) + { + $this->params = array_merge($this->params, $params); + } + /** * @return string */ @@ -346,7 +351,7 @@ class QueryData */ public function getOnErrorMessage() { - return $this->onErrorMessage; + return $this->onErrorMessage ?: __u('Error en la consulta'); } /** diff --git a/lib/SP/Storage/Database/QueryResult.php b/lib/SP/Storage/Database/QueryResult.php new file mode 100644 index 00000000..17639c6e --- /dev/null +++ b/lib/SP/Storage/Database/QueryResult.php @@ -0,0 +1,183 @@ +. + */ + +namespace SP\Storage\Database; + +/** + * Class QueryResult + * + * @package SP\Storage\Database + */ +class QueryResult +{ + /** + * @var array + */ + private $data = []; + /** + * @var int + */ + private $numRows = 0; + /** + * @var int + */ + private $totalNumRows; + /** + * @var int + */ + private $affectedNumRows; + /** + * @var int + */ + private $statusCode; + /** + * @var int + */ + private $lastId = 0; + + /** + * QueryResult constructor. + * + * @param array $data + */ + public function __construct(array $data = null) + { + if ($data !== null) { + $this->data = $data; + $this->numRows = count($data); + } + } + + /** + * @return mixed + */ + public function getData() + { + // FIXME + if ($this->numRows === 1) { + return $this->data[0]; + } + + return $this->data; + } + + /** + * Always returns an array + * + * @return array + */ + public function getDataAsArray(): array + { + return $this->data; + } + + /** + * @return int + */ + public function getNumRows(): int + { + return $this->numRows; + } + + /** + * @return int + */ + public function getTotalNumRows(): int + { + return $this->totalNumRows; + } + + /** + * @param int $totalNumRows + * + * @return QueryResult + */ + public function setTotalNumRows(int $totalNumRows) + { + $this->totalNumRows = $totalNumRows; + + return $this; + } + + /** + * @return int + */ + public function getStatusCode(): int + { + return $this->statusCode; + } + + /** + * @param int $statusCode + * + * @return QueryResult + */ + public function setStatusCode(int $statusCode) + { + $this->statusCode = $statusCode; + + return $this; + } + + /** + * @return int + */ + public function getAffectedNumRows(): int + { + return $this->affectedNumRows; + } + + /** + * @param int $affectedNumRows + * + * @return QueryResult + */ + public function setAffectedNumRows(int $affectedNumRows) + { + $this->affectedNumRows = $affectedNumRows; + + return $this; + } + + /** + * @return int + */ + public function getLastId(): int + { + return $this->lastId; + } + + /** + * @param int $lastId + * + * @return QueryResult + */ + public function setLastId(int $lastId) + { + $this->lastId = $lastId; + + return $this; + } + +} \ No newline at end of file diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 3d16c415..0466977c 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -86,6 +86,7 @@ class ConfigTest extends TestCase * Comprobar que la configuración se guarda correctamente * * @depends testLoadClass + * * @param Config $config */ public function testSaveConfig($config) @@ -100,7 +101,9 @@ class ConfigTest extends TestCase * Comprobar la carga de la configuración en el contexto * * @depends testLoadClass + * * @param Config $config + * * @throws DependencyException * @throws NotFoundException */ @@ -117,6 +120,7 @@ class ConfigTest extends TestCase * Comprobar la actualización de la configuración * * @depends testLoadClass + * * @param Config $config */ public function testUpdateConfig($config) @@ -130,7 +134,9 @@ class ConfigTest extends TestCase * Comprobar la generación de una clave de actualización y que su longitud es correcta * * @depends testLoadClass + * * @param Config $config + * * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public function testGenerateUpgradeKey($config) diff --git a/tests/CryptTest.php b/tests/CryptTest.php index a125e205..c291b714 100644 --- a/tests/CryptTest.php +++ b/tests/CryptTest.php @@ -55,7 +55,9 @@ class CryptTest extends TestCase * Comprobar el desbloqueo de una llave de cifrado * * @depends testMakeSecuredKey + * * @param string $key LLave de cifrado + * * @throws CryptoException */ public function testUnlockSecuredKey($key) @@ -70,7 +72,9 @@ class CryptTest extends TestCase * * @depends testMakeSecuredKey * @covers \SP\Core\Crypt\Crypt::unlockSecuredKey + * * @param string $key LLave de cifrado + * * @throws CryptoException */ public function testUnlockSecuredKeyWithWrongPassword($key) @@ -86,7 +90,9 @@ class CryptTest extends TestCase * @depends testMakeSecuredKey * @covers \SP\Core\Crypt\Crypt::encrypt * @covers \SP\Core\Crypt\Crypt::decrypt + * * @param string $key LLave de cifrado + * * @throws CryptoException */ public function testEncryptAndDecrypt($key) @@ -102,7 +108,9 @@ class CryptTest extends TestCase * @depends testMakeSecuredKey * @covers \SP\Core\Crypt\Crypt::encrypt * @covers \SP\Core\Crypt\Crypt::decrypt + * * @param string $key LLave de cifrado + * * @throws CryptoException */ public function testEncryptAndDecryptWithDifferentPassword($key) diff --git a/tests/DatabaseTestCase.php b/tests/DatabaseTestCase.php index de8f4166..53342696 100644 --- a/tests/DatabaseTestCase.php +++ b/tests/DatabaseTestCase.php @@ -28,8 +28,8 @@ use PHPUnit\DbUnit\Database\DefaultConnection; use PHPUnit\DbUnit\DataSet\IDataSet; use PHPUnit\DbUnit\TestCaseTrait; use PHPUnit\Framework\TestCase; -use SP\Storage\DatabaseConnectionData; -use SP\Storage\MySQLHandler; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Storage\Database\MySQLHandler; /** * Class DatabaseBaseTest diff --git a/tests/AccountRepositoryTest.php b/tests/Repositories/AccountRepositoryTest.php similarity index 89% rename from tests/AccountRepositoryTest.php rename to tests/Repositories/AccountRepositoryTest.php index b5566f6a..6b64dd69 100644 --- a/tests/AccountRepositoryTest.php +++ b/tests/Repositories/AccountRepositoryTest.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Account\AccountRequest; @@ -35,7 +35,9 @@ use SP\DataModel\ItemSearchData; use SP\Mvc\Model\QueryCondition; use SP\Repositories\Account\AccountRepository; use SP\Services\Account\AccountPasswordRequest; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class AccountRepositoryTest @@ -116,7 +118,7 @@ class AccountRepositoryTest extends DatabaseTestCase $accountRequest->passDateChange = time() + 3600; // Comprobar que la modificación de la clave es correcta - $this->assertTrue(self::$accountRepository->editPassword($accountRequest)); + $this->assertEquals(1, self::$accountRepository->editPassword($accountRequest)); $accountPassData = self::$accountRepository->getPasswordForId(2); $clearPassword = Crypt::decrypt($accountPassData->pass, $accountPassData->key, self::SECURE_KEY_PASSWORD); @@ -128,14 +130,6 @@ class AccountRepositoryTest extends DatabaseTestCase $this->assertCount(0, self::$accountRepository->getPasswordForId(10)); } - /** - * No implementado - */ - public function testCheckInUse() - { - $this->markTestSkipped('Not implemented'); - } - /** * Comprobar la obtención de cuentas * @@ -173,7 +167,7 @@ class AccountRepositoryTest extends DatabaseTestCase $accountRequest->parentId = 0; $accountRequest->userGroupId = 2; - $this->assertTrue(self::$accountRepository->update($accountRequest)); + $this->assertEquals(1, self::$accountRepository->update($accountRequest)); $account = self::$accountRepository->getById(1); @@ -221,6 +215,9 @@ class AccountRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda de cuentas + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testSearch() { @@ -229,31 +226,35 @@ class AccountRepositoryTest extends DatabaseTestCase $itemSearchData->setSeachString('Google'); $itemSearchData->setLimitCount(10); - $search = self::$accountRepository->search($itemSearchData); + $result = self::$accountRepository->search($itemSearchData); + $data = $result->getDataAsArray(); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertInstanceOf(\stdClass::class, $search[0]); - $this->assertEquals(1, $search[0]->id); - $this->assertEquals('Google', $search[0]->name); + $this->assertCount(1, $data); + $this->assertEquals(1, $result->getNumRows()); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(1, $data[0]->id); + $this->assertEquals('Google', $data[0]->name); // Comprobar búsqueda con el texto Apple $itemSearchData = new ItemSearchData(); $itemSearchData->setSeachString('Apple'); $itemSearchData->setLimitCount(1); - $search = self::$accountRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertInstanceOf(\stdClass::class, $search[0]); - $this->assertEquals(2, $search[0]->id); - $this->assertEquals('Apple', $search[0]->name); + $result = self::$accountRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertCount(1, $data); + $this->assertEquals(1, $result->getNumRows()); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(2, $data[0]->id); + $this->assertEquals('Apple', $data[0]->name); } /** * Comprobar las cuentas enlazadas + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetLinked() { @@ -283,6 +284,9 @@ class AccountRepositoryTest extends DatabaseTestCase /** * Obtener todas las cuentas + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetAll() { @@ -331,6 +335,9 @@ class AccountRepositoryTest extends DatabaseTestCase /** * Comprobar el número total de cuentas + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetTotalNumAccounts() { @@ -347,6 +354,9 @@ class AccountRepositoryTest extends DatabaseTestCase /** * Comprobar las cuentas devueltas para un filtro de usuario + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetForUser() { @@ -358,6 +368,9 @@ class AccountRepositoryTest extends DatabaseTestCase /** * Comprobar las cuentas devueltas para obtener los datos de las claves + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetAccountsPassData() { @@ -426,6 +439,10 @@ class AccountRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda de cuentas mediante filtros + * + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetByFilter() { @@ -435,6 +452,7 @@ class AccountRepositoryTest extends DatabaseTestCase // Comprobar un Id de categoría $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(1, $response->getCount()); $this->assertCount(1, $response->getData()); @@ -445,6 +463,7 @@ class AccountRepositoryTest extends DatabaseTestCase $searchFilter->setCategoryId(10); $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(0, $response->getCount()); $this->assertCount(0, $response->getData()); @@ -455,6 +474,7 @@ class AccountRepositoryTest extends DatabaseTestCase $searchFilter->setClientId(1); $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(1, $response->getCount()); $this->assertCount(1, $response->getData()); @@ -465,6 +485,7 @@ class AccountRepositoryTest extends DatabaseTestCase $searchFilter->setClientId(10); $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(0, $response->getCount()); $this->assertCount(0, $response->getData()); @@ -475,6 +496,7 @@ class AccountRepositoryTest extends DatabaseTestCase $searchFilter->setCleanTxtSearch('apple.com'); $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(1, $response->getCount()); $this->assertCount(1, $response->getData()); @@ -486,6 +508,7 @@ class AccountRepositoryTest extends DatabaseTestCase $searchFilter->setSearchFavorites(true); $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(0, $response->getCount()); $this->assertCount(0, $response->getData()); @@ -496,6 +519,7 @@ class AccountRepositoryTest extends DatabaseTestCase $searchFilter->setTagsId([1]); $response = self::$accountRepository->getByFilter($searchFilter); + $this->assertInstanceOf(AccountSearchResponse::class, $response); $this->assertEquals(1, $response->getCount()); $this->assertCount(1, $response->getData()); diff --git a/tests/AccountToTagRepositoryTest.php b/tests/Repositories/AccountToTagRepositoryTest.php similarity index 86% rename from tests/AccountToTagRepositoryTest.php rename to tests/Repositories/AccountToTagRepositoryTest.php index 684c8f91..b5a17199 100644 --- a/tests/AccountToTagRepositoryTest.php +++ b/tests/Repositories/AccountToTagRepositoryTest.php @@ -22,14 +22,16 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Account\AccountRequest; use SP\Core\Exceptions\ConstraintException; use SP\DataModel\ItemData; use SP\Repositories\Account\AccountToTagRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class AccountToTagRepositoryTest @@ -73,7 +75,7 @@ class AccountToTagRepositoryTest extends DatabaseTestCase $accountRequest->id = 1; $accountRequest->tags = [2, 3]; - $this->assertTrue(self::$accountToTagRepository->add($accountRequest)); + $this->assertEquals(2, self::$accountToTagRepository->add($accountRequest)); $tags = self::$accountToTagRepository->getTagsByAccountId($accountRequest->id); @@ -87,6 +89,10 @@ class AccountToTagRepositoryTest extends DatabaseTestCase $accountRequest->tags = [1]; self::$accountToTagRepository->add($accountRequest); + + $accountRequest->id = 10; + + self::$accountToTagRepository->add($accountRequest); } @@ -98,13 +104,18 @@ class AccountToTagRepositoryTest extends DatabaseTestCase */ public function testDeleteByAccountId() { - $this->assertTrue(self::$accountToTagRepository->deleteByAccountId(1)); + $this->assertEquals(1, self::$accountToTagRepository->deleteByAccountId(1)); + $this->assertCount(0, self::$accountToTagRepository->getTagsByAccountId(1)); - $this->assertTrue(self::$accountToTagRepository->deleteByAccountId(10)); + + $this->assertEquals(0, self::$accountToTagRepository->deleteByAccountId(10)); } /** * Comprobar la obtención de etiquetas por Id de cuenta + * + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testGetTagsByAccountId() { diff --git a/tests/AccountToUserGroupRepositoryTest.php b/tests/Repositories/AccountToUserGroupRepositoryTest.php similarity index 98% rename from tests/AccountToUserGroupRepositoryTest.php rename to tests/Repositories/AccountToUserGroupRepositoryTest.php index a25f8581..a5bda9e4 100644 --- a/tests/AccountToUserGroupRepositoryTest.php +++ b/tests/Repositories/AccountToUserGroupRepositoryTest.php @@ -22,14 +22,16 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Account\AccountRequest; use SP\Core\Exceptions\ConstraintException; use SP\DataModel\ItemData; use SP\Repositories\Account\AccountToUserGroupRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class AccountToUserGroupRepositoryTest diff --git a/tests/AccountToUserRepositoryTest.php b/tests/Repositories/AccountToUserRepositoryTest.php similarity index 98% rename from tests/AccountToUserRepositoryTest.php rename to tests/Repositories/AccountToUserRepositoryTest.php index d02b1d15..a9c71a9f 100644 --- a/tests/AccountToUserRepositoryTest.php +++ b/tests/Repositories/AccountToUserRepositoryTest.php @@ -22,14 +22,16 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Account\AccountRequest; use SP\Core\Exceptions\ConstraintException; use SP\DataModel\ItemData; use SP\Repositories\Account\AccountToUserRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class AccountToUserRepositoryTest diff --git a/tests/Repositories/AuthTokenRepositoryTest.php b/tests/Repositories/AuthTokenRepositoryTest.php new file mode 100644 index 00000000..2daf3336 --- /dev/null +++ b/tests/Repositories/AuthTokenRepositoryTest.php @@ -0,0 +1,315 @@ +. + */ + +namespace SP\Tests\Repositories; + +use Defuse\Crypto\Exception\CryptoException; +use DI\DependencyException; +use SP\Core\Acl\ActionsInterface; +use SP\Core\Crypt\Hash; +use SP\Core\Crypt\Vault; +use SP\DataModel\AuthTokenData; +use SP\DataModel\ItemSearchData; +use SP\Repositories\AuthToken\AuthTokenRepository; +use SP\Repositories\DuplicatedItemException; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use SP\Util\Util; +use function SP\Tests\setupContext; + +/** + * Class AuthTokenRepositoryTest + * + * @package SP\Tests\Repositories + */ +class AuthTokenRepositoryTest extends DatabaseTestCase +{ + const AUTH_TOKEN = '2cee8b224f48e01ef48ac172e879cc7825800a9d7ce3b23783212f4758f1c146'; + const AUTH_TOKEN_PASS = 123456; + + /** + * @var AuthTokenRepository + */ + private static $authTokenRepository; + + /** + * @throws DependencyException + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el repositorio + self::$authTokenRepository = $dic->get(AuthTokenRepository::class); + } + + public function testGetById() + { + $authToken = self::$authTokenRepository->getById(1); + + $this->assertInstanceOf(AuthTokenData::class, $authToken); + $this->assertEquals(1, $authToken->getId()); + $this->assertEquals(ActionsInterface::ACCOUNT_SEARCH, $authToken->getActionId()); + $this->assertEquals(self::AUTH_TOKEN, $authToken->getToken()); + $this->assertNull($authToken->getHash()); + + $authToken = self::$authTokenRepository->getById(2); + + $this->assertInstanceOf(AuthTokenData::class, $authToken); + $this->assertEquals(2, $authToken->getId()); + $this->assertEquals(ActionsInterface::ACCOUNT_VIEW_PASS, $authToken->getActionId()); + $this->assertEquals(self::AUTH_TOKEN, $authToken->getToken()); + $this->assertTrue(Hash::checkHashKey(self::AUTH_TOKEN_PASS, $authToken->getHash())); + } + + public function testGetTokenByUserId() + { + $this->assertEquals(self::AUTH_TOKEN, self::$authTokenRepository->getTokenByUserId(1)); + + $this->assertNull(self::$authTokenRepository->getTokenByUserId(2)); + } + + /** + * @throws CryptoException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testRefreshVaultByUserId() + { + $vault = Vault::getInstance() + ->saveData('prueba', self::AUTH_TOKEN_PASS) + ->getSerialized(); + $hash = Hash::hashKey(self::AUTH_TOKEN_PASS); + + $this->assertEquals(1, self::$authTokenRepository->refreshVaultByUserId(1, $vault, $hash)); + + $authToken = self::$authTokenRepository->getTokenByToken(ActionsInterface::ACCOUNT_VIEW_PASS, self::AUTH_TOKEN); + + $this->assertInstanceOf(AuthTokenData::class, $authToken); + $this->assertTrue(Hash::checkHashKey(self::AUTH_TOKEN_PASS, $authToken->getHash())); + $this->assertEquals($vault, $authToken->getVault()); + + /** @var Vault $vault */ + $vault = Util::unserialize(Vault::class, $authToken->getVault()); + $this->assertEquals('prueba', $vault->getData(self::AUTH_TOKEN_PASS)); + } + + /** + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testRefreshTokenByUserId() + { + $token = Util::generateRandomBytes(); + + // Comprobar actualización con usuario que existe + $this->assertEquals(2, self::$authTokenRepository->refreshTokenByUserId(1, $token)); + $this->assertEquals($token, self::$authTokenRepository->getTokenByUserId(1)); + + // Comprobar actualización con usuario que NO existe + $this->assertEquals(0, self::$authTokenRepository->refreshTokenByUserId(2, $token)); + $this->assertNull(self::$authTokenRepository->getTokenByUserId(2)); + } + + /** + * @covers \SP\Repositories\AuthToken\AuthTokenRepository::checkDuplicatedOnUpdate() + * + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Repositories\DuplicatedItemException + * @throws CryptoException + */ + public function testUpdate() + { + $token = Util::generateRandomBytes(); + $hash = Hash::hashKey('prueba123'); + $vault = Vault::getInstance()->saveData('prueba', 'prueba123'); + + $authTokenData = new AuthTokenData(); + $authTokenData->setId(1); + $authTokenData->setActionId(ActionsInterface::ACCOUNT_CREATE); + $authTokenData->setCreatedBy(1); + $authTokenData->setHash($hash); + $authTokenData->setToken($token); + $authTokenData->setVault($vault); + $authTokenData->setUserId(2); + + $this->assertEquals(1, self::$authTokenRepository->update($authTokenData)); + + $result = self::$authTokenRepository->getTokenByToken(ActionsInterface::ACCOUNT_CREATE, $token); + + $this->assertInstanceOf(AuthTokenData::class, $result); + $this->assertEquals(ActionsInterface::ACCOUNT_CREATE, $result->getActionId()); + $this->assertEquals($hash, $result->getHash()); + $this->assertEquals(2, $result->getUserId()); + $this->assertEquals($vault->getSerialized(), $result->getVault()); + + $this->expectException(DuplicatedItemException::class); + + $authTokenData->setId(2); + $authTokenData->setUserId(1); + + self::$authTokenRepository->update($authTokenData); + } + + /** + * @throws CryptoException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetTokenByToken() + { + $authToken = self::$authTokenRepository->getTokenByToken(ActionsInterface::ACCOUNT_VIEW_PASS, self::AUTH_TOKEN); + + $this->assertEquals(2, $authToken->getId()); + $this->assertEquals(ActionsInterface::ACCOUNT_VIEW_PASS, $authToken->getActionId()); + $this->assertTrue(Hash::checkHashKey(self::AUTH_TOKEN_PASS, $authToken->getHash())); + $this->assertNotEmpty($authToken->getVault()); + + /** @var Vault $vault */ + $vault = Util::unserialize(Vault::class, $authToken->getVault()); + $this->assertEquals('12345678900', $vault->getData(self::AUTH_TOKEN_PASS . self::AUTH_TOKEN)); + + $this->expectException(CryptoException::class); + + $vault->getData(1234); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testSearch() + { + $itemSearchData = new ItemSearchData(); + $itemSearchData->setSeachString('admin'); + + $result = self::$authTokenRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(2, $result->getNumRows()); + $this->assertCount(2, $data); + + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(ActionsInterface::ACCOUNT_SEARCH, $data[0]->actionId); + $this->assertEquals(self::AUTH_TOKEN, $data[0]->token); + + $this->assertInstanceOf(\stdClass::class, $data[1]); + $this->assertEquals(ActionsInterface::ACCOUNT_VIEW_PASS, $data[1]->actionId); + $this->assertEquals(self::AUTH_TOKEN, $data[1]->token); + + $itemSearchData = new ItemSearchData(); + $itemSearchData->setSeachString('test'); + + $result = self::$authTokenRepository->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDeleteByIdBatch() + { + $this->assertEquals(2, $this->conn->getRowCount('AuthToken')); + + $this->assertEquals(2, self::$authTokenRepository->deleteByIdBatch([1, 2, 3])); + $this->assertEquals(0, $this->conn->getRowCount('AuthToken')); + + $this->assertEquals(0, self::$authTokenRepository->deleteByIdBatch([])); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetUserIdForToken() + { + $this->assertEquals(1, self::$authTokenRepository->getUserIdForToken(self::AUTH_TOKEN)); + + $this->assertFalse(self::$authTokenRepository->getUserIdForToken('no_token')); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDelete() + { + $this->assertEquals(1, self::$authTokenRepository->delete(1)); + + $this->assertEquals(0, self::$authTokenRepository->delete(10)); + } + + /** + * @covers \SP\Repositories\AuthToken\AuthTokenRepository::checkDuplicatedOnAdd() + * + * @throws CryptoException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function testCreate() + { + $token = Util::generateRandomBytes(); + $hash = Hash::hashKey('prueba123'); + $vault = Vault::getInstance()->saveData('prueba', 'prueba123'); + + $authTokenData = new AuthTokenData(); + $authTokenData->setActionId(ActionsInterface::ACCOUNT_CREATE); + $authTokenData->setCreatedBy(1); + $authTokenData->setHash($hash); + $authTokenData->setToken($token); + $authTokenData->setVault($vault); + $authTokenData->setUserId(2); + + $this->assertEquals(3, self::$authTokenRepository->create($authTokenData)); + $this->assertEquals(3, $this->conn->getRowCount('AuthToken')); + + $result = self::$authTokenRepository->getTokenByToken(ActionsInterface::ACCOUNT_CREATE, $token); + + $this->assertInstanceOf(AuthTokenData::class, $result); + $this->assertEquals(ActionsInterface::ACCOUNT_CREATE, $result->getActionId()); + $this->assertEquals($hash, $result->getHash()); + $this->assertEquals(3, $result->getId()); + $this->assertEquals(2, $result->getUserId()); + $this->assertEquals($vault->getSerialized(), $result->getVault()); + + $this->expectException(DuplicatedItemException::class); + + $authTokenData->setUserId(1); + + self::$authTokenRepository->create($authTokenData); + } +} diff --git a/tests/CategoryRepositoryTest.php b/tests/Repositories/CategoryRepositoryTest.php similarity index 90% rename from tests/CategoryRepositoryTest.php rename to tests/Repositories/CategoryRepositoryTest.php index ae1628bd..ad269ca7 100644 --- a/tests/CategoryRepositoryTest.php +++ b/tests/Repositories/CategoryRepositoryTest.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; @@ -30,7 +30,9 @@ use SP\DataModel\CategoryData; use SP\DataModel\ItemSearchData; use SP\Repositories\Category\CategoryRepository; use SP\Repositories\DuplicatedItemException; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class CategoryRepositoryTest @@ -90,6 +92,9 @@ class CategoryRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda mediante texto + * + * @throws ConstraintException + * @throws QueryException */ public function testSearch() { @@ -97,21 +102,24 @@ class CategoryRepositoryTest extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('linux'); - $search = self::$categoryRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertEquals(2, $search[0]->id); - $this->assertEquals('Linux server', $search[0]->description); + $result = self::$categoryRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(2, $data[0]->id); + $this->assertEquals('Linux server', $data[0]->description); $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('prueba'); - $search = self::$categoryRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$categoryRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $data); } /** @@ -261,6 +269,9 @@ class CategoryRepositoryTest extends DatabaseTestCase /** * Comprobar la obtención de categorías por Id en lote + * + * @throws ConstraintException + * @throws QueryException */ public function testGetByIdBatch() { @@ -268,12 +279,4 @@ class CategoryRepositoryTest extends DatabaseTestCase $this->assertCount(3, self::$categoryRepository->getByIdBatch([1, 2, 3, 4, 5])); $this->assertCount(0, self::$categoryRepository->getByIdBatch([])); } - - /** - * No implementado - */ - public function testCheckInUse() - { - $this->markTestSkipped('Not implemented'); - } } diff --git a/tests/ClientRepositoryTest.php b/tests/Repositories/ClientRepositoryTest.php similarity index 90% rename from tests/ClientRepositoryTest.php rename to tests/Repositories/ClientRepositoryTest.php index 5620f653..989edf02 100644 --- a/tests/ClientRepositoryTest.php +++ b/tests/Repositories/ClientRepositoryTest.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; @@ -31,7 +31,9 @@ use SP\DataModel\ItemSearchData; use SP\Mvc\Model\QueryCondition; use SP\Repositories\Client\ClientRepository; use SP\Repositories\DuplicatedItemException; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class ClientRepositoryTest @@ -91,6 +93,9 @@ class ClientRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda mediante texto + * + * @throws ConstraintException + * @throws QueryException */ public function testSearch() { @@ -98,21 +103,23 @@ class ClientRepositoryTest extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('google'); - $search = self::$clientRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertEquals(1, $search[0]->id); - $this->assertEquals('Google Inc.', $search[0]->description); + $result = self::$clientRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(ClientData::class, $data[0]); + $this->assertEquals(1, $data[0]->id); + $this->assertEquals('Google Inc.', $data[0]->description); $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('prueba'); - $search = self::$clientRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$clientRepository->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); } /** @@ -264,6 +271,9 @@ class ClientRepositoryTest extends DatabaseTestCase /** * Comprobar la obtención de clientes por Id en lote + * + * @throws ConstraintException + * @throws QueryException */ public function testGetByIdBatch() { @@ -273,14 +283,7 @@ class ClientRepositoryTest extends DatabaseTestCase } /** - * No implementado - */ - public function testCheckInUse() - { - $this->markTestSkipped('Not implemented'); - } - - /** + * @throws ConstraintException * @throws QueryException */ public function testGetAllForFilter() diff --git a/tests/EventlogRepositoryTest.php b/tests/Repositories/EventlogRepositoryTest.php similarity index 74% rename from tests/EventlogRepositoryTest.php rename to tests/Repositories/EventlogRepositoryTest.php index c33586cb..974a7e6a 100644 --- a/tests/EventlogRepositoryTest.php +++ b/tests/Repositories/EventlogRepositoryTest.php @@ -22,14 +22,16 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Exceptions\ConstraintException; use SP\DataModel\EventlogData; use SP\DataModel\ItemSearchData; use SP\Repositories\EventLog\EventlogRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class EventlogRepositoryTest @@ -63,6 +65,9 @@ class EventlogRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda de eventos por texto + * + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function testSearch() { @@ -70,32 +75,38 @@ class EventlogRepositoryTest extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('login.auth.database'); - $search = self::$eventlogRepository->search($itemSearchData); - $this->assertCount(5, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(4, $search['count']); - $this->assertEquals('login.auth.database', $search[0]->action); + $result = self::$eventlogRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(4, $result->getNumRows()); + $this->assertCount(4, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals('login.auth.database', $data[0]->action); $itemSearchData->setSeachString('login.auth.'); - $search = self::$eventlogRepository->search($itemSearchData); - $this->assertCount(5, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(4, $search['count']); + $result = self::$eventlogRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(4, $result->getNumRows()); + $this->assertCount(4, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); $itemSearchData->setSeachString('Tiempo inactivo : 0 min.'); - $search = self::$eventlogRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); + $result = self::$eventlogRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); $itemSearchData->setSeachString('prueba'); - $search = self::$eventlogRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$eventlogRepository->search($itemSearchData); + + $this->assertCount(0, $result->getDataAsArray()); + $this->assertEquals(0, $result->getNumRows()); } /** diff --git a/tests/TagRepositoryTest.php b/tests/Repositories/TagRepositoryTest.php similarity index 88% rename from tests/TagRepositoryTest.php rename to tests/Repositories/TagRepositoryTest.php index 17356234..2993ee24 100644 --- a/tests/TagRepositoryTest.php +++ b/tests/Repositories/TagRepositoryTest.php @@ -22,14 +22,16 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use SP\Core\Exceptions\QueryException; use SP\DataModel\ItemSearchData; use SP\DataModel\TagData; use SP\Repositories\DuplicatedItemException; use SP\Repositories\Tag\TagRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class TagRepositoryTest @@ -63,6 +65,9 @@ class TagRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda mediante texto + * + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function testSearch() { @@ -70,20 +75,22 @@ class TagRepositoryTest extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('www'); - $search = self::$tagRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertEquals(1, $search[0]->id); + $result = self::$tagRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(1, $data[0]->id); $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('prueba'); - $search = self::$tagRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$tagRepository->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); } /** @@ -216,6 +223,9 @@ class TagRepositoryTest extends DatabaseTestCase /** * Comprobar la obtención de etiquetas por Id en lote + * + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function testGetByIdBatch() { diff --git a/tests/UserGroupRepositoryTest.php b/tests/Repositories/UserGroupRepositoryTest.php similarity index 90% rename from tests/UserGroupRepositoryTest.php rename to tests/Repositories/UserGroupRepositoryTest.php index dca972aa..c304baac 100644 --- a/tests/UserGroupRepositoryTest.php +++ b/tests/Repositories/UserGroupRepositoryTest.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Core\Exceptions\ConstraintException; @@ -31,7 +31,9 @@ use SP\DataModel\ItemSearchData; use SP\DataModel\UserGroupData; use SP\Repositories\DuplicatedItemException; use SP\Repositories\UserGroup\UserGroupRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class UserGroupRepositoryTest @@ -88,6 +90,9 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase /** * Comprobar la obtención de grupos por nombre + * + * @throws ConstraintException + * @throws QueryException */ public function testGetByName() { @@ -237,6 +242,9 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase /** * Comprobar la búsqueda de grupos + * + * @throws ConstraintException + * @throws QueryException */ public function testSearch() { @@ -244,20 +252,22 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('Demo'); - $search = self::$userGroupRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertEquals(2, $search[0]->id); - $this->assertEquals('Demo', $search[0]->name); + $result = self::$userGroupRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(UserGroupData::class, $data[0]); + $this->assertEquals(2, $data[0]->id); + $this->assertEquals('Demo', $data[0]->name); $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('prueba'); - $search = self::$userGroupRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$userGroupRepository->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); } } diff --git a/tests/UserProfileRepositoryTest.php b/tests/Repositories/UserProfileRepositoryTest.php similarity index 89% rename from tests/UserProfileRepositoryTest.php rename to tests/Repositories/UserProfileRepositoryTest.php index f951874a..03eff63a 100644 --- a/tests/UserProfileRepositoryTest.php +++ b/tests/Repositories/UserProfileRepositoryTest.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Core\Exceptions\ConstraintException; @@ -32,7 +32,9 @@ use SP\DataModel\ProfileData; use SP\DataModel\UserProfileData; use SP\Repositories\DuplicatedItemException; use SP\Repositories\UserProfile\UserProfileRepository; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class UserProfileRepositoryTest @@ -66,6 +68,9 @@ class UserProfileRepositoryTest extends DatabaseTestCase /** * Comprobar la obtención de perfiles + * + * @throws ConstraintException + * @throws QueryException */ public function testGetAll() { @@ -80,6 +85,9 @@ class UserProfileRepositoryTest extends DatabaseTestCase /** * Comprobar la búsqueda de perfiles + * + * @throws ConstraintException + * @throws QueryException */ public function testSearch() { @@ -87,20 +95,22 @@ class UserProfileRepositoryTest extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('Demo'); - $search = self::$userProfileRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertEquals(2, $search[0]->id); - $this->assertEquals('Demo', $search[0]->name); + $result = self::$userProfileRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(2, $data[0]->id); + $this->assertEquals('Demo', $data[0]->name); // Nueva búsqueda de perfil no existente $itemSearchData->setSeachString('prueba'); - $search = self::$userProfileRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$userProfileRepository->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); } /** diff --git a/tests/UserRepositoryTest.php b/tests/Repositories/UserRepositoryTest.php similarity index 86% rename from tests/UserRepositoryTest.php rename to tests/Repositories/UserRepositoryTest.php index be8f6f45..03503eea 100644 --- a/tests/UserRepositoryTest.php +++ b/tests/Repositories/UserRepositoryTest.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Tests; +namespace SP\Tests\Repositories; use DI\DependencyException; use SP\Core\Crypt\Crypt; @@ -36,7 +36,9 @@ use SP\Repositories\DuplicatedItemException; use SP\Repositories\NoSuchItemException; use SP\Repositories\User\UserRepository; use SP\Services\User\UpdatePassRequest; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; /** * Class UserRepositoryTest @@ -121,7 +123,7 @@ class UserRepositoryTest extends DatabaseTestCase $preferences->setResultsAsCards(true); $preferences->setResultsPerPage(10); - $this->assertTrue(self::$userRepository->updatePreferencesById(2, $preferences)); + $this->assertEquals(1, self::$userRepository->updatePreferencesById(2, $preferences)); } /** @@ -173,11 +175,17 @@ class UserRepositoryTest extends DatabaseTestCase { $result = self::$userRepository->updatePassById(2, new UpdatePassRequest(Hash::hashKey('prueba123'))); - $this->assertTrue($result); + $this->assertEquals(1, $result); + + $result = self::$userRepository->updatePassById(10, new UpdatePassRequest(Hash::hashKey('prueba123'))); + $this->assertEquals(0, $result); } /** * Obtener los datos de los usuarios por Id en lote + * + * @throws ConstraintException + * @throws QueryException */ public function testGetByIdBatch() { @@ -216,9 +224,11 @@ class UserRepositoryTest extends DatabaseTestCase $userData->setIsLdap(1); $userData->setLogin('demo'); - $result = self::$userRepository->updateOnLogin($userData); + $this->assertEquals(1, self::$userRepository->updateOnLogin($userData)); - $this->assertTrue($result); + $userData->setLogin('demodedadae'); + + $this->assertEquals(0, self::$userRepository->updateOnLogin($userData)); } /** @@ -291,13 +301,16 @@ class UserRepositoryTest extends DatabaseTestCase */ public function testUpdateLastLoginById() { - $result = self::$userRepository->updateLastLoginById(2); + $this->assertEquals(1, self::$userRepository->updateLastLoginById(2)); - $this->assertTrue($result); + $this->assertEquals(0, self::$userRepository->updateLastLoginById(10)); } /** * Comprobar la búsqueda de usuarios mediante texto + * + * @throws ConstraintException + * @throws QueryException */ public function testSearch() { @@ -305,21 +318,23 @@ class UserRepositoryTest extends DatabaseTestCase $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('User A'); - $search = self::$userRepository->search($itemSearchData); - $this->assertCount(2, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(1, $search['count']); - $this->assertEquals(3, $search[0]->id); - $this->assertEquals('User A', $search[0]->name); + $result = self::$userRepository->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(\stdClass::class, $data[0]); + $this->assertEquals(3, $data[0]->id); + $this->assertEquals('User A', $data[0]->name); $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('prueba'); - $search = self::$userRepository->search($itemSearchData); - $this->assertCount(1, $search); - $this->assertArrayHasKey('count', $search); - $this->assertEquals(0, $search['count']); + $result = self::$userRepository->search($itemSearchData); + + $this->assertEquals(0, $result->getNumRows()); + $this->assertCount(0, $result->getDataAsArray()); } /** @@ -333,7 +348,7 @@ class UserRepositoryTest extends DatabaseTestCase $key = Crypt::makeSecuredKey('prueba123'); $pass = Crypt::encrypt('prueba_key', $key, 'prueba123'); - $this->assertTrue(self::$userRepository->updateMasterPassById(3, $pass, $key)); + $this->assertEquals(1, self::$userRepository->updateMasterPassById(3, $pass, $key)); $user = self::$userRepository->getById(3); diff --git a/tests/VaultTest.php b/tests/VaultTest.php new file mode 100644 index 00000000..1009cf83 --- /dev/null +++ b/tests/VaultTest.php @@ -0,0 +1,115 @@ +. + */ + +namespace SP\Tests; + +use PHPUnit\Framework\TestCase; +use SP\Core\Crypt\Vault; +use SP\Util\Util; + +/** + * Class VaultTest + * + * @package SP\Tests + */ +class VaultTest extends TestCase +{ + /** + * @var string + */ + private $key; + + /** + * Sets up the fixture, for example, open a network connection. + * This method is called before a test is executed. + * + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + */ + protected function setUp() + { + $this->key = Util::generateRandomBytes(); + } + + /** + * @covers \SP\Core\Crypt\Vault::saveData() + * @throws \Defuse\Crypto\Exception\CryptoException + */ + public function testGetData() + { + $vault = new Vault(); + $vault->saveData('prueba', $this->key); + $this->assertEquals('prueba', $vault->getData($this->key)); + + $randomData = Util::generateRandomBytes(); + + $vault = new Vault(); + $vault->saveData($randomData, $this->key); + $this->assertEquals($randomData, $vault->getData($this->key)); + } + + + /** + * @covers \SP\Core\Crypt\Vault::saveData() + * @throws \Defuse\Crypto\Exception\CryptoException + */ + public function testGetTimeSet() + { + $vault = new Vault(); + $vault->saveData('test', $this->key); + $this->assertTrue($vault->getTimeSet() !== 0); + } + + /** + * @covers \SP\Core\Crypt\Vault::saveData() + * @throws \Defuse\Crypto\Exception\CryptoException + */ + public function testReKey() + { + $vault = new Vault(); + $vault->saveData('prueba', $this->key); + + $this->assertEquals('prueba', $vault->getData($this->key)); + + $vault->reKey(1234, $this->key); + + $this->assertEquals('prueba', $vault->getData(1234)); + } + + /** + * @covers \SP\Core\Crypt\Vault::saveData() + * @throws \Defuse\Crypto\Exception\CryptoException + */ + public function testGetTimeUpdated() + { + $vault = new Vault(); + $vault->saveData('test', $this->key); + + $this->assertTrue($vault->getTimeUpdated() === 0); + + $vault->reKey(1234, $this->key); + + $this->assertTrue(is_int($vault->getTimeUpdated())); + $this->assertTrue($vault->getTimeUpdated() > 0); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c59ef972..beb39f93 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -29,7 +29,7 @@ use Doctrine\Common\Cache\ArrayCache; use SP\Config\ConfigData; use SP\Core\Context\ContextInterface; use SP\Services\User\UserLoginResponse; -use SP\Storage\DatabaseConnectionData; +use SP\Storage\Database\DatabaseConnectionData; define('APP_MODULE', 'tests'); @@ -51,6 +51,7 @@ require APP_ROOT . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'BaseFunc if (!function_exists('gettext')) { /** * @param $str + * * @return string */ function gettext($str) diff --git a/tests/res/datasets/syspass.xml b/tests/res/datasets/syspass.xml index 4be4d507..477ca0f1 100644 --- a/tests/res/datasets/syspass.xml +++ b/tests/res/datasets/syspass.xml @@ -377,5 +377,27 @@ 1 + + + 1 + 1 + 32636565386232323466343865303165663438616331373265383739636337383235383030613964376365336232333738333231326634373538663163313436 + 1 + 1 + 1528619057 + + + + + 2 + 1 + 32636565386232323466343865303165663438616331373265383739636337383235383030613964376365336232333738333231326634373538663163313436 + 104 + 1 + 1528619077 + 4F3A31393A2253505C436F72655C43727970745C5661756C74223A343A7B733A32353A220053505C436F72655C43727970745C5661756C740064617461223B733A3139303A2264656635303230306233333662326437343731666333396264336438316465353839343038393761303636393532636338613065326434313730653865653539636631666166343866323866333132653338366136323534333934386530633461613066373235353931363366316265636137353566656430316138353165363338626530666236636230396234653235623539663431333236643766363065363739363765643032613831303965353065343961313166353365653762223B733A32343A220053505C436F72655C43727970745C5661756C74006B6579223B733A3531323A226465663130303030646566353032303035336434633932363065616233643863653961666337313763313964353937363362313235653239623237616462393462333938343035663636373037653165653234313637363762396566383836336334373266653637623033373333656262383565613565636330313963396161323636303231663430306235353661376562663038386563663761613336376637653138383062383431333866623933383832616633663766353131663530653264646539643563626663383361366431336633386335386636663734633063613530383062646437393530656538346265636462316635646131336363386431653566626466616164366437313031333063313232656365333563666239356338646332383362626166303335363030646633343632643635653334653436356435646234396636306266306466636332303761353732643036386162303035333133396661373634356335363733636535613536303236666638663732613034646662356336316230366234303337383432613063663461393664623430336232346333613462303630353434336566363163643265663865316539643766663637656464616237363432323362326664376261333861656132626431663962313432626335356664643230373664386533653562386637636233376262223B733A32383A220053505C436F72655C43727970745C5661756C740074696D65536574223B693A313532383632333036353B733A33323A220053505C436F72655C43727970745C5661756C740074696D6555706461746564223B693A303B7D + 2432792431302456425842645839546157334B6146506F38504F7A4C753749412E364D74414A555448633466305A37716A445A664C41416D69795275 + + diff --git a/tests/res/datasets/syspass_eventlog.xml b/tests/res/datasets/syspass_eventlog.xml index 252e2228..87207aea 100644 --- a/tests/res/datasets/syspass_eventlog.xml +++ b/tests/res/datasets/syspass_eventlog.xml @@ -1,165 +1,175 @@ - - - - 1 - 1527720052 - - - - 172.19.0.1 - login.auth.database - -Tipo : authDatabase -Usuario : admin - INFO - - - 2 - 1527720053 - admin - 1 - 172.19.0.1 - login.session.load - -Usuario : admin - INFO - - - 3 - 1527720053 - admin - 1 - 172.19.0.1 - login.preferences.load - - INFO - - - 4 - 1527723866 - admin - 1 - 172.19.0.1 - logout - Finalizar sesión -Usuario : admin -Tiempo inactivo : 0.02 min. -Tiempo total : 63.72 min. - INFO - - - 5 - 1527796875 - - - - 172.19.0.1 - login.auth.database - -Tipo : authDatabase -Usuario : admin - INFO - - - 6 - 1527796876 - admin - 1 - 172.19.0.1 - login.session.load - -Usuario : admin - INFO - - - 7 - 1527796876 - admin - 1 - 172.19.0.1 - login.preferences.load - - INFO - - - 8 - 1527800242 - - - - 172.19.0.1 - login.auth.database - -Tipo : authDatabase -Usuario : admin - INFO - - - 9 - 1527800243 - admin - 1 - 172.19.0.1 - login.session.load - -Usuario : admin - INFO - - - 10 - 1527800243 - admin - 1 - 172.19.0.1 - login.preferences.load - - INFO - - - 11 - 1527807482 - - - - 172.19.0.1 - login.auth.database - -Tipo : authDatabase -Usuario : admin - INFO - - - 12 - 1527807483 - admin - 1 - 172.19.0.1 - login.session.load - -Usuario : admin - INFO - - - 13 - 1527807483 - admin - 1 - 172.19.0.1 - login.preferences.load - - INFO - - - 14 - 1527807531 - admin - 1 - 172.19.0.1 - logout - Finalizar sesión -Usuario : admin -Tiempo inactivo : 0 min. -Tiempo total : 0.92 min. - INFO - - - + + + + 1 + 1527720052 + - + + 172.19.0.1 + login.auth.database + + Tipo : authDatabase + Usuario : admin + + INFO + + + 2 + 1527720053 + admin + 1 + 172.19.0.1 + login.session.load + + Usuario : admin + + INFO + + + 3 + 1527720053 + admin + 1 + 172.19.0.1 + login.preferences.load + + INFO + + + 4 + 1527723866 + admin + 1 + 172.19.0.1 + logout + Finalizar sesión + Usuario : admin + Tiempo inactivo : 0.02 min. + Tiempo total : 63.72 min. + + INFO + + + 5 + 1527796875 + - + + 172.19.0.1 + login.auth.database + + Tipo : authDatabase + Usuario : admin + + INFO + + + 6 + 1527796876 + admin + 1 + 172.19.0.1 + login.session.load + + Usuario : admin + + INFO + + + 7 + 1527796876 + admin + 1 + 172.19.0.1 + login.preferences.load + + INFO + + + 8 + 1527800242 + - + + 172.19.0.1 + login.auth.database + + Tipo : authDatabase + Usuario : admin + + INFO + + + 9 + 1527800243 + admin + 1 + 172.19.0.1 + login.session.load + + Usuario : admin + + INFO + + + 10 + 1527800243 + admin + 1 + 172.19.0.1 + login.preferences.load + + INFO + + + 11 + 1527807482 + - + + 172.19.0.1 + login.auth.database + + Tipo : authDatabase + Usuario : admin + + INFO + + + 12 + 1527807483 + admin + 1 + 172.19.0.1 + login.session.load + + Usuario : admin + + INFO + + + 13 + 1527807483 + admin + 1 + 172.19.0.1 + login.preferences.load + + INFO + + + 14 + 1527807531 + admin + 1 + 172.19.0.1 + logout + Finalizar sesión + Usuario : admin + Tiempo inactivo : 0 min. + Tiempo total : 0.92 min. + + INFO + + +