Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ApplyController 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 ApplyController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class ApplyController extends AbstractActionController |
||
| 45 | { |
||
| 46 | |||
| 47 | protected $container; |
||
| 48 | |||
| 49 | View Code Duplication | public function attachDefaultListeners() |
|
|
|
|||
| 50 | { |
||
| 51 | parent::attachDefaultListeners(); |
||
| 52 | $events = $this->getEventManager(); |
||
| 53 | $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 10); |
||
| 54 | $serviceLocator = $this->getServiceLocator(); |
||
| 55 | $defaultServices = $serviceLocator->get('DefaultListeners'); |
||
| 56 | $events->attach($defaultServices); |
||
| 57 | return $this; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function preDispatch(MvcEvent $e) |
||
| 61 | { |
||
| 62 | /* @var $application \Applications\Entity\Application */ |
||
| 63 | if ($this->params()->fromQuery('do')) { |
||
| 64 | $e->getRouteMatch()->setParam('action', 'do'); |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | /* @var $request \Zend\Http\Request */ |
||
| 69 | /* @var $repository \Applications\Repository\Application */ |
||
| 70 | /* @var $container \Applications\Form\Apply */ |
||
| 71 | $request = $this->getRequest(); |
||
| 72 | $services = $this->getServiceLocator(); |
||
| 73 | $repositories = $services->get('repositories'); |
||
| 74 | $repository = $repositories->get('Applications/Application'); |
||
| 75 | $container = $services->get('forms')->get('Applications/Apply'); |
||
| 76 | |||
| 77 | if ($request->isPost()) { |
||
| 78 | $appId = $this->params()->fromPost('applicationId'); |
||
| 79 | if (!$appId) { |
||
| 80 | throw new \RuntimeException('Missing application id.'); |
||
| 81 | } |
||
| 82 | $routeMatch = $e->getRouteMatch(); |
||
| 83 | |||
| 84 | if ('recruiter-preview' == $appId) { |
||
| 85 | $routeMatch->setParam('action', 'process-preview'); |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | $application = $repository->find($appId); |
||
| 90 | if (!$application) { |
||
| 91 | throw new \RuntimeException('Invalid application id.'); |
||
| 92 | } |
||
| 93 | |||
| 94 | $action = 'process'; |
||
| 95 | |||
| 96 | $routeMatch->setParam('action', $action); |
||
| 97 | } else { |
||
| 98 | $user = $this->auth()->getUser(); |
||
| 99 | $appId = $this->params('applyId'); |
||
| 100 | if (!$appId) { |
||
| 101 | throw new \RuntimeException('Missing apply id'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /* @var \Jobs\Entity\Job $job */ |
||
| 105 | $job = $repositories->get('Jobs/Job')->findOneByApplyId($appId); |
||
| 106 | |||
| 107 | if (!$job) { |
||
| 108 | $e->getRouteMatch()->setParam('action', 'job-not-found'); |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | switch ($job->getStatus()) { |
||
| 113 | case \Jobs\Entity\Status::ACTIVE: |
||
| 114 | break; |
||
| 115 | default: |
||
| 116 | $e->getRouteMatch()->setParam('action', 'job-not-found'); |
||
| 117 | return; |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($user === $job->getUser()) { |
||
| 122 | $application = new \Applications\Entity\Application(); |
||
| 123 | $application->setContact(new Contact()); |
||
| 124 | $application->setJob($job); |
||
| 125 | $application->setId('recruiter-preview'); |
||
| 126 | } else { |
||
| 127 | $subscriberUri = $this->params()->fromQuery('subscriber'); |
||
| 128 | $application = $repository->findDraft($user, $appId); |
||
| 129 | |||
| 130 | if ($application) { |
||
| 131 | /* @var $form \Auth\Form\UserInfo */ |
||
| 132 | $form = $container->getForm('contact.contact'); |
||
| 133 | $form->setDisplayMode('summary'); |
||
| 134 | |||
| 135 | if ($subscriberUri) { |
||
| 136 | $subscriber = $application->getSubscriber(); |
||
| 137 | if (!$subscriber || $subscriber->uri != $subscriberUri) { |
||
| 138 | $subscriber = $repositories->get('Applications/Subscriber')->findbyUri($subscriberUri, /*create*/ true); |
||
| 139 | $application->setSubscriber($subscriber); |
||
| 140 | $subscriber->getname(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | if (!$job) { |
||
| 145 | $e->getRouteMatch()->setParam('action', 'job-not-found'); |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | if ($job->getUriApply()) { |
||
| 149 | return $this->redirect($job->getUriApply()); |
||
| 150 | } |
||
| 151 | |||
| 152 | /* @var $application \Applications\Entity\Application */ |
||
| 153 | $application = $repository->create(); |
||
| 154 | $application->setIsDraft(true) |
||
| 155 | ->setContact($user->getInfo()) |
||
| 156 | ->setUser($user) |
||
| 157 | ->setJob($job); |
||
| 158 | |||
| 159 | if ($subscriberUri) { |
||
| 160 | $subscriber = $repositories->get('Applications/Subscriber')->findbyUri($subscriberUri, /*create*/ true); |
||
| 161 | $application->setSubscriber($subscriber); |
||
| 162 | } |
||
| 163 | |||
| 164 | $repositories->store($application); |
||
| 165 | /* |
||
| 166 | * If we had copy an user image, we need to refresh its data |
||
| 167 | * to populate the length property. |
||
| 168 | */ |
||
| 169 | if ($image = $application->getContact()->getImage()) { |
||
| 170 | $repositories->refresh($image); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | $container->setEntity($application); |
||
| 177 | $this->configureContainer($container); |
||
| 178 | $this->container = $container; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function jobNotFoundAction() |
||
| 190 | |||
| 191 | public function indexAction() |
||
| 192 | { |
||
| 219 | |||
| 220 | public function oneClickApplyAction() |
||
| 297 | |||
| 298 | public function processPreviewAction() |
||
| 302 | |||
| 303 | public function processAction() |
||
| 345 | |||
| 346 | public function doAction() |
||
| 414 | |||
| 415 | protected function checkApplication($application) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Configures the apply form container. |
||
| 423 | * |
||
| 424 | * Currently only disables elements. |
||
| 425 | * |
||
| 426 | * @param Container $container |
||
| 427 | */ |
||
| 428 | protected function configureContainer(Container $container) |
||
| 451 | } |
||
| 452 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.