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