openstamanager/modules/emails/src/EmailHook.php

85 lines
2.2 KiB
PHP
Raw Normal View History

2019-08-27 15:42:13 +02:00
<?php
namespace Modules\Emails;
use Hooks\Manager;
use Notifications\EmailNotification;
class EmailHook extends Manager
{
public function execute()
{
2019-08-29 10:25:14 +02:00
$accounts = Account::all();
2019-08-27 15:42:13 +02:00
$diff = date('Y-m-d', strtotime('-4 hours'));
$list = [];
foreach ($accounts as $account) {
$mail = Mail::whereNull('sent_at')
->where('id_account', $account->id)
->whereNull('failed_at')
->orWhereDate('failed_at', '<', $diff)
->orderBy('created_at')
->first();
if (!empty($mail)) {
$list[] = $mail;
}
}
foreach ($list as $mail) {
$email = EmailNotification::build($mail);
try {
// Invio mail
$email->send();
} catch (PHPMailer\PHPMailer\Exception $e) {
}
}
return count($list);
}
public function response($data)
{
return $this->prepare();
}
public function prepare()
{
$yesterday = date('Y-m-d', strtotime('-1 days'));
2019-08-27 15:48:31 +02:00
$diff = date('Y-m-d', strtotime('-4 hours'));
2019-08-27 15:42:13 +02:00
$user = auth()->getUser();
2019-08-28 16:58:47 +02:00
$failed = function ($query) use ($diff) {
2019-08-27 17:25:52 +02:00
$query->whereDate('failed_at', '<', $diff)
->orWhereNull('failed_at');
};
$current = Mail::whereDate('sent_at', '>', $yesterday)
2019-08-27 15:42:13 +02:00
->where('created_by', $user->id)
->count();
$total = Mail::whereDate('sent_at', '>', $yesterday)
->orWhereNull('sent_at')
->where('created_by', $user->id)
->count();
2019-08-28 11:52:04 +02:00
$remaining = Mail::whereNull('sent_at')
2019-08-27 17:25:52 +02:00
->where($failed)
2019-08-27 15:48:31 +02:00
->count();
2019-08-27 15:42:13 +02:00
$message = !empty($remaining) ? 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-28 11:52:04 +02:00
'execute' => !empty($remaining),
2019-08-27 15:42:13 +02:00
'show' => true,
'progress' => [
'current' => $current,
'total' => $total,
],
];
}
}