<?php
namespace App\Helper;
use App\Entity\TMA\TMALog;
use App\Entity\TMA\WPError;
use Doctrine\ORM\EntityManagerInterface;
class BDDHelper
{
public $entityManager;
public function __construct(EntityManagerInterface $entityManager = null)
{
$this->entityManager = $entityManager;
}
/**
* @param $url
* @param $duration
* @param $info
* @return void
*/
public function addTMALog($url, $duration, $info, $status = 0)
{
$tmaLog = new TMALog();
$tmaLog->setLogDate(new \DateTime());
$tmaLog->setUrl($url);
$tmaLog->setInfo($info);
$tmaLog->setStatus($status);
$tmaLog->setDuration($duration);
$this->entityManager->persist($tmaLog);
$this->entityManager->flush();
}
public function addTMAWpErrorLog($url, $error, $type, $filename, $fileline, $status = 0)
{
$errorLog = $this->entityManager->getRepository(WPError::class)->findOneBy(['error' => $error, 'url' => $url]);
$now = new \DateTime();
$dateHisto = '';
if($errorLog) {
$count = (int) $errorLog->getCountError();
$errorLog->setCountError($count + 1);
$errorLog->setStatus(WPError::STATUS_TODO);
$errorLog->setLogDate($now);
$dateHisto = $errorLog->getDateHisto().'#';
} else {
$errorLog = new WPError();
$errorLog->setLogDate($now);
$errorLog->setCountError(1);
$errorLog->setUrl($url);
$errorLog->setError($error);
$errorLog->setLineerror($filename.' : '.$fileline);
$errorLog->setType($type);
$errorLog->setStatus($status);
}
$dateHisto .= $now->format('d/m/Y H:i:s');
$errorLog->setDateHisto($dateHisto);
$this->entityManager->persist($errorLog);
$this->entityManager->flush();
}
}