-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComplaintController.php
More file actions
110 lines (93 loc) · 3.27 KB
/
ComplaintController.php
File metadata and controls
110 lines (93 loc) · 3.27 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace App\Http\Controllers;
use App\Notifications\ComplaintNotificationExternal;
use App\Notifications\ComplaintNotification;
use App\Rules\ReCaptchaValidation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Notification;
use Illuminate\Validation\Rule;
use App\ComplaintRecord;
use Illuminate\Support\Facades\Log;
class ComplaintController extends Controller
{
/**
* @var \App\Rules\ReCaptchaValidation
*/
protected $recaptchaValidation;
public function __construct(ReCaptchaValidation $recaptchaValidation) {
$this->recaptchaValidation = $recaptchaValidation;
}
/**
* Handle a complaint report page request for the application.
*
* @param \Illuminate\Http\Request $request
*/
public function sendMessage(Request $request): \Illuminate\Http\JsonResponse
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$failed = $validator->failed();
if (isset($failed['recaptcha'])) {
abort(401);
} else {
abort(400);
}
}
$validated = $validator->safe();
$complaintRecord = new ComplaintRecord;
$complaintRecord->name = $validated['name'];
$complaintRecord->mail_address = $validated['email'];
$complaintRecord->reason = $validated['message'];
$complaintRecord->offending_urls = $validated['url'];
$complaintRecord->save();
if (! empty($complaintRecord->mail_address)) {
Notification::route('mail', [
$complaintRecord->mail_address,
])->notify(
new ComplaintNotificationExternal(
$complaintRecord->offending_urls,
$complaintRecord->reason,
$complaintRecord->name,
$complaintRecord->mail_address,
)
);
}
Notification::route('mail', [
config('app.complaint-mail-recipient'),
])->notify(
new ComplaintNotification(
$complaintRecord->offending_urls,
$complaintRecord->reason,
$complaintRecord->name,
$complaintRecord->mail_address,
)
);
$complaintRecord->markAsDispatched();
$complaintRecord->save();
return response()->json('Success', 200);
}
/**
* Get a validator for an incoming complaint report page request.
*/
protected function validator(array $data): \Illuminate\Validation\Validator
{
$data['name'] = $data['name'] ?? '';
$data['email'] = $data['email'] ?? '';
$validation = [
'recaptcha' => ['required', 'string', 'bail', $this->recaptchaValidation],
'name' => ['nullable', 'string', 'max:300'],
'message' => ['required', 'string', 'max:1000'],
'url' => ['required', 'string', 'max:1000'],
'email' => [
'nullable',
'max:300',
Rule::when(
!empty($data['email']),
['email:rfc']
),
],
];
return Validator::make($data, $validation);
}
}