From adfd7fa533114663052895ca330cbf7f61637a19 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 Apr 2023 11:35:34 +1000 Subject: [PATCH] Code cleanup --- app/Models/Company.php | 2 - app/Utils/Traits/ThrottlesEmail.php | 72 ----------------------------- 2 files changed, 74 deletions(-) delete mode 100644 app/Utils/Traits/ThrottlesEmail.php diff --git a/app/Models/Company.php b/app/Models/Company.php index df8d643bf0e2..e4bcfca7aa7b 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -18,7 +18,6 @@ use App\Utils\Ninja; use App\Utils\Traits\AppSetup; use App\Utils\Traits\CompanySettingsSaver; use App\Utils\Traits\MakesHash; -use App\Utils\Traits\ThrottlesEmail; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Notifications\Notification; @@ -785,7 +784,6 @@ class Company extends BaseModel use PresentableTrait; use MakesHash; use CompanySettingsSaver; - use ThrottlesEmail; use AppSetup; use \Awobaz\Compoships\Compoships; diff --git a/app/Utils/Traits/ThrottlesEmail.php b/app/Utils/Traits/ThrottlesEmail.php deleted file mode 100644 index 8bb2e7afe04e..000000000000 --- a/app/Utils/Traits/ThrottlesEmail.php +++ /dev/null @@ -1,72 +0,0 @@ -created_at->diffInMonths() * 100; - - return min($limit, 5000); - } - - public function isThrottled(Company $company) - { - $key = $company->company_key; - - // http://stackoverflow.com/questions/1375501/how-do-i-throttle-my-sites-api-users - $day = 60 * 60 * 24; - $day_limit = $this->getDailyEmailLimit($company); - $day_throttle = Cache::get("email_day_throttle:{$key}", null); - $last_api_request = Cache::get("last_email_request:{$key}", 0); - $last_api_diff = time() - $last_api_request; - - if (is_null($day_throttle)) { - $new_day_throttle = 0; - } else { - $new_day_throttle = $day_throttle - $last_api_diff; - $new_day_throttle = $new_day_throttle < 0 ? 0 : $new_day_throttle; - $new_day_throttle += $day / $day_limit; - $day_hits_remaining = floor(($day - $new_day_throttle) * $day_limit / $day); - $day_hits_remaining = $day_hits_remaining >= 0 ? $day_hits_remaining : 0; - } - - Cache::put("email_day_throttle:{$key}", $new_day_throttle, 60); - Cache::put("last_email_request:{$key}", time(), 60); - - if ($new_day_throttle > $day) { - $error_email = config('ninja.error_email'); - if ($error_email && ! Cache::get("throttle_notified:{$key}")) { - Mail::raw('Account Throttle: '.$company->company_key, function ($message) use ($error_email, $company) { - $message->to($error_email) - ->from(config('ninja.contact.email')) - ->subject('Email throttle triggered for company '.$company->id); - }); - } - Cache::put("throttle_notified:{$key}", true, 60 * 24); - - return true; - } - - return false; - } -}