|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Requirepin\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Ikechukwukalu\Requirepin\Requests\ChangePinRequest; |
|
6
|
|
|
use Ikechukwukalu\Requirepin\Services\PinService; |
|
7
|
|
|
use Ikechukwukalu\Requirepin\Traits\Helpers; |
|
8
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
9
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
|
10
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
|
11
|
|
|
use Illuminate\Routing\Controller as BaseController; |
|
12
|
|
|
use Illuminate\Http\JsonResponse; |
|
13
|
|
|
use Illuminate\Http\RedirectResponse; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use Illuminate\Http\Response; |
|
16
|
|
|
use Illuminate\View\View; |
|
17
|
|
|
|
|
18
|
|
|
class PinController extends BaseController |
|
19
|
|
|
{ |
|
20
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
|
21
|
|
|
use Helpers; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
public function __construct(private PinService $pinService) |
|
24
|
|
|
{} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Change Pin. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Ikechukwukalu\Requirepin\Requests\ChangePinRequest $request |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
32
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
33
|
|
|
* @return \Illuminate\Http\Response |
|
34
|
|
|
*/ |
|
35
|
|
|
public function changePin(ChangePinRequest $request): JsonResponse|RedirectResponse|Response |
|
36
|
|
|
{ |
|
37
|
|
|
if ($data = $this->pinService->handlePinChange($request)) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->httpResponse($request, |
|
|
|
|
|
|
40
|
|
|
trans('requirepin::general.success'), 200, $data); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->unknownErrorResponse($request); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Pin Authentication. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Illuminate\Http\Request $request |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
52
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
53
|
|
|
* @return \Illuminate\Http\Response |
|
54
|
|
|
*/ |
|
55
|
|
|
public function pinRequired(Request $request, string $uuid): JsonResponse|RedirectResponse|Response |
|
56
|
|
|
{ |
|
57
|
|
|
if ($data = $this->pinService->pinRequestAttempts($request, $uuid)) { |
|
58
|
|
|
return $this->pinService->errorResponseForPinRequired($request, |
|
|
|
|
|
|
59
|
|
|
$uuid, 500, $data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($data = $this->pinService->pinUrlHasValidSignature($request)) { |
|
63
|
|
|
return $this->pinService->errorResponseForPinRequired($request, |
|
|
|
|
|
|
64
|
|
|
$uuid, 400, $data); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($data = $this->pinService->pinUrlHasValidUUID($uuid)) { |
|
68
|
|
|
return $this->pinService->errorResponseForPinRequired($request, |
|
|
|
|
|
|
69
|
|
|
$uuid, 401, $data); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($data = $this->pinService->pinValidation($request)) { |
|
73
|
|
|
return $this->pinService->errorResponseForPinRequired($request, |
|
|
|
|
|
|
74
|
|
|
$uuid, 400, $data); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->pinService->handlePinRequired($request, $uuid); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Change Pin View. |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Illuminate\View\View |
|
84
|
|
|
*/ |
|
85
|
|
|
public function changePinView(): View |
|
86
|
|
|
{ |
|
87
|
|
|
return view('requirepin::pin.changepin'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Require Pin View. |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Illuminate\View\View |
|
94
|
|
|
*/ |
|
95
|
|
|
public function requirePinView(): View |
|
96
|
|
|
{ |
|
97
|
|
|
return view('requirepin::pin.pinrequired'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|