| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 218 |
| Code Lines | 129 |
| Lines | 50 |
| Ratio | 22.94 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 52 | public function indexAction() |
||
| 53 | { |
||
| 54 | $services = $this->getServiceLocator(); |
||
| 55 | /** @var Request $request */ |
||
| 56 | $request = $this->getRequest(); |
||
| 57 | |||
| 58 | $jobId = $this->params()->fromPost('jobId', 0); |
||
| 59 | $applyId = (int) $this->params()->fromPost('applyId', 0); |
||
| 60 | |||
| 61 | // subscriber comes from the form |
||
| 62 | $subscriberUri = $this->params()->fromPost('subscriberUri', ''); |
||
| 63 | if (empty($subscriberUri)) { |
||
| 64 | // subscriber comes with the request of the form |
||
| 65 | // which implies that the backlink in the job-offer had such an link in the query |
||
| 66 | $subscriberUri = $this->params()->fromQuery('subscriberUri', ''); |
||
| 67 | } |
||
| 68 | if (empty($subscriberUri)) { |
||
| 69 | // the subscriber comes from an external module, maybe after interpreting the backlink, or the referer |
||
| 70 | $e = $this->getEvent(); |
||
| 71 | $subscriberResponseCollection = $this->getEventManager()->trigger('subscriber.getUri', $e); |
||
| 72 | if (!$subscriberResponseCollection->isEmpty()) { |
||
| 73 | $subscriberUri = $subscriberResponseCollection->last(); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | $job = ($request->isPost() && !empty($jobId)) |
||
| 79 | ? $services->get('repositories')->get('Jobs/Job')->find($jobId) |
||
| 80 | : $services->get('repositories')->get('Jobs/Job')->findOneBy(array("applyId"=>(0 == $applyId)?$this->params('jobId'):$applyId)); |
||
| 81 | |||
| 82 | |||
| 83 | View Code Duplication | if (!$job) { |
|
| 84 | $this->response->setStatusCode(410); |
||
| 85 | $model = new ViewModel( |
||
| 86 | array( |
||
| 87 | 'content' => /*@translate*/ 'Invalid apply id' |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | $model->setTemplate('auth/index/job-not-found.phtml'); |
||
| 91 | return $model; |
||
| 92 | } |
||
| 93 | |||
| 94 | /* @var $form \Zend\Form\Form */ |
||
| 95 | $form = $services->get('FormElementManager')->get('Application/Create'); |
||
| 96 | $form->setValidate(); |
||
| 97 | |||
| 98 | $viewModel = new ViewModel(); |
||
| 99 | $viewModel->setVariables( |
||
| 100 | array( |
||
| 101 | 'job' => $job, |
||
| 102 | 'form' => $form, |
||
| 103 | 'isApplicationSaved' => false, |
||
| 104 | 'subscriberUri' => $subscriberUri, |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | $applicationEntity = new Application(); |
||
| 109 | $applicationEntity->setJob($job); |
||
| 110 | |||
| 111 | if ($this->auth()->isLoggedIn()) { |
||
| 112 | // copy the contact info into the application |
||
| 113 | $contact = new Info(); |
||
| 114 | $contact->fromArray(Info::toArray($this->auth()->get('info'))); |
||
| 115 | $applicationEntity->setContact($contact); |
||
| 116 | } |
||
| 117 | |||
| 118 | $form->bind($applicationEntity); |
||
| 119 | $form->get('jobId')->setValue($job->id); |
||
| 120 | $form->get('subscriberUri')->setValue($subscriberUri); |
||
| 121 | |||
| 122 | if ($request->isPost()) { |
||
| 123 | if ($returnTo = $this->params()->fromPost('returnTo', false)) { |
||
| 124 | $returnTo = \Zend\Uri\UriFactory::factory($returnTo); |
||
| 125 | } |
||
| 126 | $services = $this->getServiceLocator(); |
||
| 127 | |||
| 128 | $data = array_merge_recursive( |
||
| 129 | $this->request->getPost()->toArray(), |
||
| 130 | $this->request->getFiles()->toArray() |
||
| 131 | ); |
||
| 132 | |||
| 133 | if (!empty($subscriberUri) && $request->isPost()) { |
||
| 134 | $subscriber = $services->get('repositories')->get('Applications/Subscriber')->findbyUriOrCreate($subscriberUri); |
||
| 135 | $applicationEntity->subscriber = $subscriber; |
||
| 136 | } |
||
| 137 | |||
| 138 | $form->setData($data); |
||
| 139 | |||
| 140 | if (!$form->isValid()) { |
||
| 141 | if ($request->isXmlHttpRequest()) { |
||
| 142 | return new JsonModel( |
||
| 143 | array( |
||
| 144 | 'ok' => false, |
||
| 145 | 'messages' => $form->getMessages() |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | View Code Duplication | if ($returnTo) { |
|
| 150 | $returnTo->setQuery($returnTo->getQueryAsArray() + array('status' => 'failure')); |
||
| 151 | return $this->redirect()->toUrl((string) $returnTo); |
||
| 152 | } |
||
| 153 | $this->notification()->error(/*@translate*/ 'There were errors in the form.'); |
||
| 154 | |||
| 155 | } else { |
||
| 156 | $auth = $this->auth(); |
||
| 157 | |||
| 158 | if ($auth->isLoggedIn()) { |
||
| 159 | // in instance user is logged in, |
||
| 160 | // and no image is uploaded |
||
| 161 | // take his profile-image as copy |
||
| 162 | $applicationEntity->setUser($auth->getUser()); |
||
| 163 | $imageData = $form->get('contact')->get('image')->getValue(); |
||
| 164 | if (UPLOAD_ERR_NO_FILE == $imageData['error']) { |
||
| 165 | // has the user an image |
||
| 166 | $image = $auth->getUser()->info->image; |
||
| 167 | if ($image) { |
||
| 168 | $repositoryAttachment = $services->get('repositories')->get('Applications/Attachment'); |
||
| 169 | |||
| 170 | // this should provide a real copy, not just a reference |
||
| 171 | $contactImage = $repositoryAttachment->copy($image); |
||
| 172 | $applicationEntity->contact->setImage($contactImage); |
||
| 173 | } else { |
||
| 174 | $applicationEntity->contact->setImage(null); //explicitly remove image. |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!$request->isXmlHttpRequest()) { |
||
| 180 | $applicationEntity->setStatus(new Status()); |
||
| 181 | $permissions = $applicationEntity->getPermissions(); |
||
| 182 | $permissions->inherit($job->getPermissions()); |
||
| 183 | |||
| 184 | /* |
||
| 185 | * New Application alert Mails to job recruiter |
||
| 186 | * This is temporary until Companies are implemented. |
||
| 187 | */ |
||
| 188 | $recruiter = $services->get('repositories')->get('Auth/User')->findOneByEmail($job->contactEmail); |
||
| 189 | View Code Duplication | if (!$recruiter) { |
|
| 190 | $recruiter = $job->user; |
||
| 191 | $admin = false; |
||
| 192 | } else { |
||
| 193 | $admin = $job->user; |
||
| 194 | } |
||
| 195 | |||
| 196 | $services->get('repositories')->store($applicationEntity); |
||
| 197 | /* |
||
| 198 | * New Application alert Mails to job recruiter |
||
| 199 | * This is temporary until Companies are implemented. |
||
| 200 | */ |
||
| 201 | $recruiter = $services->get('repositories')->get('Auth/User')->findOneByEmail($job->contactEmail); |
||
| 202 | View Code Duplication | if (!$recruiter) { |
|
| 203 | $recruiter = $job->user; |
||
| 204 | $admin = false; |
||
| 205 | } else { |
||
| 206 | $admin = $job->user; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($recruiter->getSettings('Applications')->getMailAccess()) { |
||
| 210 | $this->mailer('Applications/NewApplication', array('job' => $job, 'user' => $recruiter, 'admin' => $admin), /*send*/ true); |
||
| 211 | } |
||
| 212 | View Code Duplication | if ($recruiter->getSettings('Applications')->getAutoConfirmMail()) { |
|
| 213 | $ackBody = $recruiter->getSettings('Applications')->getMailConfirmationText(); |
||
| 214 | if (empty($ackBody)) { |
||
| 215 | $ackBody = $job->user->getSettings('Applications')->getMailConfirmationText(); |
||
| 216 | } |
||
| 217 | if (!empty($ackBody)) { |
||
| 218 | /* Acknowledge mail to the applicant */ |
||
| 219 | $ackMail = $this->mailer( |
||
| 220 | 'Applications/Confirmation', |
||
| 221 | array('application' => $applicationEntity, |
||
| 222 | 'body' => $ackBody, |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | // Must be called after initializers in creation |
||
| 226 | $ackMail->setSubject(/*@translate*/ 'Application confirmation'); |
||
| 227 | $ackMail->setFrom($recruiter->getInfo()->getEmail()); |
||
| 228 | $this->mailer($ackMail); |
||
| 229 | $applicationEntity->changeStatus(StatusInterface::CONFIRMED, sprintf('Mail was sent to %s', $applicationEntity->contact->email)); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | // send carbon copy of the application |
||
| 234 | |||
| 235 | $paramsCC = $this->getRequest()->getPost('carboncopy', 0); |
||
| 236 | if (isset($paramsCC) && array_key_exists('carboncopy', $paramsCC)) { |
||
| 237 | $wantCarbonCopy = (int) $paramsCC['carboncopy']; |
||
| 238 | if ($wantCarbonCopy) { |
||
| 239 | $mail = $this->mailer( |
||
| 240 | 'Applications/CarbonCopy', |
||
| 241 | array( |
||
| 242 | 'application' => $applicationEntity, |
||
| 243 | 'to' => $applicationEntity->contact->email, |
||
| 244 | ), /*send*/ |
||
| 245 | true |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($request->isXmlHttpRequest()) { |
||
| 252 | return new JsonModel( |
||
| 253 | array( |
||
| 254 | 'ok' => true, |
||
| 255 | 'id' => $applicationEntity->id, |
||
| 256 | 'jobId' => $applicationEntity->job->id, |
||
| 257 | ) |
||
| 258 | ); |
||
| 259 | } |
||
| 260 | View Code Duplication | if ($returnTo) { |
|
| 261 | $returnTo->setQuery($returnTo->getQueryAsArray() + array('status' => 'success')); |
||
| 262 | return $this->redirect()->toUrl((string) $returnTo); |
||
| 263 | } |
||
| 264 | $this->notification()->success(/*@translate*/ 'your application was sent successfully'); |
||
| 265 | $viewModel->setVariable('isApplicationSaved', true); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | return $viewModel; |
||
| 269 | } |
||
| 270 | |||
| 353 |
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: