<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Controller\AppBundle\Helpers\Admin\ConsultantHelper;
use Psr\Container\ContainerInterface;
/**
* log
*/
#[ORM\Table(name: 'log')]
#[ORM\Entity(repositoryClass: 'App\Repository\logRepository')]
class Log
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var string
*/
#[ORM\ManyToOne(targetEntity: 'User')]
#[ORM\JoinColumn(name: 'userID', referencedColumnName: 'id', nullable: true)]
private $user;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'timestamp', type: 'datetime')]
private $timestamp;
/**
* @var string
*/
#[ORM\Column(name: 'ipAddress', type: 'string', length: 255)]
private $ipAddress;
/**
* @var string
*/
#[ORM\Column(name: 'sessionKey', type: 'string', length: 255)]
private $sessionKey;
/**
* @var string
*/
#[ORM\Column(name: 'page', type: 'string', length: 255, nullable: true)]
private $page;
/**
* @var string
*/
#[ORM\Column(name: 'action', type: 'string', length: 255)]
private $action;
/**
* @var string
*/
#[ORM\Column(name: 'details', type: 'array', nullable: true)]
private $details;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* @param string $user
*
* @return Log
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return string
*/
public function getUser()
{
return $this->user;
}
/**
* Set timestamp
*
* @param \DateTime $timestamp
*
* @return Log
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
return $this;
}
/**
* Get timestamp
*
* @return \DateTime
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Set ipAddress
*
* @param string $ipAddress
*
* @return Log
*/
public function setIpAddress($ipAddress)
{
$this->ipAddress = $ipAddress;
return $this;
}
/**
* Get ipAddress
*
* @return string
*/
public function getIpAddress()
{
return $this->ipAddress;
}
/**
* Set sessionKey
*
* @param string $sessionKey
*
* @return Log
*/
public function setSessionKey($sessionKey)
{
$this->sessionKey = $sessionKey;
return $this;
}
/**
* Get sessionKey
*
* @return string
*/
public function getSessionKey()
{
return $this->sessionKey;
}
/**
* Set page
*
* @param string $page
*
* @return Log
*/
public function setPage($page)
{
$this->page = $page;
return $this;
}
/**
* Get page
*
* @return string
*/
public function getPage()
{
return $this->page;
}
/**
* Set action
*
* @param string $action
*
* @return Log
*/
public function setAction($action)
{
$this->action = $action;
return $this;
}
/**
* Get action
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Set details
*
* @param array $details
*
* @return Log
*/
public function setDetails($details)
{
$this->details = $details;
return $this;
}
/**
* Get details
*
* @return string
*/
public function getDetails()
{
return $this->details;
}
/**
* @param $container ContainerInterface Container from controller
* @param $request \Symfony\Component\HttpFoundation\Request Current request
* @param $action string Action to log
* @param $user User Current logged in user
* @param $page string Current page
* @param $details array Array of extra details
*/
public static function log($em, $request, $action, $user = null, $page = null, $details = [])
{
// Get session
$session = $request->getSession();
// Fetch entity manager
// $em = $container->get('doctrine.orm.default_entity_manager');
$log = new Log();
if (ConsultantHelper::isLoggedInSession($request->getSession())) {
$details['consultant'] = ConsultantHelper::getLoggedInConsultant($request, $em)->getUsername();
if ($user != null && $page !== 'admin') {
$details['targetUser'] = $user->getAnvaKey();
$user = null;
}
} else {
if ($user != null) {
$user = $em->getRepository(User::class)->findOneById($user->getId());
$log->setUser($user);
}
}
$log->setAction($action);
$log->setPage($page);
$log->setDetails($details);
$log->setTimestamp(new \DateTime());
$log->setIpAddress($request->getClientIp());
$log->setSessionKey('');
$em->persist($log);
try {
$em->flush();
} catch (\Exception $e) {
$e->getTrace();
}
}
}