|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Notifications\ComplaintNotificationExternal; |
| 6 | +use App\Notifications\ComplaintNotification; |
| 7 | +use App\Rules\ReCaptchaValidation; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Support\Facades\Validator; |
| 10 | +use Illuminate\Support\Facades\Notification; |
| 11 | +use Illuminate\Validation\Rule; |
| 12 | +use App\ComplaintRecord; |
| 13 | +use Illuminate\Support\Facades\Log; |
| 14 | + |
| 15 | +class ComplaintController extends Controller |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var \App\Rules\ReCaptchaValidation |
| 19 | + */ |
| 20 | + protected $recaptchaValidation; |
| 21 | + |
| 22 | + public function __construct(ReCaptchaValidation $recaptchaValidation) { |
| 23 | + $this->recaptchaValidation = $recaptchaValidation; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Handle a complaint report page request for the application. |
| 28 | + * |
| 29 | + * @param \Illuminate\Http\Request $request |
| 30 | + */ |
| 31 | + public function sendMessage(Request $request): \Illuminate\Http\JsonResponse |
| 32 | + { |
| 33 | + $validator = $this->validator($request->all()); |
| 34 | + |
| 35 | + if ($validator->fails()) { |
| 36 | + $failed = $validator->failed(); |
| 37 | + |
| 38 | + if (isset($failed['recaptcha'])) { |
| 39 | + abort(401); |
| 40 | + } else { |
| 41 | + abort(400); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + $validated = $validator->safe(); |
| 46 | + |
| 47 | + $complaintRecord = new ComplaintRecord; |
| 48 | + $complaintRecord->name = $validated['name']; |
| 49 | + $complaintRecord->mail_address = $validated['email']; |
| 50 | + $complaintRecord->reason = $validated['message']; |
| 51 | + $complaintRecord->offending_urls = $validated['url']; |
| 52 | + $complaintRecord->save(); |
| 53 | + |
| 54 | + if (! empty($complaintRecord->mail_address)) { |
| 55 | + Notification::route('mail', [ |
| 56 | + $complaintRecord->mail_address, |
| 57 | + ])->notify( |
| 58 | + new ComplaintNotificationExternal( |
| 59 | + $complaintRecord->offending_urls, |
| 60 | + $complaintRecord->reason, |
| 61 | + $complaintRecord->name, |
| 62 | + $complaintRecord->mail_address, |
| 63 | + ) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + Notification::route('mail', [ |
| 68 | + config('app.complaint-mail-recipient'), |
| 69 | + ])->notify( |
| 70 | + new ComplaintNotification( |
| 71 | + $complaintRecord->offending_urls, |
| 72 | + $complaintRecord->reason, |
| 73 | + $complaintRecord->name, |
| 74 | + $complaintRecord->mail_address, |
| 75 | + ) |
| 76 | + ); |
| 77 | + |
| 78 | + $complaintRecord->markAsDispatched(); |
| 79 | + $complaintRecord->save(); |
| 80 | + |
| 81 | + return response()->json('Success', 200); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get a validator for an incoming complaint report page request. |
| 86 | + */ |
| 87 | + protected function validator(array $data): \Illuminate\Validation\Validator |
| 88 | + { |
| 89 | + $data['name'] = $data['name'] ?? ''; |
| 90 | + $data['email'] = $data['email'] ?? ''; |
| 91 | + |
| 92 | + $validation = [ |
| 93 | + 'recaptcha' => ['required', 'string', 'bail', $this->recaptchaValidation], |
| 94 | + 'name' => ['nullable', 'string', 'max:300'], |
| 95 | + 'message' => ['required', 'string', 'max:1000'], |
| 96 | + 'url' => ['required', 'string', 'max:1000'], |
| 97 | + |
| 98 | + 'email' => [ |
| 99 | + 'nullable', |
| 100 | + 'max:300', |
| 101 | + Rule::when( |
| 102 | + !empty($data['email']), |
| 103 | + ['email:rfc'] |
| 104 | + ), |
| 105 | + ], |
| 106 | + ]; |
| 107 | + |
| 108 | + return Validator::make($data, $validation); |
| 109 | + } |
| 110 | +} |
0 commit comments