102 lines
2.8 KiB
PHP
Executable File
102 lines
2.8 KiB
PHP
Executable File
<?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;
|
|
|
|
protected $scrivisoloseaggiornato = false;
|
|
|
|
public function __construct($settingOra, $channel, $scrivisoloseaggiornato = false)
|
|
{
|
|
$this->settingOra = $settingOra;
|
|
$this->channel = $channel;
|
|
$this->scrivisoloseaggiornato = $scrivisoloseaggiornato;
|
|
$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 (($this->scrivisoloseaggiornato && $this->aggiornato) || !$this->scrivisoloseaggiornato) {
|
|
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 ($type === '') {
|
|
$type = 'LOG';
|
|
}
|
|
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;
|
|
}
|
|
}
|