Conditions | 13 |
Paths | 224 |
Total Lines | 91 |
Code Lines | 54 |
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 |
||
89 | public function authenticate() |
||
90 | { |
||
91 | $hybridAuth = $this->getHybridAuth(); |
||
92 | /* @var $adapter \Hybrid_Provider_Model */ |
||
93 | $adapter = $hybridAuth->authenticate($this->_provider); |
||
94 | |||
95 | $userProfile = $adapter->getUserProfile(); |
||
96 | $email = isset($userProfile->emailVerified) && !empty($userProfile->emailVerified) |
||
97 | ? $userProfile->emailVerified |
||
98 | : $userProfile->email; |
||
99 | |||
100 | |||
101 | $forceSave = false; |
||
102 | $user = $this->getRepository()->findByProfileIdentifier($userProfile->identifier, $this->_provider, ['allowDeactivated' => true]); |
||
103 | |||
104 | if (!$user) { |
||
105 | $forceSave = true; |
||
106 | $user = $this->getRepository()->create(); |
||
107 | } |
||
108 | |||
109 | |||
110 | $currentInfo = $user->getProfile($this->_provider); |
||
111 | $socialData = []; |
||
112 | |||
113 | try { |
||
114 | $socialProfile = $this->socialProfilePlugin->fetch($this->_provider); |
||
115 | |||
116 | if (false !== $socialProfile) { |
||
117 | $socialData = $socialProfile->getData(); |
||
118 | } |
||
119 | } catch (\InvalidArgumentException $e) {} |
||
|
|||
120 | |||
121 | $newInfo = [ |
||
122 | 'auth' => (array) $userProfile, |
||
123 | 'data' => $socialData, |
||
124 | ]; |
||
125 | |||
126 | if ($forceSave || $currentInfo != $newInfo) { |
||
127 | $dm = $this->getRepository()->getDocumentManager(); |
||
128 | $userInfo = $user->getInfo(); |
||
129 | |||
130 | if ('' == $userInfo->getEmail()) { |
||
131 | $userInfo->setEmail($email); |
||
132 | } |
||
133 | |||
134 | $userInfo->setFirstName($userProfile->firstName); |
||
135 | $userInfo->setLastName($userProfile->lastName); |
||
136 | $userInfo->setBirthDay($userProfile->birthDay); |
||
137 | $userInfo->setBirthMonth($userProfile->birthMonth); |
||
138 | $userInfo->setBirthYear($userProfile->birthYear); |
||
139 | $userInfo->setPostalCode($userProfile->zip); |
||
140 | $userInfo->setCity($userProfile->city); |
||
141 | $userInfo->setStreet($userProfile->address); |
||
142 | $userInfo->setPhone($userProfile->phone); |
||
143 | $userInfo->setGender($userProfile->gender); |
||
144 | |||
145 | // $user->setLogin($email); // this may cause duplicate key exception |
||
146 | $user->addProfile($this->_provider, $newInfo); |
||
147 | |||
148 | $dm->persist($user); |
||
149 | // make sure all ids are generated and user exists in database. |
||
150 | $dm->flush(); |
||
151 | |||
152 | /* |
||
153 | * This must be after flush because a newly created user has no id! |
||
154 | */ |
||
155 | if ($forceSave || (!$userInfo->getImage() && $userProfile->photoURL)) { |
||
156 | // get user image |
||
157 | if ('' != $userProfile->photoURL) { |
||
158 | $client = new \Zend\Http\Client($userProfile->photoURL, array('sslverifypeer' => false)); |
||
159 | $response = $client->send(); |
||
160 | $file = new GridFSFile(); |
||
161 | $file->setBytes($response->getBody()); |
||
162 | |||
163 | $userImage = new UserImage(); |
||
164 | $userImage->setName($userProfile->lastName.$userProfile->firstName); |
||
165 | $userImage->setType($response->getHeaders()->get('Content-Type')->getFieldValue()); |
||
166 | $userImage->setUser($user); |
||
167 | $userImage->setFile($file); |
||
168 | $userInfo->setImage($userImage); |
||
169 | $dm->persist($userImage); |
||
170 | //$this->getRepository()->store($user->info); |
||
171 | } |
||
172 | |||
173 | // We have to flush again |
||
174 | $dm->flush(); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | |||
179 | return new Result(Result::SUCCESS, $user->getId(), array('firstLogin' => $forceSave, 'user' => $user)); |
||
180 | } |
||
239 |