Conditions | 16 |
Paths | 12 |
Total Lines | 113 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
93 | public function profileAction() |
||
94 | { |
||
95 | /* @var \Auth\Form\UserProfileContainer $userProfileContainer */ |
||
96 | $userProfileContainer = $this->userProfileContainer; |
||
97 | $user = $this->authService->getUser(); /* @var $user \Auth\Entity\User */ |
||
98 | $postProfiles = (array)$this->params()->fromPost('social_profiles'); |
||
99 | $userProfiles = $user->getProfile(); |
||
100 | $formSocialProfiles = $this->socialProfileForm |
||
101 | ->setUseDefaultValidation(true) |
||
102 | ->setData(['social_profiles' => array_map(function ($array) |
||
103 | { |
||
104 | return $array['data']; |
||
105 | }, $userProfiles)]); |
||
106 | |||
107 | $translator = $this->translator; |
||
108 | /* @var \Auth\Form\SocialProfiles $formSocialProfiles */ |
||
109 | $formSocialProfiles->getBaseFieldset() |
||
110 | ->setOption( |
||
111 | 'description', |
||
112 | $translator->translate('You can connect your user profile with social networks. This allows you to log in via these networks.') |
||
113 | ); |
||
114 | $userProfileContainer->setEntity($user); |
||
115 | |||
116 | if ($this->request->isPost()) { |
||
|
|||
117 | $formName = $this->params()->fromQuery('form'); |
||
118 | $form = $userProfileContainer->getForm($formName); |
||
119 | |||
120 | if ($form) { |
||
121 | $postData = $form->getOption('use_post_array') ? $_POST : array(); |
||
122 | //@TODO: [ZF3] option use_files_array is false by default |
||
123 | //$filesData = $form->getOption('use_files_array') ? $_FILES : array(); |
||
124 | $filesData = $_FILES; |
||
125 | $data = array_merge($postData, $filesData); |
||
126 | $form->setData($data); |
||
127 | |||
128 | if (!$form->isValid()) { |
||
129 | return new JsonModel( |
||
130 | array( |
||
131 | 'valid' => false, |
||
132 | 'errors' => $form->getMessages(), |
||
133 | ) |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | $this->repositories->store($user); |
||
138 | |||
139 | if ('file-uri' === $this->params()->fromPost('return')) { |
||
140 | $content = $form->getHydrator()->getLastUploadedFile()->getUri(); |
||
141 | } else { |
||
142 | if ($form instanceof SummaryFormInterface) { |
||
143 | $form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY); |
||
144 | $viewHelper = 'summaryForm'; |
||
145 | } else { |
||
146 | $viewHelper = 'form'; |
||
147 | } |
||
148 | $content = $this->viewHelper->get($viewHelper)->__invoke($form); |
||
149 | } |
||
150 | |||
151 | return new JsonModel( |
||
152 | array( |
||
153 | 'valid' => $form->isValid(), |
||
154 | 'content' => $content, |
||
155 | ) |
||
156 | ); |
||
157 | } |
||
158 | elseif ($postProfiles) { |
||
159 | $formSocialProfiles->setData($this->params()->fromPost()); |
||
160 | |||
161 | if ($formSocialProfiles->isValid()) { |
||
162 | $dataProfiles = $formSocialProfiles->getData()['social_profiles']; |
||
163 | $userRepository = $this->repositories->get('Auth/User'); /* @var $userRepository \Auth\Repository\User */ |
||
164 | $hybridAuth = $this->hybridAuthAdapter->getHybridAuth(); |
||
165 | |||
166 | foreach ($dataProfiles as $network => $postProfile) { |
||
167 | // remove |
||
168 | if (isset($userProfiles[$network]) && !$dataProfiles[$network]) { |
||
169 | $user->removeProfile($network); |
||
170 | } |
||
171 | |||
172 | // add |
||
173 | if (!isset($userProfiles[$network]) && $dataProfiles[$network]) { |
||
174 | $authProfile = $hybridAuth->authenticate($network) |
||
175 | ->getUserProfile(); |
||
176 | // check for existing profiles |
||
177 | if ($userRepository->isProfileAssignedToAnotherUser($user->getId(), $authProfile->identifier, $network)) { |
||
178 | $dataProfiles[$network] = null; |
||
179 | $formSocialProfiles->setMessages(array( |
||
180 | 'social_profiles' => [ |
||
181 | $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)] |
||
182 | ] |
||
183 | )); |
||
184 | } else { |
||
185 | $profile = [ |
||
186 | 'auth' => (array)$authProfile, |
||
187 | 'data' => \Zend\Json\Json::decode($dataProfiles[$network]) |
||
188 | ]; |
||
189 | $user->addProfile($network, $profile); |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | // keep data in sync & properly decoded |
||
196 | $formSocialProfiles->setData(['social_profiles' => array_map(function ($array) |
||
197 | { |
||
198 | return \Zend\Json\Json::decode($array) ?: ''; |
||
199 | }, $dataProfiles)]); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | return array( |
||
204 | 'form' => $userProfileContainer, |
||
205 | 'socialProfilesForm' => $formSocialProfiles |
||
206 | ); |
||
209 |