1 | <?php |
||
14 | trait Response |
||
15 | { |
||
16 | /** |
||
17 | * Make a JSON response. |
||
18 | * |
||
19 | * @param $statusCode |
||
20 | * |
||
21 | * @return IlluminateJsonResponse |
||
22 | */ |
||
23 | protected function makeJsonResponse($statusCode) |
||
30 | |||
31 | /** |
||
32 | * Make the status code, to respond accordingly. |
||
33 | * |
||
34 | * @return int |
||
35 | */ |
||
36 | protected function makeStatusCode() |
||
37 | { |
||
38 | if ($this->getRequest()->isMethod('get') || ($this->checkOTP() === Constants::OTP_VALID)) { |
||
39 | return SymfonyResponse::HTTP_OK; |
||
40 | } |
||
41 | |||
42 | if ($this->checkOTP() === Constants::OTP_EMPTY) { |
||
43 | return SymfonyResponse::HTTP_BAD_REQUEST; |
||
44 | } |
||
45 | |||
46 | return SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Make a web response. |
||
51 | * |
||
52 | * @param $statusCode |
||
53 | * |
||
54 | * @return \Illuminate\Http\Response |
||
55 | */ |
||
56 | protected function makeHtmlResponse($statusCode) |
||
57 | { |
||
58 | $view = $this->getView(); |
||
59 | |||
60 | if ($statusCode !== SymfonyResponse::HTTP_OK) { |
||
61 | $view->withErrors($this->getErrorBagForStatusCode($statusCode)); |
||
62 | } |
||
63 | |||
64 | return new IlluminateHtmlResponse($view, $statusCode); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Create a response to request the OTP. |
||
69 | * |
||
70 | * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
||
71 | */ |
||
72 | public function makeRequestOneTimePasswordResponse() |
||
73 | { |
||
74 | event( |
||
75 | app()->version() < '5.4' |
||
76 | ? new OneTimePasswordRequested53($this->getUser()) |
||
77 | : new OneTimePasswordRequested($this->getUser()) |
||
78 | ); |
||
79 | |||
80 | $expectJson = app()->version() < '5.4' |
||
81 | ? $this->getRequest()->wantsJson() |
||
82 | : $this->getRequest()->expectsJson(); |
||
83 | |||
84 | return $expectJson |
||
85 | ? $this->makeJsonResponse($this->makeStatusCode()) |
||
86 | : $this->makeHtmlResponse($this->makeStatusCode()); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get the OTP view. |
||
91 | * |
||
92 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
93 | */ |
||
94 | private function getView() |
||
95 | { |
||
96 | return view($this->config('view')); |
||
97 | } |
||
98 | |||
99 | abstract protected function getErrorBagForStatusCode($statusCode); |
||
100 | |||
101 | abstract protected function inputHasOneTimePassword(); |
||
102 | |||
103 | abstract public function checkOTP(); |
||
104 | |||
105 | abstract protected function getUser(); |
||
106 | |||
107 | abstract public function getRequest(); |
||
108 | |||
109 | abstract protected function config($string, $children = []); |
||
110 | } |
||
111 |