| Conditions | 12 |
| Paths | 112 |
| Total Lines | 89 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 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 | |||
| 112 | try { |
||
| 113 | $socialData = $this->socialProfilePlugin->fetch($this->_provider)->getData(); |
||
| 114 | } catch (\InvalidArgumentException $e) { |
||
| 115 | // social profile adapter does not exist |
||
| 116 | $socialData = []; |
||
| 117 | } |
||
| 118 | |||
| 119 | $newInfo = [ |
||
| 120 | 'auth' => (array) $userProfile, |
||
| 121 | 'data' => $socialData, |
||
| 122 | ]; |
||
| 123 | |||
| 124 | if ($forceSave || $currentInfo != $newInfo) { |
||
| 125 | /* */ |
||
| 126 | |||
| 127 | $dm = $this->getRepository()->getDocumentManager(); |
||
| 128 | if ('' == $user->getInfo()->email) { |
||
| 129 | $user->getInfo()->email = $email; |
||
| 130 | } |
||
| 131 | $user->getInfo()->firstName = $userProfile->firstName; |
||
| 132 | $user->getInfo()->lastName = $userProfile->lastName; |
||
| 133 | $user->getInfo()->birthDay = $userProfile->birthDay; |
||
| 134 | $user->getInfo()->birthMonth = $userProfile->birthMonth; |
||
| 135 | $user->getInfo()->birthYear = $userProfile->birthYear; |
||
| 136 | $user->getInfo()->postalcode = $userProfile->zip; |
||
| 137 | $user->getInfo()->city = $userProfile->city; |
||
| 138 | $user->getInfo()->street = $userProfile->address; |
||
| 139 | $user->getInfo()->phone = $userProfile->phone; |
||
| 140 | $user->getInfo()->gender = $userProfile->gender; |
||
| 141 | |||
| 142 | // $user->setLogin($email); // this may cause duplicate key exception |
||
| 143 | $user->addProfile($this->_provider, $newInfo); |
||
| 144 | |||
| 145 | $dm->persist($user); |
||
| 146 | // make sure all ids are generated and user exists in database. |
||
| 147 | $dm->flush(); |
||
| 148 | |||
| 149 | /* |
||
| 150 | * This must be after flush because a newly created user has no id! |
||
| 151 | */ |
||
| 152 | if ($forceSave || (!$user->getInfo()->image && $userProfile->photoURL)) { |
||
| 153 | // get user image |
||
| 154 | if ('' != $userProfile->photoURL) { |
||
| 155 | $client = new \Zend\Http\Client($userProfile->photoURL, array('sslverifypeer' => false)); |
||
| 156 | $response = $client->send(); |
||
| 157 | $file = new GridFSFile(); |
||
| 158 | $file->setBytes($response->getBody()); |
||
| 159 | |||
| 160 | $userImage = new UserImage(); |
||
| 161 | $userImage->setName($userProfile->lastName.$userProfile->firstName); |
||
| 162 | $userImage->setType($response->getHeaders()->get('Content-Type')->getFieldValue()); |
||
| 163 | $userImage->setUser($user); |
||
| 164 | $userImage->setFile($file); |
||
| 165 | $user->getInfo()->setImage($userImage); |
||
| 166 | $dm->persist($userImage); |
||
| 167 | //$this->getRepository()->store($user->info); |
||
| 168 | } |
||
| 169 | |||
| 170 | // We have to flush again |
||
| 171 | $dm->flush(); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | return new Result(Result::SUCCESS, $user->getId(), array('firstLogin' => $forceSave, 'user' => $user)); |
||
| 177 | } |
||
| 178 | |||
| 236 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.