kphcdr 1 rok pred
rodič
commit
084c97839d

+ 27 - 0
app/Modules/Mini/Services/CaptchaService.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Modules\Mini\Services;
+
+use App\Base\BaseService;
+use Illuminate\Support\Facades\Cache;
+
+class CaptchaService extends BaseService
+{
+    public function createCaptcha($email)
+    {
+        return Cache::remember($this->cacheKey($email), 10 * 60, function () use ($email) {
+            return mt_rand(100000, 999999);
+        });
+    }
+
+    public function cacheKey($email): string
+    {
+        return "email:captcha" . $email;
+    }
+
+    public function checkCaptcha($email, $captcha): bool
+    {
+        return Cache::get($this->cacheKey($email)) == $captcha;
+    }
+
+}

+ 4 - 7
app/Modules/Mini/Services/EmailService.php

@@ -6,7 +6,6 @@ use App\Base\BaseService;
 use App\Exceptions\ClientException;
 use App\Mail\ForgetPasswordEmail;
 use App\Mail\RegisterEmail;
-use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Mail;
 
 class EmailService extends BaseService
@@ -25,9 +24,8 @@ class EmailService extends BaseService
 
     private function register($email): bool
     {
-        $captcha = Cache::remember("email:registerCaptcha" . $email, 10, function () {
-            return rand(100000, 999999);
-        });
+        $captcha = app(CaptchaService::class)->createCaptcha($email);
+
         try {
             Mail::to($email)->send(new RegisterEmail($captcha));
         } catch (\Exception $e) {
@@ -40,9 +38,8 @@ class EmailService extends BaseService
 
     private function forgetPassword($email): bool
     {
-        $captcha = Cache::remember("email:forgetPasswordCaptcha" . $email, 10, function () {
-            return rand(100000, 999999);
-        });
+        $captcha = app(CaptchaService::class)->createCaptcha($email);
+
         try {
             Mail::to($email)->send(new ForgetPasswordEmail($captcha));
         } catch (\Exception $e) {