Files
apimacro/app/Services/ProductLogger.php
paoloar77 ab1144c659 aa
2024-06-19 18:54:22 +02:00

96 lines
2.5 KiB
PHP

<?php
namespace App\Services;
use Carbon\Carbon;
use App\Setting;
use App\Article;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use App\Mylog;
class ProductLogger
{
public $logs = [];
protected $settingOra = null;
protected $channel = "";
protected $aggiornato = false;
public function __construct($settingOra, $channel)
{
$this->settingOra = $settingOra;
$this->channel = $channel;
$this->init();
}
public function init()
{
$oraUpdate = Carbon::now();
if ($this->settingOra) {
$this->logs['start'] = 'Inizio da ' . $this->settingOra->value . "\n<br>";
$this->logs['end'] = 'Fino a ' . $oraUpdate->toDateTimeString() . "\n<br>";
} else {
$this->logs['start'] = 'Iniziato: ' . $oraUpdate->toDateTimeString() . "\n<br>";
}
$this->logs['custom'] = ""; // for custom log messages
}
public function updateSettingOra($newOra)
{
$this->settingOra = $newOra;
}
public function setLogandSendEmail($descr)
{
if (is_object($this->settingOra) && method_exists($this->settingOra, 'save')) {
$ora_update = Carbon::now();
$this->settingOra->value = $ora_update->toDateTimeString();
$this->settingOra->save();
} else {
// Gestisci il caso in cui $this->settingOra non è un oggetto o non ha il metodo save
// Log::warning('Impossibile eseguire il metodo save() su $this->settingOra, non è un oggetto o il metodo non esiste.');
}
if (true) {
Log::channel($this->channel)->notice($this->concatenateLogs());
Mail::raw($this->concatenateLogs(), function ($message) use ($descr) {
$message->to(Mylog::getEmail());
$message->subject(Mylog::getSubjectEmail($descr));
});
}
}
public function addLog($type, $message)
{
if (!isset($this->logs[$type])) {
$this->logs[$type] = "";
}
if (isset($this->logs[$type])) {
$this->logs[$type] .= $message . "\n<br>";
}
}
public function getLogs()
{
return $this->logs;
}
public function concatenateLogs()
{
return implode("", $this->logs);
}
public function setAggiornato($aggiornato)
{
$this->aggiornato = $aggiornato;
}
public function getAggiornato()
{
return $this->aggiornato;
}
}