| Total Complexity | 96 |
| Total Lines | 743 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AccountService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AccountService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | final class AccountService extends Service implements AccountServiceInterface |
||
| 65 | { |
||
| 66 | use ServiceItemTrait; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var AccountRepository |
||
| 70 | */ |
||
| 71 | protected $accountRepository; |
||
| 72 | /** |
||
| 73 | * @var AccountToUserGroupRepository |
||
| 74 | */ |
||
| 75 | protected $accountToUserGroupRepository; |
||
| 76 | /** |
||
| 77 | * @var AccountToUserRepository |
||
| 78 | */ |
||
| 79 | protected $accountToUserRepository; |
||
| 80 | /** |
||
| 81 | * @var AccountToTagRepository |
||
| 82 | */ |
||
| 83 | protected $accountToTagRepository; |
||
| 84 | /** |
||
| 85 | * @var ItemPresetService |
||
| 86 | */ |
||
| 87 | protected $itemPresetService; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param AccountDetailsResponse $accountDetailsResponse |
||
| 91 | * |
||
| 92 | * @return AccountService |
||
| 93 | * @throws QueryException |
||
| 94 | * @throws ConstraintException |
||
| 95 | */ |
||
| 96 | public function withUsersById(AccountDetailsResponse $accountDetailsResponse) |
||
| 97 | { |
||
| 98 | $accountDetailsResponse->setUsers($this->accountToUserRepository->getUsersByAccountId($accountDetailsResponse->getId())->getDataAsArray()); |
||
| 99 | |||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param AccountDetailsResponse $accountDetailsResponse |
||
| 105 | * |
||
| 106 | * @return AccountService |
||
| 107 | * @throws QueryException |
||
| 108 | * @throws ConstraintException |
||
| 109 | */ |
||
| 110 | public function withUserGroupsById(AccountDetailsResponse $accountDetailsResponse) |
||
| 111 | { |
||
| 112 | $accountDetailsResponse->setUserGroups($this->accountToUserGroupRepository->getUserGroupsByAccountId($accountDetailsResponse->getId())->getDataAsArray()); |
||
| 113 | |||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param AccountDetailsResponse $accountDetailsResponse |
||
| 119 | * |
||
| 120 | * @return AccountService |
||
| 121 | * @throws QueryException |
||
| 122 | * @throws ConstraintException |
||
| 123 | */ |
||
| 124 | public function withTagsById(AccountDetailsResponse $accountDetailsResponse) |
||
| 125 | { |
||
| 126 | $accountDetailsResponse->setTags($this->accountToTagRepository->getTagsByAccountId($accountDetailsResponse->getId())->getDataAsArray()); |
||
| 127 | |||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param $id |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | * @throws QueryException |
||
| 136 | * @throws ConstraintException |
||
| 137 | */ |
||
| 138 | public function incrementViewCounter($id) |
||
| 139 | { |
||
| 140 | return $this->accountRepository->incrementViewCounter($id); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $id |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | * @throws QueryException |
||
| 148 | * @throws ConstraintException |
||
| 149 | */ |
||
| 150 | public function incrementDecryptCounter($id) |
||
| 151 | { |
||
| 152 | return $this->accountRepository->incrementDecryptCounter($id); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param $id |
||
| 157 | * |
||
| 158 | * @return AccountPassData |
||
| 159 | * @throws QueryException |
||
| 160 | * @throws ConstraintException |
||
| 161 | * @throws NoSuchItemException |
||
| 162 | */ |
||
| 163 | public function getPasswordForId($id) |
||
| 164 | { |
||
| 165 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter(); |
||
| 166 | |||
| 167 | $result = $this->accountRepository->getPasswordForId($id, $queryFilter); |
||
| 168 | |||
| 169 | if ($result->getNumRows() === 0) { |
||
| 170 | throw new NoSuchItemException(__u('Account not found')); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $result->getData(); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param AccountRequest $accountRequest |
||
| 178 | * |
||
| 179 | * @return int |
||
| 180 | * @throws QueryException |
||
| 181 | * @throws SPException |
||
| 182 | * @throws ConstraintException |
||
| 183 | * @throws NoSuchPropertyException |
||
| 184 | */ |
||
| 185 | public function create(AccountRequest $accountRequest) |
||
| 186 | { |
||
| 187 | $userData = $this->context->getUserData(); |
||
| 188 | |||
| 189 | $accountRequest->changePermissions = AccountAclService::getShowPermission( |
||
| 190 | $userData, |
||
| 191 | $this->context->getUserProfile()); |
||
| 192 | |||
| 193 | if (empty($accountRequest->userGroupId) |
||
| 194 | || !$accountRequest->changePermissions |
||
| 195 | ) { |
||
| 196 | $accountRequest->userGroupId = $userData->getUserGroupId(); |
||
| 197 | } |
||
| 198 | |||
| 199 | if (empty($accountRequest->userId) |
||
| 200 | || !$accountRequest->changePermissions |
||
| 201 | ) { |
||
| 202 | $accountRequest->userId = $userData->getId(); |
||
| 203 | } |
||
| 204 | |||
| 205 | if (empty($accountRequest->key)) { |
||
| 206 | $pass = $this->getPasswordEncrypted($accountRequest->pass); |
||
| 207 | |||
| 208 | $accountRequest->pass = $pass['pass']; |
||
| 209 | $accountRequest->key = $pass['key']; |
||
| 210 | } |
||
| 211 | |||
| 212 | $this->setPresetPrivate($accountRequest); |
||
| 213 | |||
| 214 | $accountRequest->id = $this->accountRepository->create($accountRequest); |
||
| 215 | |||
| 216 | $this->addItems($accountRequest); |
||
| 217 | |||
| 218 | $this->addPresetPermissions($accountRequest->id); |
||
| 219 | |||
| 220 | return $accountRequest->id; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Devolver los datos de la clave encriptados |
||
| 225 | * |
||
| 226 | * @param string $pass |
||
| 227 | * @param string $masterPass Clave maestra a utilizar |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | * @throws ServiceException |
||
| 231 | */ |
||
| 232 | public function getPasswordEncrypted($pass, $masterPass = null) |
||
| 233 | { |
||
| 234 | try { |
||
| 235 | if ($masterPass === null) { |
||
| 236 | $masterPass = $this->getMasterKeyFromContext(); |
||
| 237 | } |
||
| 238 | |||
| 239 | if (empty($masterPass)) { |
||
| 240 | throw new ServiceException(__u('Master password not set')); |
||
| 241 | } |
||
| 242 | |||
| 243 | $out['key'] = Crypt::makeSecuredKey($masterPass); |
||
|
|
|||
| 244 | $out['pass'] = Crypt::encrypt($pass, $out['key'], $masterPass); |
||
| 245 | |||
| 246 | if (strlen($out['pass']) > 1000 || strlen($out['key']) > 1000) { |
||
| 247 | throw new ServiceException(__u('Internal error')); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $out; |
||
| 251 | } catch (CryptoException $e) { |
||
| 252 | throw new ServiceException(__u('Internal error')); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param AccountRequest $accountRequest |
||
| 258 | * |
||
| 259 | * @throws QueryException |
||
| 260 | * @throws ConstraintException |
||
| 261 | * @throws NoSuchPropertyException |
||
| 262 | * @throws NoSuchItemException |
||
| 263 | */ |
||
| 264 | private function setPresetPrivate(AccountRequest $accountRequest) |
||
| 265 | { |
||
| 266 | $userData = $this->context->getUserData(); |
||
| 267 | $itemPreset = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PRIVATE); |
||
| 268 | |||
| 269 | if ($itemPreset !== null |
||
| 270 | && $itemPreset->getFixed() |
||
| 271 | ) { |
||
| 272 | $accountPrivate = $itemPreset->hydrate(AccountPrivate::class); |
||
| 273 | |||
| 274 | $userId = $accountRequest->userId; |
||
| 275 | |||
| 276 | if ($userId === null && $accountRequest->id > 0) { |
||
| 277 | $userId = $this->getById($accountRequest->id)->getAccountVData()->getUserId(); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($userData->getId() === $userId) { |
||
| 281 | $accountRequest->isPrivate = (int)$accountPrivate->isPrivateUser(); |
||
| 282 | } |
||
| 283 | |||
| 284 | if ($userData->getUserGroupId() === $accountRequest->userGroupId) { |
||
| 285 | $accountRequest->isPrivateGroup = (int)$accountPrivate->isPrivateGroup(); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param int $id |
||
| 292 | * |
||
| 293 | * @return AccountDetailsResponse |
||
| 294 | * @throws QueryException |
||
| 295 | * @throws NoSuchItemException |
||
| 296 | * @throws ConstraintException |
||
| 297 | */ |
||
| 298 | public function getById($id) |
||
| 299 | { |
||
| 300 | $result = $this->accountRepository->getById($id); |
||
| 301 | |||
| 302 | if ($result->getNumRows() === 0) { |
||
| 303 | throw new NoSuchItemException(__u('The account doesn\'t exist')); |
||
| 304 | } |
||
| 305 | |||
| 306 | return new AccountDetailsResponse($id, $result->getData()); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Adds external items to the account |
||
| 311 | * |
||
| 312 | * @param AccountRequest $accountRequest |
||
| 313 | */ |
||
| 314 | private function addItems(AccountRequest $accountRequest) |
||
| 315 | { |
||
| 316 | try { |
||
| 317 | |||
| 318 | if ($accountRequest->changePermissions) { |
||
| 319 | if (is_array($accountRequest->userGroupsView) |
||
| 320 | && !empty($accountRequest->userGroupsView) |
||
| 321 | ) { |
||
| 322 | $this->accountToUserGroupRepository->addByType($accountRequest, false); |
||
| 323 | } |
||
| 324 | |||
| 325 | if (is_array($accountRequest->userGroupsEdit) |
||
| 326 | && !empty($accountRequest->userGroupsEdit) |
||
| 327 | ) { |
||
| 328 | $this->accountToUserGroupRepository->addByType($accountRequest, true); |
||
| 329 | } |
||
| 330 | |||
| 331 | if (is_array($accountRequest->usersView) |
||
| 332 | && !empty($accountRequest->usersView) |
||
| 333 | ) { |
||
| 334 | $this->accountToUserRepository->addByType($accountRequest, false); |
||
| 335 | } |
||
| 336 | |||
| 337 | if (is_array($accountRequest->usersEdit) |
||
| 338 | && !empty($accountRequest->usersEdit) |
||
| 339 | ) { |
||
| 340 | $this->accountToUserRepository->addByType($accountRequest, true); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | if (is_array($accountRequest->tags) |
||
| 345 | && !empty($accountRequest->tags) |
||
| 346 | ) { |
||
| 347 | $this->accountToTagRepository->add($accountRequest); |
||
| 348 | } |
||
| 349 | } catch (SPException $e) { |
||
| 350 | logger($e->getMessage()); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param int $accountId |
||
| 356 | * |
||
| 357 | * @throws QueryException |
||
| 358 | * @throws ConstraintException |
||
| 359 | * @throws NoSuchPropertyException |
||
| 360 | */ |
||
| 361 | private function addPresetPermissions(int $accountId) |
||
| 362 | { |
||
| 363 | $itemPresetData = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION); |
||
| 364 | |||
| 365 | if ($itemPresetData !== null |
||
| 366 | && $itemPresetData->getFixed() |
||
| 367 | ) { |
||
| 368 | $userData = $this->context->getUserData(); |
||
| 369 | $accountPermission = $itemPresetData->hydrate(AccountPermission::class); |
||
| 370 | |||
| 371 | $accountRequest = new AccountRequest(); |
||
| 372 | $accountRequest->id = $accountId; |
||
| 373 | $accountRequest->usersView = array_diff($accountPermission->getUsersView(), [$userData->getId()]); |
||
| 374 | $accountRequest->usersEdit = array_diff($accountPermission->getUsersEdit(), [$userData->getId()]); |
||
| 375 | $accountRequest->userGroupsView = array_diff($accountPermission->getUserGroupsView(), [$userData->getUserGroupId()]); |
||
| 376 | $accountRequest->userGroupsEdit = array_diff($accountPermission->getUserGroupsEdit(), [$userData->getUserGroupId()]); |
||
| 377 | |||
| 378 | if (!empty($accountRequest->usersView)) { |
||
| 379 | $this->accountToUserRepository->addByType($accountRequest, false); |
||
| 380 | } |
||
| 381 | |||
| 382 | if (!empty($accountRequest->usersEdit)) { |
||
| 383 | $this->accountToUserRepository->addByType($accountRequest, true); |
||
| 384 | } |
||
| 385 | |||
| 386 | if (!empty($accountRequest->userGroupsView)) { |
||
| 387 | $this->accountToUserGroupRepository->addByType($accountRequest, false); |
||
| 388 | } |
||
| 389 | |||
| 390 | if (!empty($accountRequest->userGroupsEdit)) { |
||
| 391 | $this->accountToUserGroupRepository->addByType($accountRequest, true); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param AccountHistoryData $data |
||
| 398 | * |
||
| 399 | * @return int |
||
| 400 | * @throws QueryException |
||
| 401 | * @throws ConstraintException |
||
| 402 | */ |
||
| 403 | public function createFromHistory(AccountHistoryData $data) |
||
| 404 | { |
||
| 405 | $accountRequest = new AccountRequest(); |
||
| 406 | $accountRequest->name = $data->getName(); |
||
| 407 | $accountRequest->categoryId = $data->getCategoryId(); |
||
| 408 | $accountRequest->clientId = $data->getClientId(); |
||
| 409 | $accountRequest->url = $data->getUrl(); |
||
| 410 | $accountRequest->login = $data->getLogin(); |
||
| 411 | $accountRequest->pass = $data->getPass(); |
||
| 412 | $accountRequest->key = $data->getKey(); |
||
| 413 | $accountRequest->notes = $data->getNotes(); |
||
| 414 | $accountRequest->userId = $data->getUserId(); |
||
| 415 | $accountRequest->userGroupId = $data->getUserGroupId(); |
||
| 416 | $accountRequest->passDateChange = $data->getPassDateChange(); |
||
| 417 | $accountRequest->parentId = $data->getParentId(); |
||
| 418 | $accountRequest->isPrivate = $data->getIsPrivate(); |
||
| 419 | $accountRequest->isPrivateGroup = $data->getIsPrivateGroup(); |
||
| 420 | |||
| 421 | return $this->accountRepository->create($accountRequest); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Updates external items for the account |
||
| 426 | * |
||
| 427 | * @param AccountRequest $accountRequest |
||
| 428 | * |
||
| 429 | * @throws Exception |
||
| 430 | */ |
||
| 431 | public function update(AccountRequest $accountRequest) |
||
| 432 | { |
||
| 433 | $this->transactionAware(function () use ($accountRequest) { |
||
| 434 | $userData = $this->context->getUserData(); |
||
| 435 | $userProfile = $this->context->getUserProfile(); |
||
| 436 | |||
| 437 | $accountRequest->changePermissions = |
||
| 438 | AccountAclService::getShowPermission($userData, $userProfile); |
||
| 439 | |||
| 440 | if ($accountRequest->changePermissions) { |
||
| 441 | $account = $this->getById($accountRequest->id)->getAccountVData(); |
||
| 442 | |||
| 443 | $accountRequest->changeOwner = $accountRequest->userId > 0 |
||
| 444 | && ($userData->getIsAdminApp() |
||
| 445 | || $userData->getIsAdminAcc() |
||
| 446 | || ($userProfile->isAccPermission() |
||
| 447 | && $userData->getId() === $account->getUserId())); |
||
| 448 | |||
| 449 | $accountRequest->changeUserGroup = $accountRequest->userGroupId > 0 |
||
| 450 | && ($userData->getIsAdminApp() |
||
| 451 | || $userData->getIsAdminAcc() |
||
| 452 | || ($userProfile->isAccPermission() |
||
| 453 | && ($userData->getUserGroupId() === $account->getUserGroupId()) |
||
| 454 | || $userData->getId() === $account->getUserId())); |
||
| 455 | } |
||
| 456 | |||
| 457 | $this->addHistory($accountRequest->id); |
||
| 458 | |||
| 459 | $this->setPresetPrivate($accountRequest); |
||
| 460 | |||
| 461 | $this->accountRepository->update($accountRequest); |
||
| 462 | |||
| 463 | $this->updateItems($accountRequest); |
||
| 464 | |||
| 465 | $this->addPresetPermissions($accountRequest->id); |
||
| 466 | }); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param int $accountId |
||
| 471 | * @param bool $isDelete |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | * @throws NoSuchItemException |
||
| 475 | * @throws QueryException |
||
| 476 | * @throws ServiceException |
||
| 477 | * @throws ConstraintException |
||
| 478 | */ |
||
| 479 | private function addHistory($accountId, $isDelete = false) |
||
| 480 | { |
||
| 481 | $accountHistoryRepository = $this->dic->get(AccountHistoryService::class); |
||
| 482 | $configService = $this->dic->get(ConfigService::class); |
||
| 483 | |||
| 484 | return $accountHistoryRepository->create( |
||
| 485 | new AccountHistoryCreateDto( |
||
| 486 | $accountId, |
||
| 487 | !$isDelete, |
||
| 488 | $isDelete, |
||
| 489 | $configService->getByParam('masterPwd')) |
||
| 490 | ); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Updates external items for the account |
||
| 495 | * |
||
| 496 | * @param AccountRequest $accountRequest |
||
| 497 | * |
||
| 498 | * @throws QueryException |
||
| 499 | * @throws ConstraintException |
||
| 500 | */ |
||
| 501 | private function updateItems(AccountRequest $accountRequest) |
||
| 502 | { |
||
| 503 | if ($accountRequest->changePermissions) { |
||
| 504 | if ($accountRequest->userGroupsView !== null) { |
||
| 505 | if (count($accountRequest->userGroupsView) > 0) { |
||
| 506 | $this->accountToUserGroupRepository->updateByType($accountRequest, false); |
||
| 507 | } else { |
||
| 508 | $this->accountToUserGroupRepository->deleteTypeByAccountId($accountRequest->id, false); |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | if ($accountRequest->userGroupsEdit !== null) { |
||
| 513 | if (count($accountRequest->userGroupsEdit) > 0) { |
||
| 514 | $this->accountToUserGroupRepository->updateByType($accountRequest, true); |
||
| 515 | } else { |
||
| 516 | $this->accountToUserGroupRepository->deleteTypeByAccountId($accountRequest->id, true); |
||
| 517 | } |
||
| 518 | } |
||
| 519 | |||
| 520 | if ($accountRequest->usersView !== null) { |
||
| 521 | if (count($accountRequest->usersView) > 0) { |
||
| 522 | $this->accountToUserRepository->updateByType($accountRequest, false); |
||
| 523 | } else { |
||
| 524 | $this->accountToUserRepository->deleteTypeByAccountId($accountRequest->id, false); |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | if ($accountRequest->usersEdit !== null) { |
||
| 529 | if (count($accountRequest->usersEdit) > 0) { |
||
| 530 | $this->accountToUserRepository->updateByType($accountRequest, true); |
||
| 531 | } else { |
||
| 532 | $this->accountToUserRepository->deleteTypeByAccountId($accountRequest->id, true); |
||
| 533 | } |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | if ($accountRequest->tags !== null) { |
||
| 538 | if (count($accountRequest->tags) > 0) { |
||
| 539 | $this->accountToTagRepository->update($accountRequest); |
||
| 540 | } else { |
||
| 541 | $this->accountToTagRepository->deleteByAccountId($accountRequest->id); |
||
| 542 | } |
||
| 543 | } |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Update accounts in bulk mode |
||
| 548 | * |
||
| 549 | * @param AccountBulkRequest $request |
||
| 550 | * |
||
| 551 | * @throws ServiceException |
||
| 552 | */ |
||
| 553 | public function updateBulk(AccountBulkRequest $request) |
||
| 554 | { |
||
| 555 | $this->transactionAware(function () use ($request) { |
||
| 556 | foreach ($request->getItemsId() as $itemId) { |
||
| 557 | $accountRequest = $request->getAccountRequestForId($itemId); |
||
| 558 | |||
| 559 | $this->addHistory($accountRequest->id); |
||
| 560 | |||
| 561 | $this->accountRepository->updateBulk($accountRequest); |
||
| 562 | |||
| 563 | $this->updateItems($accountRequest); |
||
| 564 | } |
||
| 565 | }); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param AccountRequest $accountRequest |
||
| 570 | * |
||
| 571 | * @throws Exception |
||
| 572 | */ |
||
| 573 | public function editPassword(AccountRequest $accountRequest) |
||
| 584 | }); |
||
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Updates an already encrypted password data from a master password changing action |
||
| 589 | * |
||
| 590 | * @param AccountPasswordRequest $accountRequest |
||
| 591 | * |
||
| 592 | * @return bool |
||
| 593 | * @throws ConstraintException |
||
| 594 | * @throws QueryException |
||
| 595 | */ |
||
| 596 | public function updatePasswordMasterPass(AccountPasswordRequest $accountRequest) |
||
| 597 | { |
||
| 598 | return $this->accountRepository->updatePassword($accountRequest); |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @param $historyId |
||
| 603 | * @param $accountId |
||
| 604 | * |
||
| 605 | * @throws Exception |
||
| 606 | */ |
||
| 607 | public function editRestore($historyId, $accountId) |
||
| 608 | { |
||
| 609 | $this->transactionAware(function () use ($historyId, $accountId) { |
||
| 610 | $this->addHistory($accountId); |
||
| 611 | |||
| 612 | if (!$this->accountRepository->editRestore($historyId, $this->context->getUserData()->getId())) { |
||
| 613 | throw new ServiceException(__u('Error on restoring the account')); |
||
| 614 | } |
||
| 615 | }); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @param $id |
||
| 620 | * |
||
| 621 | * @return AccountService |
||
| 622 | * @throws ServiceException |
||
| 623 | */ |
||
| 624 | public function delete($id) |
||
| 625 | { |
||
| 626 | $this->transactionAware(function () use ($id) { |
||
| 627 | $this->addHistory($id, 1); |
||
| 628 | |||
| 629 | if ($this->accountRepository->delete($id) === 0) { |
||
| 630 | throw new NoSuchItemException(__u('Account not found')); |
||
| 631 | } |
||
| 632 | }); |
||
| 633 | |||
| 634 | return $this; |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @param array $ids |
||
| 639 | * |
||
| 640 | * @return AccountService |
||
| 641 | * @throws SPException |
||
| 642 | * @throws ServiceException |
||
| 643 | */ |
||
| 644 | public function deleteByIdBatch(array $ids) |
||
| 645 | { |
||
| 646 | if ($this->accountRepository->deleteByIdBatch($ids) === 0) { |
||
| 647 | throw new ServiceException(__u('Error while deleting the accounts')); |
||
| 648 | } |
||
| 649 | |||
| 650 | return $this; |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @param $accountId |
||
| 655 | * |
||
| 656 | * @return array |
||
| 657 | * @throws QueryException |
||
| 658 | * @throws ConstraintException |
||
| 659 | */ |
||
| 660 | public function getForUser($accountId = null) |
||
| 661 | { |
||
| 662 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter(); |
||
| 663 | |||
| 664 | if (null !== $accountId) { |
||
| 665 | $queryFilter->addFilter('Account.id <> ? AND (Account.parentId = 0 OR Account.parentId IS NULL)', [$accountId]); |
||
| 666 | } |
||
| 667 | |||
| 668 | return $this->accountRepository->getForUser($queryFilter)->getDataAsArray(); |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @param $accountId |
||
| 673 | * |
||
| 674 | * @return array |
||
| 675 | * @throws QueryException |
||
| 676 | * @throws ConstraintException |
||
| 677 | */ |
||
| 678 | public function getLinked($accountId) |
||
| 679 | { |
||
| 680 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilter(); |
||
| 681 | |||
| 682 | $queryFilter->addFilter('Account.parentId = ?', [$accountId]); |
||
| 683 | |||
| 684 | return $this->accountRepository->getLinked($queryFilter)->getDataAsArray(); |
||
| 685 | } |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param $id |
||
| 689 | * |
||
| 690 | * @return AccountPassData |
||
| 691 | * @throws QueryException |
||
| 692 | * @throws ConstraintException |
||
| 693 | * @throws NoSuchItemException |
||
| 694 | */ |
||
| 695 | public function getPasswordHistoryForId($id) |
||
| 696 | { |
||
| 697 | $queryFilter = $this->dic->get(AccountFilterUser::class)->getFilterHistory(); |
||
| 698 | $queryFilter->addFilter('AccountHistory.id = ?', [$id]); |
||
| 699 | |||
| 700 | $result = $this->accountRepository->getPasswordHistoryForId($queryFilter); |
||
| 701 | |||
| 702 | if ($result->getNumRows() === 0) { |
||
| 703 | throw new NoSuchItemException(__u('The account doesn\'t exist')); |
||
| 704 | } |
||
| 705 | |||
| 706 | return $result->getData(); |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @return AccountData[] |
||
| 711 | * @throws QueryException |
||
| 712 | * @throws ConstraintException |
||
| 713 | */ |
||
| 714 | public function getAllBasic() |
||
| 715 | { |
||
| 716 | return $this->accountRepository->getAll()->getDataAsArray(); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @param ItemSearchData $itemSearchData |
||
| 721 | * |
||
| 722 | * @return QueryResult |
||
| 723 | * @throws QueryException |
||
| 724 | * @throws ConstraintException |
||
| 725 | */ |
||
| 726 | public function search(ItemSearchData $itemSearchData) |
||
| 727 | { |
||
| 728 | return $this->accountRepository->search($itemSearchData); |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Devolver el número total de cuentas |
||
| 733 | * |
||
| 734 | * @return stdClass |
||
| 735 | * @throws QueryException |
||
| 736 | * @throws ConstraintException |
||
| 737 | */ |
||
| 738 | public function getTotalNumAccounts() |
||
| 739 | { |
||
| 740 | return $this->accountRepository->getTotalNumAccounts()->num; |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Obtener los datos de una cuenta. |
||
| 745 | * |
||
| 746 | * @param $id |
||
| 747 | * |
||
| 748 | * @return AccountExtData |
||
| 749 | * @throws QueryException |
||
| 750 | * @throws NoSuchItemException |
||
| 751 | * @throws ConstraintException |
||
| 752 | */ |
||
| 753 | public function getDataForLink($id) |
||
| 754 | { |
||
| 755 | $result = $this->accountRepository->getDataForLink($id); |
||
| 756 | |||
| 757 | if ($result->getNumRows() === 0) { |
||
| 758 | throw new NoSuchItemException(__u('The account doesn\'t exist')); |
||
| 759 | } |
||
| 760 | |||
| 761 | return $result->getData(); |
||
| 762 | } |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Obtener los datos relativos a la clave de todas las cuentas. |
||
| 766 | * |
||
| 767 | * @return array Con los datos de la clave |
||
| 768 | * @throws QueryException |
||
| 769 | * @throws ConstraintException |
||
| 770 | */ |
||
| 771 | public function getAccountsPassData() |
||
| 772 | { |
||
| 773 | return $this->accountRepository->getAccountsPassData(); |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Obtener las cuentas de una búsqueda. |
||
| 778 | * |
||
| 779 | * @param AccountSearchFilter $accountSearchFilter |
||
| 780 | * |
||
| 781 | * @return QueryResult |
||
| 782 | * @throws QueryException |
||
| 783 | * @throws SPException |
||
| 784 | * @throws ConstraintException |
||
| 785 | */ |
||
| 786 | public function getByFilter(AccountSearchFilter $accountSearchFilter) |
||
| 787 | { |
||
| 788 | $accountFilterUser = $this->dic->get(AccountFilterUser::class); |
||
| 789 | |||
| 790 | return $this->accountRepository->getByFilter( |
||
| 791 | $accountSearchFilter, |
||
| 792 | $accountFilterUser->getFilter($accountSearchFilter->getGlobalSearch()) |
||
| 793 | ); |
||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @throws ContainerExceptionInterface |
||
| 798 | * @throws NotFoundExceptionInterface |
||
| 799 | */ |
||
| 800 | protected function initialize() |
||
| 807 | } |
||
| 808 | } |