Total Complexity | 43 |
Total Lines | 468 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like PinService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PinService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class PinService { |
||
28 | |||
29 | use Helpers; |
||
30 | |||
31 | private Request $arrestedRequest; |
||
32 | private array $payload; |
||
33 | |||
34 | public function __construct() |
||
35 | { |
||
36 | $this->throttleRequestsService = new ThrottleRequestsService( |
||
37 | config('requirepin.max_attempts', 3), |
||
38 | config('requirepin.delay_minutes', 1) |
||
39 | ); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Handle Pin Change. |
||
44 | * |
||
45 | * @param \Ikechukwukalu\Requirepin\Requests\ChangePinRequest $request |
||
46 | * |
||
47 | * @return null |
||
48 | * @return array |
||
49 | */ |
||
50 | public function handlePinChange(ChangePinRequest $request) : ?array |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Handle Pin Authentication. |
||
66 | * |
||
67 | * @param \Illuminate\Http\Request $request |
||
68 | * @param string $uuid |
||
69 | * |
||
70 | * @return \Illuminate\Http\JsonResponse |
||
71 | * @return \Illuminate\Http\RedirectResponse |
||
72 | * @return \Illuminate\Http\Response |
||
73 | */ |
||
74 | public function handlePinRequired(Request $request, string $uuid): JsonResponse|Response|RedirectResponse |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Pin Request Attempts. |
||
105 | * |
||
106 | * @param \Illuminate\Http\Request $request |
||
107 | * |
||
108 | * @return null |
||
109 | * @return array |
||
110 | */ |
||
111 | public function pinRequestAttempts(Request $request, string $uuid): ?array |
||
112 | { |
||
113 | $response = $this->requestAttempts($request, 'requirepin::pin.throttle'); |
||
114 | |||
115 | if ($response) { |
||
116 | $requirePin = $this->getRequirePin($uuid); |
||
117 | $this->checkMaxTrial($requirePin); |
||
118 | } |
||
119 | |||
120 | return $response; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Valid Pin URL. |
||
125 | * |
||
126 | * @param \Illuminate\Http\Request $request |
||
127 | * |
||
128 | * @return null |
||
129 | * @return array |
||
130 | */ |
||
131 | public function pinUrlHasValidSignature(Request $request): ?array |
||
132 | { |
||
133 | if (!$request->hasValidSignature()) { |
||
134 | return ['message' => |
||
135 | trans('requirepin::pin.expired_url')]; |
||
136 | } |
||
137 | |||
138 | return null; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Valid UUID For Pin URL. |
||
143 | * |
||
144 | * @param string $uuid |
||
145 | * |
||
146 | * @return null |
||
147 | * @return array |
||
148 | */ |
||
149 | public function pinUrlHasValidUUID(string $uuid): ?array |
||
150 | { |
||
151 | if(!$this->getRequirePin($uuid)) { |
||
152 | return ['message' => |
||
153 | trans('requirepin::pin.invalid_url')]; |
||
154 | } |
||
155 | |||
156 | return null; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Pin Validation. |
||
161 | * |
||
162 | * @param \Illuminate\Http\Request $request |
||
163 | * |
||
164 | * @return null |
||
165 | * @return array |
||
166 | */ |
||
167 | public function pinValidation(Request $request): ?array |
||
168 | { |
||
169 | $validator = Validator::make($request->all(), [ |
||
170 | config('requirepin.input', '_pin') => ['required', 'string', |
||
171 | new CurrentPin(config('requirepin.allow_default_pin', false))] |
||
172 | ]); |
||
173 | |||
174 | if ($validator->fails()) { |
||
175 | return ['message' => $validator->errors()->first()]; |
||
176 | } |
||
177 | |||
178 | return null; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get RequirePin Model. |
||
183 | * |
||
184 | * @param string $uuid |
||
185 | * |
||
186 | * @return null |
||
187 | * @return \Ikechukwukalu\Requirepin\Models\RequirePin |
||
188 | */ |
||
189 | public function getRequirePin(string $uuid): ?RequirePin |
||
190 | { |
||
191 | return RequirePin::where('user_id', Auth::guard(config('requirepin.auth_guard', 'web'))->user()->id) |
||
192 | ->where('uuid', $uuid) |
||
193 | ->whereNull('approved_at') |
||
194 | ->whereNull('cancelled_at') |
||
195 | ->first(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Error Response For Pin Authentication. |
||
200 | * |
||
201 | * @param \Illuminate\Http\Request $request |
||
202 | * @param string $uuid |
||
203 | * @param int $status_code |
||
204 | * @param array $data |
||
205 | * |
||
206 | * @return \Illuminate\Http\JsonResponse |
||
207 | * @return \Illuminate\Http\RedirectResponse |
||
208 | * @return \Illuminate\Http\Response |
||
209 | */ |
||
210 | public function errorResponseForPinRequired(Request $request, string $uuid, int $status_code, array $data): JsonResponse|RedirectResponse|Response |
||
211 | { |
||
212 | if ($this->shouldResponseBeJson($request)) { |
||
213 | return $this->httpResponse($request, |
||
214 | trans('requirepin::general.fail'), $status_code, $data); |
||
215 | } |
||
216 | |||
217 | $requirePin = $this->getRequirePin($uuid); |
||
218 | |||
219 | if (isset($requirePin->pin_validation_url)) { |
||
220 | return back()->with('pin_validation', |
||
221 | json_encode([$data['message'], |
||
222 | $requirePin->pin_validation_url, $status_code])); |
||
223 | } |
||
224 | |||
225 | return back()->with('pin_validation', |
||
226 | json_encode([trans('requirepin::pin.unknown_error'), |
||
227 | 'javascript:void(0)', '500'])); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Pin Request Terminated. |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | public function pinRequestTerminated(Request $request): array |
||
236 | { |
||
237 | return [$request, trans('requirepin::general.fail'), 401, |
||
238 | ['message' => trans('requirepin::pin.terminated')]]; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Error Response For Pin Authentication. |
||
243 | * |
||
244 | * @param \Illuminate\Http\Request $request |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function isArrestedRequestValid(Request $request): bool |
||
249 | { |
||
250 | $param = config('requirepin.param', '_uuid'); |
||
251 | $requirePin = RequirePin::where('user_id', Auth::guard(config('requirepin.auth_guard', 'web'))->user()->id) |
||
252 | ->where('route_arrested', $request->path()) |
||
253 | ->where('uuid', $request->{$param}) |
||
254 | ->whereNull('approved_at') |
||
255 | ->whereNull('cancelled_at') |
||
256 | ->first(); |
||
257 | |||
258 | if (!isset($requirePin->id)) { |
||
259 | return false; |
||
260 | } |
||
261 | |||
262 | return true; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Cancel Unprocessed Arrested Request. |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | public function cancelAllOpenArrestedRequests(): void |
||
271 | { |
||
272 | RequirePin::where('user_id', Auth::guard(config('requirepin.auth_guard', 'web'))->user()->id) |
||
273 | ->whereNull('approved_at') |
||
274 | ->whereNull('cancelled_at') |
||
275 | ->update(['cancelled_at' => now()]); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Pin Validation For RequirePin Middleware. |
||
280 | * |
||
281 | * @param \Illuminate\Http\Request $request |
||
282 | * @param string $ip |
||
283 | * |
||
284 | * @return \Illuminate\Http\JsonResponse |
||
285 | * @return \Illuminate\Http\RedirectResponse |
||
286 | * @return \Illuminate\Http\Response |
||
287 | */ |
||
288 | public function requirePinValidationForRequest(Request $request, string $ip): JsonResponse|RedirectResponse|Response |
||
289 | { |
||
290 | $arrestRouteData = $this->arrestRequest($request, $ip); |
||
291 | [$status, $status_code, $data] = $this->pinValidationURL(...$arrestRouteData); |
||
292 | |||
293 | if ($this->shouldResponseBeJson($request)) |
||
294 | { |
||
295 | return ResponseFacade::json([ |
||
296 | 'status' => $status, |
||
297 | 'status_code' => $status_code, |
||
298 | 'data' => $data |
||
299 | ]); |
||
300 | } |
||
301 | |||
302 | return redirect(route('requirePinView'))->with('pin_validation', |
||
303 | json_encode([$data['message'], $data['url'], $status_code])); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Arrest Request. |
||
308 | * |
||
309 | * @param \Illuminate\Http\Request $request |
||
310 | * @param string $ip |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | private function arrestRequest(Request $request, string $ip): array |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Pin Validation URL. |
||
341 | * |
||
342 | * @param string $url |
||
343 | * @param null|string $redirect |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | private function pinValidationURL(string $url, null|string $redirect): array |
||
348 | { |
||
349 | return [trans('requirepin::general.success'), 200, |
||
350 | [ |
||
351 | 'message' => trans('requirepin::pin.require_pin'), |
||
352 | 'url' => $url, |
||
353 | 'redirect' => $redirect |
||
354 | ]]; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Dispatch Arrested Request. |
||
359 | * |
||
360 | * @param \Illuminate\Http\Request $request |
||
361 | * @param \Ikechukwukalu\Requirepin\Models\RequirePin $requirePin |
||
362 | * @param string $uuid |
||
363 | * |
||
364 | * @return \Illuminate\Http\JsonResponse |
||
365 | * @return \Illuminate\Http\RedirectResponse |
||
366 | * @return \Illuminate\Http\Response |
||
367 | */ |
||
368 | private function dispatchArrestedRequest(Request $request, RequirePin $requirePin, string $uuid): JsonResponse|RedirectResponse|Response |
||
369 | { |
||
370 | $this->arrestedRequest = Request::create($requirePin->route_arrested, |
||
371 | $requirePin->method, ['_uuid' => $uuid] + $this->payload); |
||
372 | |||
373 | if ($this->shouldResponseBeJson($request)) { |
||
374 | $this->arrestedRequest->headers->set('Accept', 'application/json'); |
||
375 | } |
||
376 | |||
377 | return Route::dispatch($this->arrestedRequest); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Transfer Sessions To New Request. |
||
382 | * |
||
383 | * @param \Illuminate\Http\Request $request |
||
384 | * |
||
385 | * @return void |
||
386 | */ |
||
387 | private function transferSessionsToNewRequest(Request $request): void |
||
388 | { |
||
389 | if ($this->shouldResponseBeJson($request)) |
||
390 | { |
||
391 | return; |
||
392 | } |
||
393 | |||
394 | foreach ($this->arrestedRequest->session()->all() as $key => $session) { |
||
395 | if (!in_array($key, ['_old_input', '_previous', 'errors'])) { |
||
396 | continue; |
||
397 | } |
||
398 | |||
399 | $request->session()->flash($key, $session); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Update Current Request. |
||
405 | * |
||
406 | * @param \Illuminate\Http\Request $request |
||
407 | * @param \Ikechukwukalu\Requirepin\Models\RequirePin $requirePin |
||
408 | * |
||
409 | * @return void |
||
410 | */ |
||
411 | private function updateCurrentRequest(Request $request, RequirePin $requirePin): void |
||
412 | { |
||
413 | $this->payload = unserialize(Crypt::decryptString($requirePin->payload)); |
||
414 | |||
415 | $request->merge([ |
||
416 | 'expires' => null, |
||
417 | 'signature' => null, |
||
418 | config('requirepin.input', '_pin') => null |
||
419 | ]); |
||
420 | |||
421 | foreach($this->payload as $key => $item) { |
||
422 | $request->merge([$key => $item]); |
||
423 | } |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Save User's New Pin. |
||
428 | * |
||
429 | * @param array $validated |
||
430 | * |
||
431 | * @return null |
||
432 | * @return \App\Models\User |
||
433 | */ |
||
434 | private function saveNewPin(array $validated) |
||
435 | { |
||
436 | $user = Auth::guard(config('requirepin.auth_guard', 'web'))->user(); |
||
437 | $user->pin = Hash::make($validated['pin']); |
||
438 | $user->default_pin = (string) $validated['pin'] === (string) config('requirepin.default', '0000'); |
||
439 | |||
440 | if ($user->save()) { |
||
441 | return $user; |
||
442 | } |
||
443 | |||
444 | return null; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Save User's Old Pin. |
||
449 | * |
||
450 | * @param \App\Models\User $user |
||
451 | * @param array $validated |
||
452 | * |
||
453 | * @return void |
||
454 | */ |
||
455 | private function saveOldPin(User|TestUser $user, array $validated): void |
||
456 | { |
||
457 | OldPin::create([ |
||
458 | 'user_id' => $user->id, |
||
459 | 'pin' => Hash::make($validated['current_pin']) |
||
460 | ]); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Send Pin change Notification. |
||
465 | * |
||
466 | * @param \App\Models\User $user |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | private function sendPinChangeNotification(User|TestUser $user): void |
||
471 | { |
||
472 | if (config('requirepin.notify.change', true) |
||
473 | && env('APP_ENV') !== 'package_testing') { |
||
474 | $user->notify(new PinChange()); |
||
475 | } |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * Max Route Dispatch For Arrested Request. |
||
480 | * |
||
481 | * @param \Ikechukwukalu\Requirepin\Models\RequirePin $requirePin |
||
482 | * |
||
483 | * @return void |
||
484 | */ |
||
485 | private function checkMaxTrial(RequirePin $requirePin): void |
||
495 | } |
||
496 | } |
||
497 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths