Conditions | 14 |
Paths | 32 |
Total Lines | 81 |
Code Lines | 43 |
Lines | 4 |
Ratio | 4.94 % |
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 |
||
88 | public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null) |
||
89 | { |
||
90 | $result = $result ?: ValidationResult::create(); |
||
91 | /** @var LDAPService $service */ |
||
92 | $service = Injector::inst()->get(LDAPService::class); |
||
93 | $login = trim($data['Login']); |
||
94 | if (Email::is_valid_address($login)) { |
||
95 | if (Config::inst()->get(self::class, 'allow_email_login') != 'yes') { |
||
96 | $result->addError( |
||
97 | _t( |
||
98 | 'LDAPAuthenticator.PLEASEUSEUSERNAME', |
||
99 | 'Please enter your username instead of your email to log in.' |
||
100 | ) |
||
101 | ); |
||
102 | return null; |
||
103 | } |
||
104 | $username = $service->getUsernameByEmail($login); |
||
105 | |||
106 | // No user found with this email. |
||
107 | if (!$username) { |
||
108 | if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { |
||
109 | if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { |
||
110 | { |
||
111 | return $fallbackMember; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $result->addError(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials')); |
||
117 | return null; |
||
118 | } |
||
119 | } else { |
||
120 | $username = $login; |
||
121 | } |
||
122 | |||
123 | $serviceAuthenticationResult = $service->authenticate($username, $data['Password']); |
||
124 | $success = $serviceAuthenticationResult['success'] === true; |
||
125 | |||
126 | if (!$success) { |
||
127 | /* |
||
128 | * Try the fallback method unless the reason was invalid credentials. This is to avoid |
||
129 | * having an unhandled exception error thrown by PasswordEncryptor::create_for_algorithm() |
||
130 | */ |
||
131 | if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes' |
||
132 | && !in_array($serviceAuthenticationResult['code'], [Result::FAILURE_CREDENTIAL_INVALID]) |
||
133 | ) { |
||
134 | if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { |
||
135 | return $fallbackMember; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $result->addError($serviceAuthenticationResult['message']); |
||
140 | |||
141 | return null; |
||
142 | } |
||
143 | $data = $service->getUserByUsername($serviceAuthenticationResult['identity']); |
||
144 | if (!$data) { |
||
145 | $result->addError( |
||
146 | _t( |
||
147 | 'LDAPAuthenticator.PROBLEMFINDINGDATA', |
||
148 | 'There was a problem retrieving your user data' |
||
149 | ) |
||
150 | ); |
||
151 | return null; |
||
152 | } |
||
153 | |||
154 | // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields |
||
155 | $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
||
156 | View Code Duplication | if (!($member && $member->exists())) { |
|
157 | $member = new Member(); |
||
158 | $member->GUID = $data['objectguid']; |
||
159 | } |
||
160 | |||
161 | // Update the users from LDAP so we are sure that the email is correct. |
||
162 | // This will also write the Member record. |
||
163 | $service->updateMemberFromLDAP($member); |
||
164 | |||
165 | $request->getSession()->clear('BackURL'); |
||
166 | |||
167 | return $member; |
||
168 | } |
||
169 | |||
217 |
This check marks private properties in classes that are never used. Those properties can be removed.