akaunting/app/Traits/Emails.php

52 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2022-09-14 11:53:22 +03:00
<?php
namespace App\Traits;
use App\Abstracts\Job;
use App\Events\Email\TooManyEmailsSent;
2022-09-14 11:53:22 +03:00
use App\Traits\Jobs;
use Illuminate\Support\Facades\RateLimiter;
trait Emails
{
use Jobs;
public function sendEmail(Job $job): array
{
// Check if the user has reached the limit of emails per month
$key_per_month = 'email-month:' . user_id();
2022-09-14 11:53:22 +03:00
$limit_per_month = config('app.throttles.email.month');
$decay_per_month = 60 * 60 * 24 * 30;
2023-03-22 00:23:41 +03:00
$can_send = RateLimiter::attempt($key_per_month, $limit_per_month, fn() => null, $decay_per_month);
2022-09-14 11:53:22 +03:00
if ($can_send) {
// Check if the user has reached the limit of emails per minute
$key_per_minute = 'email-minute:' . user_id();
2022-09-14 11:53:22 +03:00
$limit_per_minute = config('app.throttles.email.minute');
2023-03-22 00:23:41 +03:00
$can_send = RateLimiter::attempt($key_per_minute, $limit_per_minute, fn() => null);
2022-09-14 11:53:22 +03:00
}
if ($can_send) {
$this->dispatch($job);
return [
2022-09-14 11:53:22 +03:00
'success' => true,
'error' => false,
'data' => '',
'message' => '',
];
}
event(new TooManyEmailsSent(user_id()));
return [
2022-09-14 11:53:22 +03:00
'success' => false,
'error' => true,
'data' => null,
'message' => 'Too many emails sent!',
];
}
}