| Conditions | 30 |
| Paths | 2283 |
| Total Lines | 184 |
| Code Lines | 127 |
| Lines | 21 |
| Ratio | 11.41 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 155 | protected function save($parameter = array()) |
||
| 156 | { |
||
| 157 | $serviceLocator = $this->getServiceLocator(); |
||
| 158 | $user = $this->auth->getUser(); |
||
| 159 | if (empty($user->info->email)) { |
||
| 160 | return $this->getErrorViewModel('no-parent', array('cause' => 'noEmail')); |
||
| 161 | } |
||
| 162 | $userOrg = $user->getOrganization(); |
||
| 163 | if (!$userOrg->hasAssociation()) { |
||
| 164 | return $this->getErrorViewModel('no-parent', array('cause' => 'noCompany')); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** @var \Zend\Http\Request $request */ |
||
| 168 | $request = $this->getRequest(); |
||
| 169 | $isAjax = $request->isXmlHttpRequest(); |
||
| 170 | $pageToForm = array(0 => array('locationForm', 'nameForm', 'portalForm'), |
||
| 171 | 1 => array('descriptionForm'), |
||
| 172 | 2 => array('previewForm')); |
||
| 173 | |||
| 174 | $params = $this->params(); |
||
| 175 | $formIdentifier = $params->fromQuery('form'); |
||
| 176 | $pageIdentifier = (int) $params->fromQuery('page', array_key_exists('page', $parameter)?$parameter['page']:0); |
||
| 177 | $jobEntity = $this->getJob(); |
||
| 178 | $viewModel = null; |
||
| 179 | $this->acl($jobEntity, 'edit'); |
||
| 180 | $form = $this->getFormular($jobEntity); |
||
| 181 | |||
| 182 | $valid = true; |
||
| 183 | $instanceForm = null; |
||
| 184 | $formErrorMessages = array(); |
||
| 185 | |||
| 186 | if (isset($formIdentifier) && $request->isPost()) { |
||
| 187 | // at this point the form get instantiated and immediately accumulated |
||
| 188 | $instanceForm = $form->getForm($formIdentifier); |
||
| 189 | if (!isset($instanceForm)) { |
||
| 190 | throw new \RuntimeException('No form found for "' . $formIdentifier . '"'); |
||
| 191 | } |
||
| 192 | // the id may be part of the postData, but it never should be altered |
||
| 193 | $postData = $request->getPost(); |
||
| 194 | if (isset($postData['id'])) { |
||
| 195 | unset($postData['id']); |
||
| 196 | } |
||
| 197 | unset($postData['applyId']); |
||
| 198 | $instanceForm->setData($postData); |
||
| 199 | $valid = $instanceForm->isValid(); |
||
| 200 | $formErrorMessages = ArrayUtils::merge($formErrorMessages, $instanceForm->getMessages()); |
||
| 201 | if ($valid) { |
||
| 202 | /* |
||
| 203 | * @todo This is a workaround for GeoJSON data insertion |
||
| 204 | * until we figured out, what we really want it to be. |
||
| 205 | */ |
||
| 206 | if ('locationForm' == $formIdentifier) { |
||
| 207 | $locElem = $instanceForm->getBaseFieldset()->get('location'); |
||
| 208 | if ($locElem instanceOf \Geo\Form\GeoText) { |
||
| 209 | $loc = $locElem->getValue('entity'); |
||
| 210 | $locations = $jobEntity->getLocations(); |
||
| 211 | if (count($locations)) { $locations->clear(); } |
||
| 212 | $locations->add($loc); |
||
| 213 | $jobEntity->setLocation($locElem->getValue()); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | $title = $jobEntity->getTitle(); |
||
| 218 | $templateTitle = $jobEntity->getTemplateValues()->getTitle(); |
||
| 219 | |||
| 220 | if (empty($templateTitle)) { |
||
| 221 | $jobEntity->getTemplateValues()->setTitle($title); |
||
| 222 | } |
||
| 223 | $this->repositoryService->persist($jobEntity); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | // validation |
||
| 228 | $jobValid = true; |
||
| 229 | $errorMessage = array(); |
||
| 230 | if (empty($jobEntity->getTitle())) { |
||
| 231 | $jobValid = false; |
||
| 232 | $errorMessage[] = $this->translator->translate('No Title'); |
||
| 233 | } |
||
| 234 | if (empty($jobEntity->getLocation())) { |
||
| 235 | $jobValid = false; |
||
| 236 | $errorMessage[] = $this->translator->translate('No Location'); |
||
| 237 | } |
||
| 238 | if (empty($jobEntity->getTermsAccepted())) { |
||
| 239 | $jobValid = false; |
||
| 240 | $errorMessage[] = $this->translator->translate('Accept the Terms'); |
||
| 241 | } |
||
| 242 | |||
| 243 | $errorMessage = '<br />' . implode('<br />', $errorMessage); |
||
| 244 | if ($isAjax) { |
||
| 245 | if ($instanceForm instanceof SummaryForm) { |
||
| 246 | $instanceForm->setRenderMode(SummaryForm::RENDER_SUMMARY); |
||
| 247 | $viewHelper = 'summaryform'; |
||
| 248 | } else { |
||
| 249 | $viewHelper = 'form'; |
||
| 250 | } |
||
| 251 | $viewHelperManager = $serviceLocator->get('ViewHelperManager'); |
||
| 252 | $content = $viewHelperManager->get($viewHelper)->__invoke($instanceForm); |
||
| 253 | $viewModel = new JsonModel( |
||
| 254 | array( |
||
| 255 | 'content' => $content, |
||
| 256 | 'valid' => $valid, |
||
| 257 | 'jobvalid' => $jobValid, |
||
| 258 | 'errors' => $formErrorMessages, |
||
| 259 | 'errorMessage' => $errorMessage) |
||
| 260 | ); |
||
| 261 | } else { |
||
| 262 | if (isset($pageIdentifier)) { |
||
| 263 | $form->disableForm(); |
||
| 264 | if (array_key_exists($pageIdentifier, $pageToForm)) { |
||
| 265 | foreach ($pageToForm[$pageIdentifier] as $actualFormIdentifier) { |
||
| 266 | $form->enableForm($actualFormIdentifier); |
||
| 267 | if ($jobEntity->isDraft()) { |
||
| 268 | $actualForm = $form->get($actualFormIdentifier); |
||
| 269 | if ('nameForm' != $actualFormIdentifier && $actualForm instanceof SummaryFormInterface) { |
||
| 270 | $form->get($actualFormIdentifier)->setDisplayMode(SummaryFormInterface::DISPLAY_FORM); |
||
| 271 | } |
||
| 272 | if ('locationForm' == $actualFormIdentifier) { |
||
| 273 | $locElem = $actualForm->getBaseFieldset()->get('location'); |
||
| 274 | if ($locElem instanceOf \Geo\Form\GeoText) { |
||
| 275 | $loc = $jobEntity->getLocations(); |
||
| 276 | if (count($loc)) { |
||
| 277 | $locElem->setValue($loc->first()); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | if (!$jobEntity->isDraft()) { |
||
| 284 | // Job is deployed, some changes are now disabled |
||
| 285 | $form->enableAll(); |
||
| 286 | } |
||
| 287 | } else { |
||
| 288 | throw new \RuntimeException('No form found for page ' . $pageIdentifier); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | $pageLinkNext = null; |
||
| 292 | $pageLinkPrevious = null; |
||
| 293 | View Code Duplication | if (0 < $pageIdentifier) { |
|
| 294 | $pageLinkPrevious = $this->url()->fromRoute( |
||
| 295 | 'lang/jobs/manage', |
||
| 296 | [], |
||
| 297 | ['query' => [ |
||
| 298 | 'id' => $jobEntity->getId(), |
||
| 299 | 'page' => $pageIdentifier - 1] |
||
| 300 | ] |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | View Code Duplication | if ($pageIdentifier < count($pageToForm) - 1) { |
|
| 304 | $pageLinkNext = $this->url()->fromRoute( |
||
| 305 | 'lang/jobs/manage', |
||
| 306 | [], |
||
| 307 | [ |
||
| 308 | 'query' => [ |
||
| 309 | 'id' => $jobEntity->getId(), |
||
| 310 | 'page' => $pageIdentifier + 1] |
||
| 311 | ] |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | $completionLink = $this->url()->fromRoute( |
||
| 315 | 'lang/jobs/completion', |
||
| 316 | [ 'id' => $jobEntity->getId()] |
||
| 317 | ); |
||
| 318 | |||
| 319 | $viewModel = $this->getViewModel($form); |
||
| 320 | |||
| 321 | $viewModel->setVariables( |
||
| 322 | array( |
||
| 323 | 'pageLinkPrevious' => $pageLinkPrevious, |
||
| 324 | 'pageLinkNext' => $pageLinkNext, |
||
| 325 | 'completionLink' => $completionLink, |
||
| 326 | 'page' => $pageIdentifier, |
||
| 327 | 'title' => $jobEntity->title, |
||
| 328 | 'job' => $jobEntity, |
||
| 329 | 'summary' => 'this is what we charge you for your offer...', |
||
| 330 | 'valid' => $valid, |
||
| 331 | 'jobvalid' => $jobValid, |
||
| 332 | 'errorMessage' => $errorMessage, |
||
| 333 | 'isDraft' => $jobEntity->isDraft() |
||
| 334 | ) |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | return $viewModel; |
||
| 338 | } |
||
| 339 | |||
| 611 | } |
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.