2019-05-31 06:01:47 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-31 06:01:47 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-31 06:01:47 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class ThrottlesEmail.
|
2019-05-31 06:01:47 +02:00
|
|
|
*/
|
|
|
|
trait ThrottlesEmail
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
public function getDailyEmailLimit(Company $company)
|
|
|
|
{
|
|
|
|
$limit = config('ninja.daily_email_limit');
|
2019-05-31 06:01:47 +02:00
|
|
|
|
|
|
|
$limit += $company->created_at->diffInMonths() * 100;
|
|
|
|
|
|
|
|
return min($limit, 5000);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-05-31 06:01:47 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function isThrottled(Company $company)
|
2019-05-31 06:01:47 +02:00
|
|
|
{
|
|
|
|
$key = $company->company_key;
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/1375501/how-do-i-throttle-my-sites-api-users
|
|
|
|
$day = 60 * 60 * 24;
|
2020-10-28 11:10:49 +01:00
|
|
|
$day_limit = $this->getDailyEmailLimit($company);
|
2019-05-31 06:01:47 +02:00
|
|
|
$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}")) {
|
2020-09-06 11:38:10 +02:00
|
|
|
Mail::raw('Account Throttle: '.$company->company_key, function ($message) use ($error_email, $company) {
|
2019-05-31 06:01:47 +02:00
|
|
|
$message->to($error_email)
|
|
|
|
->from(config('ninja.contact.email'))
|
2020-09-06 11:38:10 +02:00
|
|
|
->subject('Email throttle triggered for company '.$company->id);
|
2019-05-31 06:01:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Cache::put("throttle_notified:{$key}", true, 60 * 24);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-05-31 06:01:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|