src/Service/LogService.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Log;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Security\Core\Security;
  8. class LogService
  9. {
  10.     private $user;
  11.     private $em;
  12.     public function __construct(Security $securityEntityManagerInterface $em) {
  13.         $this->user $security->getUser();
  14.         $this->em $em;
  15.     }
  16.     public function addLog($type,$title,$text=''): Response
  17.     {
  18.         $now = new \DateTime(date('Y-m-d H:i:s'));
  19.         $newlog = new Log();
  20.         if ($this->user$newlog->setUser($this->user);
  21.         $newlog->setLogtime($now);
  22.         $newlog->setType($type);
  23.         $newlog->setTitle($title);
  24.         $newlog->setText($text);
  25.         $newlog->setIpAddress($_SERVER['REMOTE_ADDR']);
  26.         $this->em->persist($newlog);
  27.         $this->em->flush();
  28.         return new JsonResponse(array('status' => 'success'));
  29.     }
  30. }