| Total Complexity | 45 |
| Total Lines | 479 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ManageController 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 ManageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ManageController extends AbstractActionController |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var RepositoryService |
||
| 35 | */ |
||
| 36 | private $repositories; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var |
||
| 40 | */ |
||
| 41 | private $coreNavigation; |
||
| 42 | |||
| 43 | private $forms; |
||
| 44 | |||
| 45 | private $appOptions; |
||
| 46 | |||
| 47 | private $appEvents; |
||
| 48 | |||
| 49 | private $translator; |
||
| 50 | |||
| 51 | private $container; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * ManageController constructor. |
||
| 55 | * |
||
| 56 | * @param RepositoryService $repositories |
||
| 57 | */ |
||
| 58 | public function __construct( |
||
| 59 | RepositoryService $repositories, |
||
| 60 | $coreNavigation, |
||
| 61 | $forms, |
||
| 62 | $appOptions, |
||
| 63 | $appEvents, |
||
| 64 | $translator, |
||
| 65 | $container |
||
| 66 | ) { |
||
| 67 | $this->repositories = $repositories; |
||
| 68 | $this->coreNavigation = $coreNavigation; |
||
| 69 | $this->forms = $forms; |
||
| 70 | $this->appOptions = $appOptions; |
||
| 71 | $this->appEvents = $appEvents; |
||
| 72 | $this->translator = $translator; |
||
| 73 | $this->container = $container; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param ContainerInterface $container |
||
| 78 | * |
||
| 79 | * @return ManageController |
||
| 80 | */ |
||
| 81 | public static function factory(ContainerInterface $container) |
||
| 82 | { |
||
| 83 | $ob = new self( |
||
| 84 | $container->get('Core/RepositoryService'), |
||
| 85 | $container->get('Core/Navigation'), |
||
| 86 | $container->get('forms'), |
||
| 87 | $container->get('Applications/Options'), |
||
| 88 | $container->get('Applications/Events'), |
||
| 89 | $container->get('translator'), |
||
| 90 | $container |
||
| 91 | ); |
||
| 92 | return $ob; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * (non-PHPdoc) |
||
| 97 | * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch() |
||
| 98 | */ |
||
| 99 | public function onDispatch(\Zend\Mvc\MvcEvent $e) |
||
| 100 | { |
||
| 101 | $routeMatch = $e->getRouteMatch(); |
||
| 102 | $action = $this->params()->fromQuery('action'); |
||
| 103 | |||
| 104 | if ($routeMatch && $action) { |
||
| 105 | $routeMatch->setParam('action', $action); |
||
| 106 | } |
||
| 107 | |||
| 108 | return parent::onDispatch($e); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * List applications |
||
| 113 | */ |
||
| 114 | public function indexAction() |
||
| 115 | { |
||
| 116 | return $this->pagination([ |
||
|
|
|||
| 117 | 'params' => ['Application_List', ['q', 'job', 'page' => 1, 'unread', 'status' => 'all']], |
||
| 118 | 'paginator' => ['Applications', 'as' => 'applications'], |
||
| 119 | 'form' => [ |
||
| 120 | ApplicationsFilter::class, |
||
| 121 | 'as' => 'form' |
||
| 122 | ], |
||
| 123 | ]); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Detail view of an application |
||
| 128 | * |
||
| 129 | * @return array|JsonModel|ViewModel |
||
| 130 | */ |
||
| 131 | public function detailAction() |
||
| 132 | { |
||
| 133 | if ('refresh-rating' == $this->params()->fromQuery('do')) { |
||
| 134 | return $this->refreshRatingAction(); |
||
| 135 | } |
||
| 136 | |||
| 137 | $nav = $this->coreNavigation; |
||
| 138 | $page = $nav->findByRoute('lang/applications'); |
||
| 139 | $page->setActive(); |
||
| 140 | |||
| 141 | /* @var \Applications\Repository\Application$repository */ |
||
| 142 | $repository = $this->repositories->get('Applications/Application'); |
||
| 143 | /* @var Application $application */ |
||
| 144 | $application = $repository->find($this->params('id')); |
||
| 145 | |||
| 146 | if (!$application) { |
||
| 147 | $this->response->setStatusCode(410); |
||
| 148 | $model = new ViewModel( |
||
| 149 | array( |
||
| 150 | 'content' => /*@translate*/ 'Invalid apply id' |
||
| 151 | ) |
||
| 152 | ); |
||
| 153 | $model->setTemplate('applications/error/not-found'); |
||
| 154 | return $model; |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->acl($application, 'read'); |
||
| 158 | |||
| 159 | $applicationIsUnread = false; |
||
| 160 | if ($application->isUnreadBy($this->auth('id')) && $application->getStatus()) { |
||
| 161 | $application->addReadBy($this->auth('id')); |
||
| 162 | $applicationIsUnread = true; |
||
| 163 | $application->changeStatus( |
||
| 164 | $application->getStatus(), |
||
| 165 | sprintf(/*@translate*/ 'Application was read by %s', |
||
| 166 | $this->auth()->getUser()->getInfo()->getDisplayName() |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | $format=$this->params()->fromQuery('format'); |
||
| 174 | |||
| 175 | if ($application->isDraft()) { |
||
| 176 | $list = false; |
||
| 177 | } else { |
||
| 178 | $list = $this->paginationParams('Applications\Index', $repository); |
||
| 179 | $list->setCurrent($application->getId()); |
||
| 180 | } |
||
| 181 | |||
| 182 | $return = array( |
||
| 183 | 'application'=> $application, |
||
| 184 | 'list' => $list, |
||
| 185 | 'isUnread' => $applicationIsUnread, |
||
| 186 | 'format' => 'html' |
||
| 187 | ); |
||
| 188 | switch ($format) { |
||
| 189 | case 'json': |
||
| 190 | /*@deprecated - must be refactored */ |
||
| 191 | $viewModel = new JsonModel(); |
||
| 192 | $viewModel->setVariables( |
||
| 193 | /*array( |
||
| 194 | 'application' => */$this->builders |
||
| 195 | ->get('JsonApplication') |
||
| 196 | ->unbuild($application) |
||
| 197 | ); |
||
| 198 | $viewModel->setVariable('isUnread', $applicationIsUnread); |
||
| 199 | $return = $viewModel; |
||
| 200 | break; |
||
| 201 | case 'pdf': |
||
| 202 | // @TODO: [ZF3] Refactor this so we can inject Core/Html2Pdf service during controller creation |
||
| 203 | $pdf = $this->container->get('Core/Html2Pdf'); |
||
| 204 | $return['format'] = $format; |
||
| 205 | break; |
||
| 206 | default: |
||
| 207 | $contentCollector = $this->getPluginManager()->get('Core/ContentCollector'); |
||
| 208 | $contentCollector->setTemplate('applications/manage/details/action-buttons'); |
||
| 209 | $actionButtons = $contentCollector->trigger('application.detail.actionbuttons', $application); |
||
| 210 | |||
| 211 | $return = new ViewModel($return); |
||
| 212 | $return->addChild($actionButtons, 'externActionButtons'); |
||
| 213 | |||
| 214 | $allowSubsequentAttachmentUpload = $this->appOptions |
||
| 215 | ->getAllowSubsequentAttachmentUpload(); |
||
| 216 | |||
| 217 | if ($allowSubsequentAttachmentUpload |
||
| 218 | && $this->acl($application, Application::PERMISSION_SUBSEQUENT_ATTACHMENT_UPLOAD, 'test') |
||
| 219 | ) { |
||
| 220 | $attachmentsForm = $this->forms |
||
| 221 | ->get('Applications/Attachments'); |
||
| 222 | $attachmentsForm->bind($application->getAttachments()); |
||
| 223 | |||
| 224 | /* @var $request \Zend\Http\PhpEnvironment\Request */ |
||
| 225 | $request = $this->getRequest(); |
||
| 226 | |||
| 227 | if ($request->isPost() && $attachmentsForm->get('return')->getValue() === $request->getPost('return')) { |
||
| 228 | $data = array_merge( |
||
| 229 | $attachmentsForm->getOption('use_post_array') ? $request->getPost()->toArray() : [], |
||
| 230 | $attachmentsForm->getOption('use_files_array') ? $request->getFiles()->toArray() : [] |
||
| 231 | ); |
||
| 232 | $attachmentsForm->setData($data); |
||
| 233 | |||
| 234 | if (!$attachmentsForm->isValid()) { |
||
| 235 | return new JsonModel([ |
||
| 236 | 'valid' => false, |
||
| 237 | 'errors' => $attachmentsForm->getMessages() |
||
| 238 | ]); |
||
| 239 | } |
||
| 240 | |||
| 241 | $content = $attachmentsForm->getHydrator() |
||
| 242 | ->getLastUploadedFile() |
||
| 243 | ->getUri(); |
||
| 244 | |||
| 245 | return new JsonModel([ |
||
| 246 | 'valid' => $attachmentsForm->isValid(), |
||
| 247 | 'content' => $content |
||
| 248 | ]); |
||
| 249 | } |
||
| 250 | |||
| 251 | $return->setVariable('attachmentsForm', $attachmentsForm); |
||
| 252 | } |
||
| 253 | |||
| 254 | break; |
||
| 255 | } |
||
| 256 | |||
| 257 | return $return; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Refreshes the rating of an application |
||
| 262 | * |
||
| 263 | * @throws \DomainException |
||
| 264 | * @return \Zend\View\Model\ViewModel |
||
| 265 | */ |
||
| 266 | public function refreshRatingAction() |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Attaches a social profile to an application |
||
| 284 | * @throws \InvalidArgumentException |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function socialProfileAction() |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Changes the status of an application |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function statusAction() |
||
| 321 | { |
||
| 322 | $applicationId = $this->params('id'); |
||
| 323 | /* @var \Applications\Repository\Application $repository */ |
||
| 324 | $repository = $this->repositories->get('Applications/Application'); |
||
| 325 | /* @var Application $application */ |
||
| 326 | $application = $repository->find($applicationId); |
||
| 327 | |||
| 328 | /* @var Request $request */ |
||
| 329 | $request = $this->getRequest(); |
||
| 330 | |||
| 331 | if (!$application) { |
||
| 332 | throw new \InvalidArgumentException('Could not find application.'); |
||
| 333 | } |
||
| 334 | |||
| 335 | $this->acl($application, 'change'); |
||
| 336 | |||
| 337 | $jsonFormat = 'json' == $this->params()->fromQuery('format'); |
||
| 338 | $status = $this->params('status', Status::CONFIRMED); |
||
| 339 | $settings = $this->settings(); |
||
| 340 | |||
| 341 | if (in_array($status, array(Status::INCOMING))) { |
||
| 342 | $application->changeStatus($status); |
||
| 343 | if ($request->isXmlHttpRequest()) { |
||
| 344 | $response = $this->getResponse(); |
||
| 345 | $response->setContent('ok'); |
||
| 346 | return $response; |
||
| 347 | } |
||
| 348 | if ($jsonFormat) { |
||
| 349 | return array( |
||
| 350 | 'status' => 'success', |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | return $this->redirect()->toRoute('lang/applications/detail', array(), true); |
||
| 354 | } |
||
| 355 | |||
| 356 | $events = $this->appEvents; |
||
| 357 | |||
| 358 | /* @var ApplicationEvent $event */ |
||
| 359 | $event = $events->getEvent( |
||
| 360 | ApplicationEvent::EVENT_APPLICATION_STATUS_CHANGE, |
||
| 361 | $this, |
||
| 362 | [ |
||
| 363 | 'application' => $application, |
||
| 364 | 'status' => $status, |
||
| 365 | 'user' => $this->auth()->getUser(), |
||
| 366 | ] |
||
| 367 | ); |
||
| 368 | |||
| 369 | $event->setIsPostRequest($request->isPost()); |
||
| 370 | $event->setPostData($request->getPost()); |
||
| 371 | $events->trigger($event->getName(), $event); |
||
| 372 | |||
| 373 | $params = $event->getFormData(); |
||
| 374 | |||
| 375 | |||
| 376 | if ($request->isPost()) { |
||
| 377 | if ($jsonFormat) { |
||
| 378 | return array( |
||
| 379 | 'status' => 'success', |
||
| 380 | ); |
||
| 381 | } |
||
| 382 | $this->notification()->success($event->getNotification()); |
||
| 383 | return $this->redirect()->toRoute('lang/applications/detail', array(), true); |
||
| 384 | } |
||
| 385 | |||
| 386 | if ($jsonFormat) { |
||
| 387 | return $params; |
||
| 388 | } |
||
| 389 | |||
| 390 | /* @var \Applications\Form\Mail $form */ |
||
| 391 | $form = $this->forms->get('Applications/Mail'); |
||
| 392 | $form->populateValues($params); |
||
| 393 | |||
| 394 | |||
| 395 | |||
| 396 | $recipient = $params['to']; |
||
| 397 | |||
| 398 | return [ |
||
| 399 | 'recipient' => $recipient, |
||
| 400 | 'form' => $form |
||
| 401 | ]; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Forwards an application via Email |
||
| 406 | * |
||
| 407 | * @throws \InvalidArgumentException |
||
| 408 | * @return \Zend\View\Model\JsonModel |
||
| 409 | */ |
||
| 410 | public function forwardAction() |
||
| 411 | { |
||
| 412 | $emailAddress = $this->params()->fromQuery('email'); |
||
| 413 | /* @var \Applications\Entity\Application $application */ |
||
| 414 | $application = $this->repositories->get('Applications/Application') |
||
| 415 | ->find($this->params('id')); |
||
| 416 | |||
| 417 | $this->acl($application, 'forward'); |
||
| 418 | |||
| 419 | $translator = $this->translator; |
||
| 420 | |||
| 421 | if (!$emailAddress) { |
||
| 422 | throw new \InvalidArgumentException('An email address must be supplied.'); |
||
| 423 | } |
||
| 424 | |||
| 425 | $params = array( |
||
| 426 | 'ok' => true, |
||
| 427 | 'text' => sprintf($translator->translate('Forwarded application to %s'), $emailAddress) |
||
| 428 | ); |
||
| 429 | |||
| 430 | try { |
||
| 431 | $userName = $this->auth('info')->getDisplayName(); |
||
| 432 | $fromAddress = $application->getJob()->getContactEmail(); |
||
| 433 | $mailOptions = array( |
||
| 434 | 'application' => $application, |
||
| 435 | 'to' => $emailAddress, |
||
| 436 | 'from' => array($fromAddress => $userName) |
||
| 437 | ); |
||
| 438 | $this->mailer('Applications/Forward', $mailOptions, true); |
||
| 439 | $this->notification()->success($params['text']); |
||
| 440 | } catch (\Exception $ex) { |
||
| 441 | $params = array( |
||
| 442 | 'ok' => false, |
||
| 443 | 'text' => sprintf($translator->translate('Forward application to %s failed.'), $emailAddress) |
||
| 444 | ); |
||
| 445 | $this->notification()->error($params['text']); |
||
| 446 | } |
||
| 447 | $application->changeStatus($application->getStatus(), $params['text']); |
||
| 448 | return new JsonModel($params); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Deletes an application |
||
| 453 | * |
||
| 454 | * @return array|\Zend\Http\Response |
||
| 455 | */ |
||
| 456 | public function deleteAction() |
||
| 457 | { |
||
| 458 | $id = $this->params('id'); |
||
| 459 | $repositories= $this->repositories; |
||
| 460 | $repository = $repositories->get('Applications/Application'); |
||
| 461 | $application = $repository->find($id); |
||
| 462 | |||
| 463 | if (!$application) { |
||
| 464 | throw new \DomainException('Application not found.'); |
||
| 465 | } |
||
| 466 | |||
| 467 | $this->acl($application, 'delete'); |
||
| 468 | |||
| 469 | $events = $this->appEvents; |
||
| 470 | $events->trigger(ApplicationEvent::EVENT_APPLICATION_PRE_DELETE, $this, [ 'application' => $application ]); |
||
| 471 | |||
| 472 | $repositories->remove($application); |
||
| 473 | |||
| 474 | if ('json' == $this->params()->fromQuery('format')) { |
||
| 475 | return ['status' => 'success']; |
||
| 476 | } |
||
| 477 | |||
| 478 | return $this->redirect()->toRoute('lang/applications', array(), true); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Move an application to talent pool |
||
| 483 | * |
||
| 484 | * @return \Zend\Http\Response |
||
| 485 | * @since 0.26 |
||
| 486 | */ |
||
| 487 | public function moveAction() |
||
| 510 | } |
||
| 511 | } |
||
| 512 |