. */ namespace SP\Modules\Web\Controllers\AccountHistoryManager; use Exception; use JsonException; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Account\Ports\AccountHistoryService; use SP\Domain\Account\Ports\AccountService; use SP\Domain\Http\Dtos\JsonMessage; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\WebControllerHelper; /** * Class RestoreController * * @package SP\Modules\Web\Controllers */ final class RestoreController extends ControllerBase { use JsonTrait; private AccountHistoryService $accountHistoryService; private AccountService $accountService; public function __construct( Application $application, WebControllerHelper $webControllerHelper, AccountHistoryService $accountHistoryService, AccountService $accountService ) { $this->accountHistoryService = $accountHistoryService; $this->accountService = $accountService; parent::__construct($application, $webControllerHelper); $this->checkLoggedIn(); } /** * Saves restore action * * @param int $id Account's history ID * * @return bool * @throws JsonException */ public function restoreAction(int $id): bool { try { $accountDetails = $this->accountHistoryService->getById($id); if ($accountDetails->isModify) { $this->accountService->restoreModified($id, $accountDetails->getAccountId()); } else { $this->accountService->restoreRemoved($accountDetails); } $this->eventDispatcher->notify( 'restore.accountHistory', new Event( $this, EventMessage::factory() ->addDescription(__u('Account restored')) ->addDetail(__u('Account'), $accountDetails->getName()) ->addDetail(__u('Client'), $accountDetails->getClientName()) ) ); return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Account restored')); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); return $this->returnJsonResponseException($e); } } }