Conditions | 10 |
Paths | 7 |
Total Lines | 70 |
Code Lines | 39 |
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 |
||
105 | public function authenticateRequest(HTTPRequest $request) |
||
106 | { |
||
107 | $uidAndToken = Cookie::get($this->getTokenCookieName()); |
||
108 | $deviceID = Cookie::get($this->getDeviceCookieName()); |
||
109 | |||
110 | // @todo Consider better placement of database_is_ready test |
||
111 | if ($deviceID === null || strpos($uidAndToken, ':') === false || !Security::database_is_ready()) { |
||
112 | return null; |
||
113 | } |
||
114 | |||
115 | list($uid, $token) = explode(':', $uidAndToken, 2); |
||
116 | |||
117 | if (!$uid || !$token) { |
||
118 | return null; |
||
119 | } |
||
120 | |||
121 | // check if autologin token matches |
||
122 | /** @var Member $member */ |
||
123 | $member = Member::get()->byID($uid); |
||
124 | if (!$member) { |
||
125 | return null; |
||
126 | } |
||
127 | |||
128 | $hash = $member->encryptWithUserSettings($token); |
||
129 | |||
130 | /** @var RememberLoginHash $rememberLoginHash */ |
||
131 | $rememberLoginHash = RememberLoginHash::get() |
||
132 | ->filter(array( |
||
133 | 'MemberID' => $member->ID, |
||
134 | 'DeviceID' => $deviceID, |
||
135 | 'Hash' => $hash |
||
136 | ))->first(); |
||
137 | if (!$rememberLoginHash) { |
||
138 | return null; |
||
139 | } |
||
140 | |||
141 | // Check for expired token |
||
142 | $expiryDate = new \DateTime($rememberLoginHash->ExpiryDate); |
||
143 | $now = DBDatetime::now(); |
||
144 | $now = new \DateTime($now->Rfc2822()); |
||
145 | if ($now > $expiryDate) { |
||
146 | return null; |
||
147 | } |
||
148 | |||
149 | if ($this->cascadeInTo) { |
||
150 | // @todo look at how to block "regular login" triggers from happening here |
||
151 | // @todo deal with the fact that the Session::current_session() isn't correct here :-/ |
||
152 | $this->cascadeInTo->logIn($member, false, $request); |
||
153 | } |
||
154 | |||
155 | // @todo Consider whether response should be part of logIn() as well |
||
156 | |||
157 | // Renew the token |
||
158 | $rememberLoginHash->renew(); |
||
159 | $tokenExpiryDays = RememberLoginHash::config()->uninherited('token_expiry_days'); |
||
160 | Cookie::set( |
||
161 | $this->getTokenCookieName(), |
||
162 | $member->ID . ':' . $rememberLoginHash->getToken(), |
||
163 | $tokenExpiryDays, |
||
164 | null, |
||
165 | null, |
||
166 | false, |
||
167 | true |
||
168 | ); |
||
169 | |||
170 | // Audit logging hook |
||
171 | $member->extend('memberAutoLoggedIn'); |
||
172 | |||
173 | return $member; |
||
174 | } |
||
175 | |||
246 |