| Conditions | 17 |
| Paths | 20 |
| Total Lines | 111 |
| Code Lines | 72 |
| Lines | 17 |
| Ratio | 15.32 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 40 | public function profileAction() |
||
| 41 | { |
||
| 42 | $serviceLocator = $this->getServiceLocator(); |
||
| 43 | $forms = $serviceLocator->get('forms'); |
||
| 44 | $container = $forms->get('Auth/userprofilecontainer'); |
||
| 45 | $user = $serviceLocator->get('AuthenticationService')->getUser(); /* @var $user \Auth\Entity\User */ |
||
| 46 | $postProfiles = (array)$this->params()->fromPost('social_profiles'); |
||
| 47 | $userProfiles = $user->getProfile(); |
||
| 48 | $formSocialProfiles = $forms->get('Auth/SocialProfiles') |
||
| 49 | ->setUseDefaultValidation(true) |
||
| 50 | ->setData(['social_profiles' => array_map(function ($array) |
||
| 51 | { |
||
| 52 | return $array['data']; |
||
| 53 | }, $userProfiles)]); |
||
| 54 | |||
| 55 | $translator = $serviceLocator->get('Translator'); |
||
| 56 | $formSocialProfiles->getBaseFieldset() |
||
| 57 | ->setOption('description', $translator->translate("you can add your social profile to your application. You can preview and remove the attached profile before submitting the application.")); |
||
| 58 | $container->setEntity($user); |
||
| 59 | |||
| 60 | if ($this->request->isPost()) { |
||
| 61 | $formName = $this->params()->fromQuery('form'); |
||
| 62 | $form = $container->getForm($formName); |
||
| 63 | |||
| 64 | if ($form) { |
||
| 65 | $postData = $form->getOption('use_post_array') ? $_POST : array(); |
||
| 66 | $filesData = $form->getOption('use_files_array') ? $_FILES : array(); |
||
| 67 | $data = array_merge($postData, $filesData); |
||
| 68 | $form->setData($data); |
||
| 69 | |||
| 70 | View Code Duplication | if (!$form->isValid()) { |
|
| 71 | return new JsonModel( |
||
| 72 | array( |
||
| 73 | 'valid' => false, |
||
| 74 | 'errors' => $form->getMessages(), |
||
| 75 | ) |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | $serviceLocator->get('repositories')->store($user); |
||
| 80 | |||
| 81 | if ('file-uri' === $this->params()->fromPost('return')) { |
||
| 82 | $content = $form->getHydrator()->getLastUploadedFile()->getUri(); |
||
| 83 | View Code Duplication | } else { |
|
| 84 | if ($form instanceof SummaryFormInterface) { |
||
| 85 | $form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY); |
||
| 86 | $viewHelper = 'summaryform'; |
||
| 87 | } else { |
||
| 88 | $viewHelper = 'form'; |
||
| 89 | } |
||
| 90 | $content = $serviceLocator->get('ViewHelperManager')->get($viewHelper)->__invoke($form); |
||
| 91 | } |
||
| 92 | |||
| 93 | return new JsonModel( |
||
| 94 | array( |
||
| 95 | 'valid' => $form->isValid(), |
||
| 96 | 'content' => $content, |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | elseif ($postProfiles) { |
||
| 101 | $formSocialProfiles->setData($this->params()->fromPost()); |
||
| 102 | |||
| 103 | if ($formSocialProfiles->isValid()) { |
||
| 104 | $dataProfiles = $formSocialProfiles->getData()['social_profiles']; |
||
| 105 | $userRepository = $serviceLocator->get('repositories')->get('Auth/User'); /* @var $userRepository \Auth\Repository\User */ |
||
| 106 | $hybridAuth = $serviceLocator->get('HybridAuthAdapter') |
||
| 107 | ->getHybridAuth(); |
||
| 108 | |||
| 109 | foreach ($dataProfiles as $network => $postProfile) { |
||
| 110 | // remove |
||
| 111 | if (isset($userProfiles[$network]) && !$dataProfiles[$network]) { |
||
| 112 | $user->removeProfile($network); |
||
| 113 | } |
||
| 114 | |||
| 115 | // add |
||
| 116 | if (!isset($userProfiles[$network]) && $dataProfiles[$network]) { |
||
| 117 | $authProfile = $hybridAuth->authenticate($network) |
||
| 118 | ->getUserProfile(); |
||
| 119 | // check for existing profiles |
||
| 120 | if ($userRepository->isProfileAssignedToAnotherUser($user->getId(), $authProfile->identifier, $network)) { |
||
| 121 | $dataProfiles[$network] = null; |
||
| 122 | $formSocialProfiles->setMessages(array( |
||
| 123 | 'social_profiles' => [ |
||
| 124 | $network => [sprintf($translator->translate('Could not connect your %s profile with your user account. The profile is already connected to another user account.'), $authProfile->displayName)] |
||
| 125 | ] |
||
| 126 | )); |
||
| 127 | } else { |
||
| 128 | $profile = [ |
||
| 129 | 'auth' => (array)$authProfile, |
||
| 130 | 'data' => \Zend\Json\Json::decode($dataProfiles[$network]) |
||
| 131 | ]; |
||
| 132 | $user->addProfile($network, $profile); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // keep data in sync & properly decoded |
||
| 139 | $formSocialProfiles->setData(['social_profiles' => array_map(function ($array) |
||
| 140 | { |
||
| 141 | return \Zend\Json\Json::decode($array) ?: ''; |
||
| 142 | }, $dataProfiles)]); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return array( |
||
| 147 | 'form' => $container, |
||
| 148 | 'socialProfilesForm' => $formSocialProfiles |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | } |
||
| 152 |
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.