1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\CaptainHook\Http; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Laravel\Spark\Http\Controllers\Controller; |
7
|
|
|
use Mpociot\CaptainHook\Http\Requests\CreateWebhookRequest; |
8
|
|
|
use Mpociot\CaptainHook\Http\Requests\UpdateWebhookRequest; |
9
|
|
|
use Mpociot\CaptainHook\Webhook; |
10
|
|
|
|
11
|
|
|
class WebhookController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Create a new controller instance. |
15
|
|
|
* |
16
|
|
|
* @return void |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get all of the webhooks generated by the user. |
24
|
|
|
* |
25
|
|
|
* @return Response |
26
|
|
|
*/ |
27
|
|
|
public function all(Request $request) |
28
|
|
|
{ |
29
|
|
|
return Webhook::where('tenant_id', $this->getTenantId($request)) |
30
|
|
|
->with('lastLog') |
31
|
|
|
->with('logs') |
32
|
|
|
->orderBy('created_at', 'desc') |
33
|
|
|
->get(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new webhook for the user. |
38
|
|
|
* |
39
|
|
|
* @param CreateWebhookRequest $request |
40
|
|
|
* @return Response |
41
|
|
|
*/ |
42
|
|
|
public function store(CreateWebhookRequest $request) |
43
|
|
|
{ |
44
|
|
|
$hook = Webhook::create([ |
45
|
|
|
'url' => $request->url, |
|
|
|
|
46
|
|
|
'tenant_id' => $this->getTenantId($request), |
47
|
|
|
'event' => $request->event, |
|
|
|
|
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
return response()->json($hook); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Update the given webhook. |
55
|
|
|
* |
56
|
|
|
* @param UpdateWebhookRequest $request |
57
|
|
|
* @param string $webhookId |
58
|
|
|
* @return Response |
59
|
|
|
*/ |
60
|
|
|
public function update(UpdateWebhookRequest $request, $webhookId) |
61
|
|
|
{ |
62
|
|
|
$webhook = Webhook::where('tenant_id', $this->getTenantId($request)) |
63
|
|
|
->where('id', $webhookId) |
64
|
|
|
->firstOrFail(); |
65
|
|
|
|
66
|
|
|
$webhook->url = $request->url; |
|
|
|
|
67
|
|
|
$webhook->event = $request->event; |
|
|
|
|
68
|
|
|
$webhook->save(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Delete the given webhook. |
73
|
|
|
* |
74
|
|
|
* @param Request $request |
75
|
|
|
* @param string $webhookId |
76
|
|
|
* @return Response |
77
|
|
|
*/ |
78
|
|
|
public function destroy(Request $request, $webhookId) |
79
|
|
|
{ |
80
|
|
|
Webhook::where('tenant_id', $this->getTenantId($request)) |
81
|
|
|
->where('id', $webhookId) |
82
|
|
|
->firstOrFail() |
83
|
|
|
->delete(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getTenantId(Request $request) |
87
|
|
|
{ |
88
|
|
|
return (config('captain_hook.tenant_spark_model', 'User') == 'Team' && isset($request->user()->currentTeam)) ? $request->user()->currentTeam->id : $request->user()->getKey(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.