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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 28 | class ManageController extends AbstractActionController |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * attaches further Listeners for generating / processing the output |
||
| 32 | * |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | public function attachDefaultListeners() |
||
| 36 | { |
||
| 37 | parent::attachDefaultListeners(); |
||
| 38 | $serviceLocator = $this->serviceLocator; |
||
| 39 | $defaultServices = $serviceLocator->get('DefaultListeners'); |
||
| 40 | $events = $this->getEventManager(); |
||
| 41 | $events->attach($defaultServices); |
||
|
|
|||
| 42 | return $this; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * (non-PHPdoc) |
||
| 47 | * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch() |
||
| 48 | */ |
||
| 49 | public function onDispatch(\Zend\Mvc\MvcEvent $e) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * List applications |
||
| 63 | */ |
||
| 64 | public function indexAction() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Detail view of an application |
||
| 110 | * |
||
| 111 | * @return array|JsonModel|ViewModel |
||
| 112 | */ |
||
| 113 | public function detailAction() |
||
| 114 | { |
||
| 115 | if ('refresh-rating' == $this->params()->fromQuery('do')) { |
||
| 116 | return $this->refreshRatingAction(); |
||
| 117 | } |
||
| 118 | |||
| 119 | $nav = $this->serviceLocator->get('Core/Navigation'); |
||
| 120 | $page = $nav->findByRoute('lang/applications'); |
||
| 121 | $page->setActive(); |
||
| 122 | |||
| 123 | /* @var \Applications\Repository\Application$repository */ |
||
| 124 | $repository = $this->serviceLocator->get('repositories')->get('Applications/Application'); |
||
| 125 | /* @var Application $application */ |
||
| 126 | $application = $repository->find($this->params('id')); |
||
| 127 | |||
| 128 | if (!$application) { |
||
| 129 | $this->response->setStatusCode(410); |
||
| 130 | $model = new ViewModel( |
||
| 131 | array( |
||
| 132 | 'content' => /*@translate*/ 'Invalid apply id' |
||
| 133 | ) |
||
| 134 | ); |
||
| 135 | $model->setTemplate('applications/error/not-found'); |
||
| 136 | return $model; |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->acl($application, 'read'); |
||
| 140 | |||
| 141 | $applicationIsUnread = false; |
||
| 142 | if ($application->isUnreadBy($this->auth('id')) && $application->getStatus()) { |
||
| 143 | $application->addReadBy($this->auth('id')); |
||
| 144 | $applicationIsUnread = true; |
||
| 145 | $application->changeStatus( |
||
| 146 | $application->getStatus(), |
||
| 147 | sprintf(/*@translate*/ 'Application was read by %s', |
||
| 148 | $this->auth()->getUser()->getInfo()->getDisplayName())); |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | $format=$this->params()->fromQuery('format'); |
||
| 154 | |||
| 155 | if ($application->isDraft()) { |
||
| 156 | $list = false; |
||
| 157 | } else { |
||
| 158 | $list = $this->paginationParams('Applications\Index', $repository); |
||
| 159 | $list->setCurrent($application->id); |
||
| 160 | } |
||
| 161 | |||
| 162 | $return = array( |
||
| 163 | 'application'=> $application, |
||
| 164 | 'list' => $list, |
||
| 165 | 'isUnread' => $applicationIsUnread, |
||
| 166 | 'format' => 'html' |
||
| 167 | ); |
||
| 168 | switch ($format) { |
||
| 169 | case 'json': |
||
| 170 | /*@deprecated - must be refactored */ |
||
| 171 | $viewModel = new JsonModel(); |
||
| 172 | $viewModel->setVariables( |
||
| 173 | /*array( |
||
| 174 | 'application' => */$this->serviceLocator |
||
| 175 | ->get('builders') |
||
| 176 | ->get('JsonApplication') |
||
| 177 | ->unbuild($application) |
||
| 178 | ); |
||
| 179 | $viewModel->setVariable('isUnread', $applicationIsUnread); |
||
| 180 | $return = $viewModel; |
||
| 181 | break; |
||
| 182 | case 'pdf': |
||
| 183 | $pdf = $this->serviceLocator->get('Core/html2pdf'); |
||
| 184 | $return['format'] = $format; |
||
| 185 | break; |
||
| 186 | default: |
||
| 187 | $contentCollector = $this->getPluginManager()->get('Core/ContentCollector'); |
||
| 188 | $contentCollector->setTemplate('applications/manage/details/action-buttons'); |
||
| 189 | $actionButtons = $contentCollector->trigger('application.detail.actionbuttons', $application); |
||
| 190 | |||
| 191 | $return = new ViewModel($return); |
||
| 192 | $return->addChild($actionButtons, 'externActionButtons'); |
||
| 193 | break; |
||
| 194 | } |
||
| 195 | |||
| 196 | return $return; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Refreshes the rating of an application |
||
| 201 | * |
||
| 202 | * @throws \DomainException |
||
| 203 | * @return \Zend\View\Model\ViewModel |
||
| 204 | */ |
||
| 205 | public function refreshRatingAction() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Attaches a social profile to an application |
||
| 223 | * @throws \InvalidArgumentException |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function socialProfileAction() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Changes the status of an application |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | public function statusAction() |
||
| 260 | { |
||
| 261 | $applicationId = $this->params('id'); |
||
| 262 | /* @var \Applications\Repository\Application $repository */ |
||
| 263 | $repository = $this->serviceLocator->get('repositories')->get('Applications/Application'); |
||
| 264 | /* @var Application $application */ |
||
| 265 | $application = $repository->find($applicationId); |
||
| 266 | |||
| 267 | /* @var Request $request */ |
||
| 268 | $request = $this->getRequest(); |
||
| 269 | |||
| 270 | if (!$application) { |
||
| 271 | throw new \InvalidArgumentException('Could not find application.'); |
||
| 272 | } |
||
| 273 | |||
| 274 | $this->acl($application, 'change'); |
||
| 275 | |||
| 276 | $jsonFormat = 'json' == $this->params()->fromQuery('format'); |
||
| 277 | $status = $this->params('status', Status::CONFIRMED); |
||
| 278 | $settings = $this->settings(); |
||
| 279 | |||
| 280 | if (in_array($status, array(Status::INCOMING))) { |
||
| 281 | $application->changeStatus($status); |
||
| 282 | if ($request->isXmlHttpRequest()) { |
||
| 283 | $response = $this->getResponse(); |
||
| 284 | $response->setContent('ok'); |
||
| 285 | return $response; |
||
| 286 | } |
||
| 287 | if ($jsonFormat) { |
||
| 288 | return array( |
||
| 289 | 'status' => 'success', |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | return $this->redirect()->toRoute('lang/applications/detail', array(), true); |
||
| 293 | } |
||
| 294 | |||
| 295 | $events = $this->serviceLocator->get('Applications/Events'); |
||
| 296 | |||
| 297 | /* @var ApplicationEvent $event */ |
||
| 298 | $event = $events->getEvent(ApplicationEvent::EVENT_APPLICATION_STATUS_CHANGE, |
||
| 299 | $this, |
||
| 300 | [ |
||
| 301 | 'application' => $application, |
||
| 302 | 'status' => $status, |
||
| 303 | 'user' => $this->auth()->getUser(), |
||
| 304 | ] |
||
| 305 | ); |
||
| 306 | |||
| 307 | $event->setIsPostRequest($request->isPost()); |
||
| 308 | $event->setPostData($request->getPost()); |
||
| 309 | $events->trigger($event); |
||
| 310 | |||
| 311 | $params = $event->getFormData(); |
||
| 312 | |||
| 313 | |||
| 314 | if ($request->isPost()) { |
||
| 315 | |||
| 316 | if ($jsonFormat) { |
||
| 317 | return array( |
||
| 318 | 'status' => 'success', |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | $this->notification()->success($event->getNotification()); |
||
| 322 | return $this->redirect()->toRoute('lang/applications/detail', array(), true); |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($jsonFormat) { |
||
| 326 | return $params; |
||
| 327 | } |
||
| 328 | |||
| 329 | /* @var \Applications\Form\Mail $form */ |
||
| 330 | $form = $this->serviceLocator->get('FormElementManager')->get('Applications/Mail'); |
||
| 331 | $form->populateValues($params); |
||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | $reciptient = $params['to']; |
||
| 336 | |||
| 337 | return [ |
||
| 338 | 'recipient' => $reciptient, |
||
| 339 | 'form' => $form |
||
| 340 | ]; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Forwards an application via Email |
||
| 345 | * |
||
| 346 | * @throws \InvalidArgumentException |
||
| 347 | * @return \Zend\View\Model\JsonModel |
||
| 348 | */ |
||
| 349 | public function forwardAction() |
||
| 350 | { |
||
| 351 | $services = $this->serviceLocator; |
||
| 352 | $emailAddress = $this->params()->fromQuery('email'); |
||
| 353 | /* @var \Applications\Entity\Application $application */ |
||
| 354 | $application = $services->get('repositories')->get('Applications/Application') |
||
| 355 | ->find($this->params('id')); |
||
| 356 | |||
| 357 | $this->acl($application, 'forward'); |
||
| 358 | |||
| 359 | $translator = $services->get('translator'); |
||
| 360 | |||
| 361 | if (!$emailAddress) { |
||
| 362 | throw new \InvalidArgumentException('An email address must be supplied.'); |
||
| 363 | } |
||
| 364 | |||
| 365 | $params = array( |
||
| 366 | 'ok' => true, |
||
| 367 | 'text' => sprintf($translator->translate('Forwarded application to %s'), $emailAddress) |
||
| 368 | ); |
||
| 369 | |||
| 370 | try { |
||
| 371 | $userName = $this->auth('info')->displayName; |
||
| 372 | $fromAddress = $application->getJob()->getContactEmail(); |
||
| 373 | $mailOptions = array( |
||
| 374 | 'application' => $application, |
||
| 375 | 'to' => $emailAddress, |
||
| 376 | 'from' => array($fromAddress => $userName) |
||
| 377 | ); |
||
| 378 | $this->mailer('Applications/Forward', $mailOptions, true); |
||
| 379 | $this->notification()->success($params['text']); |
||
| 380 | } catch (\Exception $ex) { |
||
| 381 | $params = array( |
||
| 382 | 'ok' => false, |
||
| 383 | 'text' => sprintf($translator->translate('Forward application to %s failed.'), $emailAddress) |
||
| 384 | ); |
||
| 385 | $this->notification()->error($params['text']); |
||
| 386 | } |
||
| 387 | $application->changeStatus($application->getStatus(), $params['text']); |
||
| 388 | return new JsonModel($params); |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Deletes an application |
||
| 393 | * |
||
| 394 | * @return array|\Zend\Http\Response |
||
| 395 | */ |
||
| 396 | public function deleteAction() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Move an application to talent pool |
||
| 424 | * |
||
| 425 | * @return \Zend\Http\Response |
||
| 426 | * @since 0.26 |
||
| 427 | */ |
||
| 428 | public function moveAction() |
||
| 452 | } |
||
| 453 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: