Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Sepa | |
0.00% |
0 / 55 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| send_vefification_email | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
90 | |||
| isEmailValid | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use Illuminate\Http\Request; |
| 6 | use Illuminate\Support\Facades\Validator; |
| 7 | |
| 8 | class Sepa extends Controller |
| 9 | { |
| 10 | public function __construct() {} |
| 11 | |
| 12 | public function send_vefification_email(Request $request) |
| 13 | { |
| 14 | |
| 15 | try { |
| 16 | |
| 17 | $data = $request->all(); |
| 18 | |
| 19 | $validator = Validator::make($data, [ |
| 20 | 'email' => ['required', 'email'], |
| 21 | 'verification_link' => ['required', 'url'], |
| 22 | ]); |
| 23 | |
| 24 | if ($validator->fails()) { |
| 25 | |
| 26 | if ($validator->errors()->has('verification_link') |
| 27 | && ! $request->has('verification_link')) { |
| 28 | |
| 29 | return response()->json([ |
| 30 | 'status' => 'error', |
| 31 | 'message' => 'Missing required field: verification_link', |
| 32 | 'code' => 'MISSING_FIELD', |
| 33 | ], 400); |
| 34 | } |
| 35 | |
| 36 | if ($validator->errors()->has('email') |
| 37 | && ! $request->has('email')) { |
| 38 | |
| 39 | return response()->json([ |
| 40 | 'status' => 'error', |
| 41 | 'message' => 'Missing required field: email', |
| 42 | 'code' => 'MISSING_FIELD', |
| 43 | ], 400); |
| 44 | } |
| 45 | |
| 46 | if (! $this->isEmailValid($data['email'])) { |
| 47 | return response([ |
| 48 | 'status' => 'error', |
| 49 | 'message' => 'Invalid email address format', |
| 50 | 'code' => 'INVALID_EMAIL', |
| 51 | ], 400); |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | $toEmail = $data['email']; |
| 57 | |
| 58 | $mail = new \SendGrid\Mail\Mail; |
| 59 | |
| 60 | $mail->setFrom('fire@fire.es', 'Fire Service Titan'); |
| 61 | $mail->addTo($toEmail); |
| 62 | $mail->setTemplateId('d-39337e738fc04ba68767b7d13124e2cf'); |
| 63 | $mail->addDynamicTemplateDatas([ |
| 64 | 'verification_link' => $data['verification_link'], |
| 65 | ]); |
| 66 | |
| 67 | $sendgrid = new \SendGrid(env('SENDGRID_API_KEY')); |
| 68 | |
| 69 | $response = $sendgrid->send($mail); |
| 70 | if ($response->statusCode() == 202) { |
| 71 | return response([ |
| 72 | 'status' => 'success', |
| 73 | 'message' => 'Verification email sent successfully', |
| 74 | 'email' => $toEmail, |
| 75 | ], 200); |
| 76 | } |
| 77 | |
| 78 | return response([ |
| 79 | 'status' => 'error', |
| 80 | 'message' => 'Failed to send email. Please try again later.', |
| 81 | 'code' => 'EMAIL_DELIVERY_FAILED', |
| 82 | ], 500); |
| 83 | |
| 84 | } catch (\Exception $e) { |
| 85 | /** @disregard P1014 */ |
| 86 | $e->exceptionCode = 'SEND_VERIFICATION_EMAIL_EXCEPTION'; |
| 87 | report($e); |
| 88 | |
| 89 | return response(['message' => 'KO', 'error' => $e->getMessage()]); |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | public function isEmailValid($email) |
| 95 | { |
| 96 | |
| 97 | $pattern = '/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/'; |
| 98 | |
| 99 | if (preg_match($pattern, $email)) { |
| 100 | return true; |
| 101 | } else { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | } |