<?php
namespace App\Service;
use App\Entity\Log;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;
class LogService
{
private $user;
private $em;
public function __construct(Security $security, EntityManagerInterface $em) {
$this->user = $security->getUser();
$this->em = $em;
}
public function addLog($type,$title,$text=''): Response
{
$now = new \DateTime(date('Y-m-d H:i:s'));
$newlog = new Log();
if ($this->user) $newlog->setUser($this->user);
$newlog->setLogtime($now);
$newlog->setType($type);
$newlog->setTitle($title);
$newlog->setText($text);
$newlog->setIpAddress($_SERVER['REMOTE_ADDR']);
$this->em->persist($newlog);
$this->em->flush();
return new JsonResponse(array('status' => 'success'));
}
}