From 6a71fe337227a77d01ce14dac21aeffa6843afe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 6 Nov 2022 16:38:05 +0100 Subject: [PATCH] chore: Remove unused methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Account/In/AccountRepositoryInterface.php | 75 ++++++++++- .../Repositories/AccountRepository.php | 121 ++++++------------ .../Repositories/AccountRepositoryTest.php | 60 ++++----- 3 files changed, 137 insertions(+), 119 deletions(-) diff --git a/lib/SP/Domain/Account/In/AccountRepositoryInterface.php b/lib/SP/Domain/Account/In/AccountRepositoryInterface.php index 9da9960c..649ef050 100644 --- a/lib/SP/Domain/Account/In/AccountRepositoryInterface.php +++ b/lib/SP/Domain/Account/In/AccountRepositoryInterface.php @@ -29,20 +29,18 @@ use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\AccountHistoryData; -use SP\Domain\Account\Search\AccountSearchFilter; +use SP\DataModel\ItemSearchData; use SP\Domain\Account\Services\AccountPasswordRequest; use SP\Domain\Account\Services\AccountRequest; -use SP\Domain\Common\In\RepositoryInterface; use SP\Domain\Common\Out\SimpleModel; use SP\Infrastructure\Database\QueryResult; -use SP\Mvc\Model\QueryCondition; /** * Class AccountRepository * * @package Services */ -interface AccountRepositoryInterface extends RepositoryInterface +interface AccountRepositoryInterface { /** * Devolver el número total de cuentas @@ -173,4 +171,73 @@ interface AccountRepositoryInterface extends RepositoryInterface * @throws QueryException */ public function getAccountsPassData(): QueryResult; + + /** + * Crea una nueva cuenta en la BBDD + * + * @param AccountRequest $accountRequest + * + * @return int + * @throws ConstraintException + * @throws QueryException + */ + public function create(AccountRequest $accountRequest): int; + + /** + * Elimina los datos de una cuenta en la BBDD. + * + * @param int $id + * + * @return bool + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function delete(int $id): bool; + + /** + * Updates an item + * + * @param AccountRequest $accountRequest + * + * @return int + * @throws SPException + */ + public function update(AccountRequest $accountRequest): int; + + /** + * Returns the item for given id + * + * @param int $id + * + * @return QueryResult + */ + public function getById(int $id): QueryResult; + + /** + * Returns all the items + * + * @return QueryResult + */ + public function getAll(): QueryResult; + + /** + * Deletes all the items for given ids + * + * @param array $ids + * + * @return int + * @throws ConstraintException + * @throws QueryException + */ + public function deleteByIdBatch(array $ids): int; + + /** + * Searches for items by a given filter + * + * @param ItemSearchData $itemSearchData + * + * @return QueryResult + */ + public function search(ItemSearchData $itemSearchData): QueryResult; + } \ No newline at end of file diff --git a/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php b/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php index 9882c122..acd8756f 100644 --- a/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php +++ b/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php @@ -25,7 +25,6 @@ namespace SP\Infrastructure\Account\Repositories; use Aura\SqlQuery\QueryFactory; -use RuntimeException; use SP\Core\Context\ContextInterface; use SP\Core\Events\EventDispatcherInterface; use SP\Core\Exceptions\ConstraintException; @@ -150,33 +149,33 @@ final class AccountRepository extends Repository implements AccountRepositoryInt /** * Crea una nueva cuenta en la BBDD * - * @param AccountRequest $itemData + * @param AccountRequest $accountRequest * * @return int * @throws ConstraintException * @throws QueryException */ - public function create($itemData): int + public function create(AccountRequest $accountRequest): int { $query = $this->queryFactory ->newInsert() ->into('Account') ->cols([ - 'clientId' => $itemData->clientId, - 'categoryId' => $itemData->categoryId, - 'name' => $itemData->name, - 'login' => $itemData->login, - 'url' => $itemData->url, - 'pass' => $itemData->pass, - 'key' => $itemData->key, - 'notes' => $itemData->notes, - 'userId' => $itemData->userId, - 'userGroupId' => $itemData->userGroupId, - 'userEditId' => $itemData->userId, - 'isPrivate' => $itemData->isPrivate, - 'isPrivateGroup' => $itemData->isPrivateGroup, - 'passDateChange' => $itemData->passDateChange, - 'parentId' => $itemData->parentId, + 'clientId' => $accountRequest->clientId, + 'categoryId' => $accountRequest->categoryId, + 'name' => $accountRequest->name, + 'login' => $accountRequest->login, + 'url' => $accountRequest->url, + 'pass' => $accountRequest->pass, + 'key' => $accountRequest->key, + 'notes' => $accountRequest->notes, + 'userId' => $accountRequest->userId, + 'userGroupId' => $accountRequest->userGroupId, + 'userEditId' => $accountRequest->userId, + 'isPrivate' => $accountRequest->isPrivate, + 'isPrivateGroup' => $accountRequest->isPrivateGroup, + 'passDateChange' => $accountRequest->passDateChange, + 'parentId' => $accountRequest->parentId, ]) ->set('dateAdd', 'NOW()') ->set('passDate', 'UNIX_TIMESTAMP()'); @@ -285,11 +284,11 @@ final class AccountRepository extends Repository implements AccountRepositoryInt * * @param int $id * - * @return int EL número de cuentas eliminadas - * @throws ConstraintException - * @throws QueryException + * @return bool + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ - public function delete(int $id): int + public function delete(int $id): bool { $query = $this->queryFactory ->newDelete() @@ -299,44 +298,44 @@ final class AccountRepository extends Repository implements AccountRepositoryInt $queryData = QueryData::build($query)->setOnErrorMessage(__u('Error while deleting the account')); - return $this->db->doQuery($queryData)->getAffectedNumRows(); + return $this->db->doQuery($queryData)->getAffectedNumRows() === 1; } /** * Updates an item * - * @param AccountRequest $itemData + * @param AccountRequest $accountRequest * * @return int * @throws SPException */ - public function update($itemData): int + public function update(AccountRequest $accountRequest): int { $query = $this->queryFactory ->newUpdate() ->table('Account') - ->where('id = :id', ['id' => $itemData->id]) + ->where('id = :id', ['id' => $accountRequest->id]) ->cols([ - 'clientId' => $itemData->clientId, - 'categoryId' => $itemData->categoryId, - 'name' => $itemData->name, - 'login' => $itemData->login, - 'url' => $itemData->url, - 'notes' => $itemData->notes, - 'userEditId' => $itemData->userEditId, - 'passDateChange' => $itemData->passDateChange, - 'isPrivate' => $itemData->isPrivate, - 'isPrivateGroup' => $itemData->isPrivateGroup, - 'parentId' => $itemData->parentId, + 'clientId' => $accountRequest->clientId, + 'categoryId' => $accountRequest->categoryId, + 'name' => $accountRequest->name, + 'login' => $accountRequest->login, + 'url' => $accountRequest->url, + 'notes' => $accountRequest->notes, + 'userEditId' => $accountRequest->userEditId, + 'passDateChange' => $accountRequest->passDateChange, + 'isPrivate' => $accountRequest->isPrivate, + 'isPrivateGroup' => $accountRequest->isPrivateGroup, + 'parentId' => $accountRequest->parentId, ]) ->set('dateEdit', 'NOW()'); - if ($itemData->changeUserGroup) { - $query->col('userGroupId', $itemData->userGroupId); + if ($accountRequest->changeUserGroup) { + $query->col('userGroupId', $accountRequest->userGroupId); } - if ($itemData->changeOwner) { - $query->col('userId', $itemData->userId); + if ($accountRequest->changeOwner) { + $query->col('userId', $accountRequest->userId); } $queryData = QueryData::build($query)->setOnErrorMessage(__u('Error while updating the account')); @@ -475,16 +474,6 @@ final class AccountRepository extends Repository implements AccountRepositoryInt return $this->db->doSelect(QueryData::build($query)); } - /** - * Returns all the items for given ids - * - * @param array $ids - */ - public function getByIdBatch(array $ids): QueryResult - { - throw new RuntimeException('Not implemented'); - } - /** * Deletes all the items for given ids * @@ -510,36 +499,6 @@ final class AccountRepository extends Repository implements AccountRepositoryInt return $this->db->doQuery($queryData)->getAffectedNumRows(); } - /** - * Checks whether the item is in use or not - * - * @param $id int - */ - public function checkInUse(int $id): bool - { - throw new RuntimeException('Not implemented'); - } - - /** - * Checks whether the item is duplicated on updating - * - * @param mixed $itemData - */ - public function checkDuplicatedOnUpdate($itemData): bool - { - throw new RuntimeException('Not implemented'); - } - - /** - * Checks whether the item is duplicated on adding - * - * @param mixed $itemData - */ - public function checkDuplicatedOnAdd($itemData): bool - { - throw new RuntimeException('Not implemented'); - } - /** * Searches for items by a given filter * diff --git a/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php b/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php index 77814d72..6e9ce04f 100644 --- a/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php +++ b/tests/SP/Infrastructure/Account/Repositories/AccountRepositoryTest.php @@ -26,7 +26,6 @@ namespace SP\Tests\Infrastructure\Account\Repositories; use Aura\SqlQuery\QueryFactory; use PHPUnit\Framework\Constraint\Callback; -use RuntimeException; use SP\DataModel\AccountHistoryData; use SP\DataModel\ItemSearchData; use SP\Domain\Account\Services\AccountFilterUser; @@ -395,7 +394,32 @@ class AccountRepositoryTest extends UnitaryTestCase ->with($callback) ->willReturn($expected); - $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->delete($id)); + $this->assertTrue($this->accountRepository->delete($id)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDeleteWithouResults(): void + { + $id = 1; + $expected = new QueryResult(); + $expected->setAffectedNumRows(0); + + $callback = new Callback( + static function (QueryData $arg) use ($id) { + return $arg->getQuery()->getBindValues()['id'] === $id + && !empty($arg->getQuery()->getStatement()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertFalse($this->accountRepository->delete($id)); } /** @@ -595,38 +619,6 @@ class AccountRepositoryTest extends UnitaryTestCase $this->accountRepository->getAll(); } - public function testGetByIdBatch() - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Not implemented'); - - $this->accountRepository->getByIdBatch([]); - } - - public function testCheckInUse() - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Not implemented'); - - $this->accountRepository->checkInUse(0); - } - - public function testCheckDuplicatedOnUpdate() - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Not implemented'); - - $this->accountRepository->checkDuplicatedOnUpdate(null); - } - - public function testCheckDuplicatedOnAdd() - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Not implemented'); - - $this->accountRepository->checkDuplicatedOnAdd(null); - } - /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException