| Total Complexity | 40 |
| Total Lines | 366 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AuthTokenController 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 AuthTokenController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | final class AuthTokenController extends ControllerBase implements CrudControllerInterface |
||
| 57 | { |
||
| 58 | use JsonTrait, ItemTrait; |
||
|
|
|||
| 59 | |||
| 60 | /** |
||
| 61 | * @var AuthTokenService |
||
| 62 | */ |
||
| 63 | protected $authTokenService; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var UserService |
||
| 67 | */ |
||
| 68 | protected $userService; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Search action |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | * @throws DependencyException |
||
| 75 | * @throws NotFoundException |
||
| 76 | * @throws ConstraintException |
||
| 77 | * @throws QueryException |
||
| 78 | * @throws SPException |
||
| 79 | */ |
||
| 80 | public function searchAction() |
||
| 81 | { |
||
| 82 | $this->checkSecurityToken($this->previousSk, $this->request); |
||
| 83 | |||
| 84 | if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_SEARCH)) { |
||
| 85 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->view->addTemplate('datagrid-table', 'grid'); |
||
| 89 | $this->view->assign('index', $this->request->analyzeInt('activetab', 0)); |
||
| 90 | $this->view->assign('data', $this->getSearchGrid()); |
||
| 91 | |||
| 92 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * getSearchGrid |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | * @throws DependencyException |
||
| 100 | * @throws NotFoundException |
||
| 101 | * @throws ConstraintException |
||
| 102 | * @throws QueryException |
||
| 103 | */ |
||
| 104 | protected function getSearchGrid() |
||
| 105 | { |
||
| 106 | $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request); |
||
| 107 | |||
| 108 | $authTokenGrid = $this->dic->get(AuthTokenGrid::class); |
||
| 109 | |||
| 110 | return $authTokenGrid->updatePager( |
||
| 111 | $authTokenGrid->getGrid($this->authTokenService->search($itemSearchData)), |
||
| 112 | $itemSearchData |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Create action |
||
| 118 | */ |
||
| 119 | public function createAction() |
||
| 120 | { |
||
| 121 | try { |
||
| 122 | $this->checkSecurityToken($this->previousSk, $this->request); |
||
| 123 | |||
| 124 | if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_CREATE)) { |
||
| 125 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->view->assign('header', __('New Authorization')); |
||
| 129 | $this->view->assign('isView', false); |
||
| 130 | $this->view->assign('route', 'authToken/saveCreate'); |
||
| 131 | |||
| 132 | $this->setViewData(); |
||
| 133 | |||
| 134 | $this->eventDispatcher->notifyEvent('show.authToken.create', new Event($this)); |
||
| 135 | |||
| 136 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||
| 137 | } catch (Exception $e) { |
||
| 138 | processException($e); |
||
| 139 | |||
| 140 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 141 | |||
| 142 | return $this->returnJsonResponseException($e); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Sets view data for displaying auth token's data |
||
| 148 | * |
||
| 149 | * @param $authTokenId |
||
| 150 | * |
||
| 151 | * @throws ConstraintException |
||
| 152 | * @throws QueryException |
||
| 153 | * @throws ServiceException |
||
| 154 | */ |
||
| 155 | protected function setViewData($authTokenId = null) |
||
| 156 | { |
||
| 157 | $this->view->addTemplate('auth_token', 'itemshow'); |
||
| 158 | |||
| 159 | $authToken = $authTokenId ? $this->authTokenService->getById($authTokenId) : new AuthTokenData(); |
||
| 160 | $this->view->assign('authToken', $authToken); |
||
| 161 | |||
| 162 | if($this->acl->checkUserAccess(Acl::AUTHTOKEN_ONLY_USER) && !$this->session->getUserData()->getIsAdminApp()) { |
||
| 163 | $tokenUserId = $this->session->getUserData()->getId(); |
||
| 164 | $selectItems = [$this->userService->getById($tokenUserId)]; |
||
| 165 | } else { |
||
| 166 | $selectItems = UserService::getItemsBasic(); |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->view->assign('users', SelectItemAdapter::factory($selectItems)->getItemsFromModelSelected([$authToken->getUserId()])); |
||
| 170 | $this->view->assign('actions', SelectItemAdapter::factory(AuthTokenService::getTokenActions())->getItemsFromArraySelected([$authToken->getActionId()])); |
||
| 171 | |||
| 172 | $this->view->assign('nextAction', Acl::getActionRoute(Acl::ACCESS_MANAGE)); |
||
| 173 | |||
| 174 | if ($this->view->isView === true) { |
||
| 175 | $this->view->assign('disabled', 'disabled'); |
||
| 176 | $this->view->assign('readonly', 'readonly'); |
||
| 177 | } else { |
||
| 178 | $this->view->assign('disabled', false); |
||
| 179 | $this->view->assign('readonly', false); |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->view->assign('customFields', $this->getCustomFieldsForItem(Acl::AUTHTOKEN, $authTokenId)); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Edit action |
||
| 187 | * |
||
| 188 | * @param $id |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function editAction($id) |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Delete action |
||
| 223 | * |
||
| 224 | * @param $id |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function deleteAction($id = null) |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Saves create action |
||
| 275 | */ |
||
| 276 | public function saveCreateAction() |
||
| 277 | { |
||
| 278 | try { |
||
| 279 | $this->checkSecurityToken($this->previousSk, $this->request); |
||
| 280 | $tokenUserId = $this->session->getUserData()->getId(); |
||
| 281 | |||
| 282 | if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_CREATE) || (!$this->session->getUserData()->getIsAdminApp() && $tokenUserId !== $this->request->analyzeInt('users'))) { |
||
| 283 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | $form = new AuthTokenForm($this->dic); |
||
| 288 | $form->validate(Acl::AUTHTOKEN_CREATE); |
||
| 289 | $form->validate(Acl::AUTHTOKEN_ONLY_USER); |
||
| 290 | |||
| 291 | $apiTokenData = $form->getItemData(); |
||
| 292 | |||
| 293 | $id = $this->authTokenService->create($apiTokenData); |
||
| 294 | |||
| 295 | $this->addCustomFieldsForItem(Acl::AUTHTOKEN, $id, $this->request); |
||
| 296 | |||
| 297 | $this->eventDispatcher->notifyEvent('create.authToken', new Event($this)); |
||
| 298 | |||
| 299 | return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Authorization added')); |
||
| 300 | } catch (ValidationException $e) { |
||
| 301 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, $e->getMessage()); |
||
| 302 | } catch (Exception $e) { |
||
| 303 | processException($e); |
||
| 304 | |||
| 305 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 306 | |||
| 307 | return $this->returnJsonResponseException($e); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Saves edit action |
||
| 313 | * |
||
| 314 | * @param $id |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function saveEditAction($id) |
||
| 319 | { |
||
| 320 | try { |
||
| 321 | $this->checkSecurityToken($this->previousSk, $this->request); |
||
| 322 | $tokenUserId = $this->authTokenService->getById($id)->getUserId(); |
||
| 323 | |||
| 324 | if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_EDIT) || !$this->authTokenOnlyUser($tokenUserId)) { |
||
| 325 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||
| 326 | } |
||
| 327 | |||
| 328 | $form = new AuthTokenForm($this->dic, $id); |
||
| 329 | $form->validate(Acl::AUTHTOKEN_EDIT); |
||
| 330 | |||
| 331 | if ($form->isRefresh()) { |
||
| 332 | $this->authTokenService->refreshAndUpdate($form->getItemData()); |
||
| 333 | |||
| 334 | $this->eventDispatcher->notifyEvent('refresh.authToken', |
||
| 335 | new Event($this, |
||
| 336 | EventMessage::factory() |
||
| 337 | ->addDescription(__u('Authorization updated')) |
||
| 338 | ->addDetail(__u('Authorization'), $id)) |
||
| 339 | ); |
||
| 340 | } else { |
||
| 341 | $this->authTokenService->update($form->getItemData()); |
||
| 342 | |||
| 343 | $this->eventDispatcher->notifyEvent('edit.authToken', |
||
| 344 | new Event($this, |
||
| 345 | EventMessage::factory() |
||
| 346 | ->addDescription(__u('Authorization updated')) |
||
| 347 | ->addDetail(__u('Authorization'), $id)) |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | $this->updateCustomFieldsForItem(Acl::AUTHTOKEN, $id, $this->request); |
||
| 352 | |||
| 353 | return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Authorization updated')); |
||
| 354 | } catch (ValidationException $e) { |
||
| 355 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, $e->getMessage()); |
||
| 356 | } catch (Exception $e) { |
||
| 357 | processException($e); |
||
| 358 | |||
| 359 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 360 | |||
| 361 | return $this->returnJsonResponseException($e); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * View action |
||
| 367 | * |
||
| 368 | * @param $id |
||
| 369 | * |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function viewAction($id) |
||
| 373 | { |
||
| 374 | try { |
||
| 375 | $this->checkSecurityToken($this->previousSk, $this->request); |
||
| 376 | $tokenUserId = $this->authTokenService->getById($id)->getUserId(); |
||
| 377 | |||
| 378 | if (!$this->acl->checkUserAccess(Acl::AUTHTOKEN_VIEW) || !$this->authTokenOnlyUser($tokenUserId)) { |
||
| 379 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||
| 380 | } |
||
| 381 | |||
| 382 | $this->view->assign('header', __('View Authorization')); |
||
| 383 | $this->view->assign('isView', true); |
||
| 384 | |||
| 385 | |||
| 386 | $this->setViewData($id); |
||
| 387 | |||
| 388 | $this->eventDispatcher->notifyEvent('show.authToken', |
||
| 389 | new Event($this, EventMessage::factory() |
||
| 390 | ->addDescription(__u('Authorization viewed')) |
||
| 391 | ->addDetail(__u('Authorization'), $id)) |
||
| 392 | ); |
||
| 393 | |||
| 394 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||
| 395 | } catch (Exception $e) { |
||
| 396 | processException($e); |
||
| 397 | |||
| 398 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 399 | |||
| 400 | return $this->returnJsonResponseException($e); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Initialize class |
||
| 406 | * |
||
| 407 | * @throws AuthException |
||
| 408 | * @throws DependencyException |
||
| 409 | * @throws NotFoundException |
||
| 410 | * @throws SessionTimeout |
||
| 411 | */ |
||
| 412 | protected function initialize() |
||
| 418 | } |
||
| 419 | |||
| 420 | protected function authTokenOnlyUser($tokenUserId) { |
||
| 422 | } |
||
| 423 | } |
||
| 424 |