2019-08-27 15:42:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Emails;
|
|
|
|
|
2019-08-29 14:48:14 +02:00
|
|
|
use Carbon\Carbon;
|
2019-08-27 15:42:13 +02:00
|
|
|
use Hooks\Manager;
|
|
|
|
use Notifications\EmailNotification;
|
2019-12-27 15:56:20 +01:00
|
|
|
use PHPMailer\PHPMailer\Exception;
|
2019-08-27 15:42:13 +02:00
|
|
|
|
|
|
|
class EmailHook extends Manager
|
|
|
|
{
|
2019-08-29 11:25:13 +02:00
|
|
|
public function isSingleton()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsExecution()
|
|
|
|
{
|
2019-09-06 17:40:02 +02:00
|
|
|
$diff = date('Y-m-d H:i:s', strtotime('-4 hours'));
|
2019-08-29 11:25:13 +02:00
|
|
|
$failed = function ($query) use ($diff) {
|
2019-09-12 10:45:36 +02:00
|
|
|
$query->where('failed_at', '<', $diff)
|
2019-08-29 11:25:13 +02:00
|
|
|
->orWhereNull('failed_at');
|
|
|
|
};
|
|
|
|
|
2019-09-12 10:45:36 +02:00
|
|
|
$accounts = Account::all();
|
2019-08-29 11:25:13 +02:00
|
|
|
$remaining = Mail::whereNull('sent_at')
|
|
|
|
->where($failed)
|
2019-10-29 16:29:59 +01:00
|
|
|
->where('attempt', '<', 10)
|
2019-09-12 10:45:36 +02:00
|
|
|
->whereIn('id_account', $accounts->pluck('id'))
|
2019-08-29 11:25:13 +02:00
|
|
|
->count();
|
|
|
|
|
|
|
|
return !empty($remaining);
|
|
|
|
}
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2019-09-06 17:40:02 +02:00
|
|
|
$diff = date('Y-m-d H:i:s', strtotime('-4 hours'));
|
2019-08-29 14:48:14 +02:00
|
|
|
$failed = function ($query) use ($diff) {
|
2019-09-12 10:45:36 +02:00
|
|
|
$query->where('failed_at', '<', $diff)
|
2019-08-29 14:48:14 +02:00
|
|
|
->orWhereNull('failed_at');
|
|
|
|
};
|
2019-08-27 15:42:13 +02:00
|
|
|
|
2019-08-29 14:48:14 +02:00
|
|
|
$accounts = Account::all();
|
2019-08-27 15:42:13 +02:00
|
|
|
$list = [];
|
|
|
|
foreach ($accounts as $account) {
|
2019-09-12 10:45:36 +02:00
|
|
|
$last_mail = $account->emails()
|
|
|
|
->whereNotNull('sent_at')
|
|
|
|
->orderBy('sent_at')
|
|
|
|
->first();
|
2019-08-29 14:48:14 +02:00
|
|
|
|
|
|
|
// Controllo sul timeout dell'account
|
|
|
|
$date = new Carbon($last_mail->sent_at);
|
|
|
|
$now = new Carbon();
|
2019-09-12 10:45:36 +02:00
|
|
|
$diff_milliseconds = $date->diffInMilliseconds($now);
|
2019-08-29 14:48:14 +02:00
|
|
|
|
2019-09-12 10:45:36 +02:00
|
|
|
if (empty($last_mail) || $diff_milliseconds > $account->timeout) {
|
2019-08-29 14:48:14 +02:00
|
|
|
$mail = Mail::whereNull('sent_at')
|
|
|
|
->where('id_account', $account->id)
|
|
|
|
->where($failed)
|
2019-10-29 16:29:59 +01:00
|
|
|
->where('attempt', '<', 10)
|
2019-08-29 14:48:14 +02:00
|
|
|
->orderBy('created_at')
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (!empty($mail)) {
|
|
|
|
$list[] = $mail;
|
|
|
|
}
|
2019-08-27 15:42:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($list as $mail) {
|
|
|
|
$email = EmailNotification::build($mail);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Invio mail
|
|
|
|
$email->send();
|
2019-12-27 15:56:20 +01:00
|
|
|
} catch (Exception $e) {
|
2019-08-27 15:42:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
return $list;
|
2019-08-27 15:42:13 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
public function response()
|
2019-08-27 15:42:13 +02:00
|
|
|
{
|
|
|
|
$yesterday = date('Y-m-d', strtotime('-1 days'));
|
|
|
|
$user = auth()->getUser();
|
|
|
|
|
2019-08-27 17:25:52 +02:00
|
|
|
$current = Mail::whereDate('sent_at', '>', $yesterday)
|
2019-10-29 16:29:59 +01:00
|
|
|
->where('attempt', '<', 10)
|
2019-08-27 15:42:13 +02:00
|
|
|
->where('created_by', $user->id)
|
|
|
|
->count();
|
2019-08-29 16:01:49 +02:00
|
|
|
$total = Mail::where(function ($query) use ($yesterday) {
|
|
|
|
$query->whereDate('sent_at', '>', $yesterday)
|
|
|
|
->orWhereNull('sent_at');
|
|
|
|
})
|
2019-10-29 16:29:59 +01:00
|
|
|
->where('attempt', '<', 10)
|
2019-08-27 15:42:13 +02:00
|
|
|
->where('created_by', $user->id)
|
|
|
|
->count();
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
$message = $total != $current ? tr('Invio email in corso...') : tr('Invio email completato!');
|
2019-08-28 11:52:04 +02:00
|
|
|
$message = empty($total) ? tr('Nessuna email presente...') : $message;
|
2019-08-27 15:42:13 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
'icon' => 'fa fa-envelope text-info',
|
|
|
|
'message' => $message,
|
2019-08-29 16:01:49 +02:00
|
|
|
'show' => ($total != $current),
|
2019-08-27 15:42:13 +02:00
|
|
|
'progress' => [
|
|
|
|
'current' => $current,
|
|
|
|
'total' => $total,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|