|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Google2FALaravel\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse as IlluminateJsonResponse; |
|
6
|
|
|
use Illuminate\Http\Response as IlluminateHtmlResponse; |
|
7
|
|
|
use PragmaRX\Google2FALaravel\Events\OneTimePasswordRequested; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
|
9
|
|
|
|
|
10
|
|
|
trait Response |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Make a JSON response. |
|
14
|
|
|
* |
|
15
|
|
|
* @param $statusCode |
|
16
|
|
|
* |
|
17
|
|
|
* @return JsonResponse |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function makeJsonResponse($statusCode) |
|
20
|
|
|
{ |
|
21
|
|
|
return new IlluminateJsonResponse( |
|
22
|
|
|
$this->getErrorBagForStatusCode($statusCode), |
|
|
|
|
|
|
23
|
|
|
$statusCode |
|
24
|
|
|
); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Make the status code, to respond accordingly. |
|
29
|
|
|
* |
|
30
|
|
|
* @return int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function makeStatusCode() |
|
33
|
|
|
{ |
|
34
|
|
|
return |
|
35
|
|
|
$this->inputHasOneTimePassword() && !$this->checkOTP() |
|
|
|
|
|
|
36
|
|
|
? SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY |
|
37
|
|
|
: SymfonyResponse::HTTP_OK; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Make a web response. |
|
42
|
|
|
* |
|
43
|
|
|
* @param $statusCode |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Illuminate\Http\Response |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function makeHtmlResponse($statusCode) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($statusCode !== SymfonyResponse::HTTP_OK) { |
|
50
|
|
|
$this->getView()->withErrors($this->getErrorBagForStatusCode($statusCode)); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return new IlluminateHtmlResponse($this->getView(), $statusCode); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create a response to request the OTP. |
|
58
|
|
|
* |
|
59
|
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
|
60
|
|
|
*/ |
|
61
|
|
|
public function makeRequestOneTimePasswordResponse() |
|
62
|
|
|
{ |
|
63
|
|
|
event(new OneTimePasswordRequested($this->getUser())); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
return |
|
66
|
|
|
$this->getRequest()->expectsJson() |
|
|
|
|
|
|
67
|
|
|
? $this->makeJsonResponse($this->makeStatusCode()) |
|
68
|
|
|
: $this->makeHtmlResponse($this->makeStatusCode()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the OTP view. |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getView() |
|
77
|
|
|
{ |
|
78
|
|
|
return view($this->config('view')); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.