Passed
Push — devel-3.0 ( 4622e9...e92637 )
by Rubén
03:18
created

AccountHistoryManagerController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A searchAction() 0 11 2
A deleteAction() 0 29 3
A initialize() 0 5 1
A getSearchGrid() 0 7 1
A restoreAction() 0 25 3
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use SP\Core\Acl\Acl;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\Http\JsonResponse;
31
use SP\Modules\Web\Controllers\Helpers\Grid\AccountHistoryGrid;
32
use SP\Modules\Web\Controllers\Traits\ItemTrait;
33
use SP\Modules\Web\Controllers\Traits\JsonTrait;
34
use SP\Services\Account\AccountHistoryService;
35
use SP\Services\Account\AccountService;
36
37
/**
38
 * Class AccountHistoryManagerController
39
 *
40
 * @package SP\Modules\Web\Controllers
41
 */
42
final class AccountHistoryManagerController extends ControllerBase
43
{
44
    use JsonTrait, ItemTrait;
0 ignored issues
show
introduced by
The trait SP\Modules\Web\Controllers\Traits\ItemTrait requires some properties which are not provided by SP\Modules\Web\Controlle...istoryManagerController: $data, $key
Loading history...
45
46
    /**
47
     * @var AccountHistoryService
48
     */
49
    protected $accountHistoryService;
50
51
    /**
52
     * @throws \SP\Core\Exceptions\ConstraintException
53
     * @throws \SP\Core\Exceptions\QueryException
54
     */
55
    public function searchAction()
56
    {
57
        if (!$this->acl->checkUserAccess(Acl::ACCOUNTMGR_HISTORY_SEARCH)) {
58
            return;
59
        }
60
61
        $this->view->addTemplate('datagrid-table', 'grid');
62
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
63
        $this->view->assign('data', $this->getSearchGrid());
64
65
        $this->returnJsonResponseData(['html' => $this->render()]);
66
    }
67
68
    /**
69
     * getSearchGrid
70
     *
71
     * @return $this
72
     * @throws \SP\Core\Exceptions\ConstraintException
73
     * @throws \SP\Core\Exceptions\QueryException
74
     */
75
    protected function getSearchGrid()
76
    {
77
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
78
79
        $historyGrid = $this->dic->get(AccountHistoryGrid::class);
80
81
        return $historyGrid->updatePager($historyGrid->getGrid($this->accountHistoryService->search($itemSearchData)), $itemSearchData);
82
    }
83
84
    /**
85
     * Delete action
86
     *
87
     * @param $id
88
     */
89
    public function deleteAction($id = null)
90
    {
91
        try {
92
            if ($id === null) {
93
                $this->accountHistoryService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
94
95
                $this->eventDispatcher->notifyEvent('delete.accountHistory.selection',
96
                    new Event($this, EventMessage::factory()->addDescription(__u('Cuentas eliminadas')))
97
                );
98
99
                $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Cuentas eliminadas'));
100
            } else {
101
                $accountDetails = $this->accountHistoryService->getById($id);
102
103
                $this->accountHistoryService->delete($id);
104
105
                $this->eventDispatcher->notifyEvent('delete.accountHistory',
106
                    new Event($this, EventMessage::factory()
107
                        ->addDescription(__u('Cuenta eliminada'))
108
                        ->addDetail(__u('Cuenta'), $accountDetails->getName())
109
                        ->addDetail(__u('Cliente'), $accountDetails->getClientName()))
110
                );
111
112
                $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Cuenta eliminada'));
113
            }
114
        } catch (\Exception $e) {
115
            processException($e);
116
117
            $this->returnJsonResponseException($e);
118
        }
119
    }
120
121
    /**
122
     * Saves restore action
123
     *
124
     * @param int $id Account's history ID
125
     */
126
    public function restoreAction($id)
127
    {
128
        try {
129
            $accountDetails = $this->accountHistoryService->getById($id);
130
131
            $accountService = $this->dic->get(AccountService::class);
132
133
            if ($accountDetails->isModify) {
134
                $accountService->editRestore($id, $accountDetails->getAccountId());
135
            } else {
136
                $accountService->createFromHistory($accountDetails);
137
            }
138
139
            $this->eventDispatcher->notifyEvent('restore.accountHistory',
140
                new Event($this, EventMessage::factory()
141
                    ->addDescription(__u('Cuenta restaurada'))
142
                    ->addDetail(__u('Cuenta'), $accountDetails->getName())
143
                    ->addDetail(__u('Cliente'), $accountDetails->getClientName()))
144
            );
145
146
            $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Cuenta restaurada'));
147
        } catch (\Exception $e) {
148
            processException($e);
149
150
            $this->returnJsonResponseException($e);
151
        }
152
    }
153
154
    /**
155
     * Initialize class
156
     *
157
     * @throws \SP\Services\Auth\AuthException
158
     */
159
    protected function initialize()
160
    {
161
        $this->checkLoggedIn();
162
163
        $this->accountHistoryService = $this->dic->get(AccountHistoryService::class);
164
    }
165
}