|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MagicLink\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Http\RedirectResponse; |
|
7
|
|
|
use Illuminate\View\View; |
|
8
|
|
|
use Laravel\SerializableClosure\SerializableClosure; |
|
9
|
|
|
use MagicLink\MagicLink; |
|
10
|
|
|
|
|
11
|
|
|
class ResponseAction extends ActionAbstract |
|
12
|
|
|
{ |
|
13
|
|
|
protected $httpResponse; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Constructor to action. |
|
17
|
|
|
* |
|
18
|
|
|
* @param mixed $httpResponse |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($httpResponse = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->response($httpResponse); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function response($response): self |
|
26
|
|
|
{ |
|
27
|
|
|
$this->httpResponse = $this->serializeResponse($response); |
|
28
|
|
|
|
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function redirect($response): self |
|
33
|
|
|
{ |
|
34
|
|
|
$this->httpResponse = $this->serializeResponse($response); |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function serializeResponse($httpResponse) |
|
40
|
|
|
{ |
|
41
|
|
|
return serialize($this->formattedResponse($httpResponse)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function formattedResponse($response) |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_null($response)) { |
|
47
|
|
|
return new RedirectResponse( |
|
48
|
|
|
config('magiclink.url.redirect_default', '/'), |
|
49
|
|
|
302 |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($response instanceof RedirectResponse) { |
|
54
|
|
|
return new RedirectResponse( |
|
55
|
|
|
$response->getTargetUrl(), |
|
56
|
|
|
$response->getStatusCode() |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (is_callable($response)) { |
|
61
|
|
|
return new SerializableClosure(Closure::fromCallable($response)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($response instanceof View) { |
|
65
|
|
|
return $response->render(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Execute Action. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function run() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->callResponse(unserialize($this->httpResponse)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function callResponse($httpResponse) |
|
80
|
|
|
{ |
|
81
|
|
|
if (is_callable($httpResponse)) { |
|
82
|
|
|
return $httpResponse(MagicLink::find($this->magiclinkId)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $httpResponse; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|