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