src/Entity/Log.php line 284

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Controller\AppBundle\Helpers\Admin\ConsultantHelper;
  5. use Psr\Container\ContainerInterface;
  6. /**
  7.  * log
  8.  */
  9. #[ORM\Table(name'log')]
  10. #[ORM\Entity(repositoryClass'App\Repository\logRepository')]
  11. class Log
  12. {
  13.     /**
  14.      * @var int
  15.      */
  16.     #[ORM\Column(name'id'type'integer')]
  17.     #[ORM\Id]
  18.     #[ORM\GeneratedValue(strategy'AUTO')]
  19.     private $id;
  20.     /**
  21.      * @var string
  22.      */
  23.     #[ORM\ManyToOne(targetEntity'User')]
  24.     #[ORM\JoinColumn(name'userID'referencedColumnName'id'nullabletrue)]
  25.     private $user;
  26.     /**
  27.      * @var \DateTime
  28.      */
  29.     #[ORM\Column(name'timestamp'type'datetime')]
  30.     private $timestamp;
  31.     /**
  32.      * @var string
  33.      */
  34.     #[ORM\Column(name'ipAddress'type'string'length255)]
  35.     private $ipAddress;
  36.     /**
  37.      * @var string
  38.      */
  39.     #[ORM\Column(name'sessionKey'type'string'length255)]
  40.     private $sessionKey;
  41.     /**
  42.      * @var string
  43.      */
  44.     #[ORM\Column(name'page'type'string'length255nullabletrue)]
  45.     private $page;
  46.     /**
  47.      * @var string
  48.      */
  49.     #[ORM\Column(name'action'type'string'length255)]
  50.     private $action;
  51.     /**
  52.      * @var string
  53.      */
  54.     #[ORM\Column(name'details'type'array'nullabletrue)]
  55.     private $details;
  56.     /**
  57.      * Get id
  58.      *
  59.      * @return int
  60.      */
  61.     public function getId()
  62.     {
  63.         return $this->id;
  64.     }
  65.     /**
  66.      * Set user
  67.      *
  68.      * @param string $user
  69.      *
  70.      * @return Log
  71.      */
  72.     public function setUser($user)
  73.     {
  74.         $this->user $user;
  75.         return $this;
  76.     }
  77.     /**
  78.      * Get user
  79.      *
  80.      * @return string
  81.      */
  82.     public function getUser()
  83.     {
  84.         return $this->user;
  85.     }
  86.     /**
  87.      * Set timestamp
  88.      *
  89.      * @param \DateTime $timestamp
  90.      *
  91.      * @return Log
  92.      */
  93.     public function setTimestamp($timestamp)
  94.     {
  95.         $this->timestamp $timestamp;
  96.         return $this;
  97.     }
  98.     /**
  99.      * Get timestamp
  100.      *
  101.      * @return \DateTime
  102.      */
  103.     public function getTimestamp()
  104.     {
  105.         return $this->timestamp;
  106.     }
  107.     /**
  108.      * Set ipAddress
  109.      *
  110.      * @param string $ipAddress
  111.      *
  112.      * @return Log
  113.      */
  114.     public function setIpAddress($ipAddress)
  115.     {
  116.         $this->ipAddress $ipAddress;
  117.         return $this;
  118.     }
  119.     /**
  120.      * Get ipAddress
  121.      *
  122.      * @return string
  123.      */
  124.     public function getIpAddress()
  125.     {
  126.         return $this->ipAddress;
  127.     }
  128.     /**
  129.      * Set sessionKey
  130.      *
  131.      * @param string $sessionKey
  132.      *
  133.      * @return Log
  134.      */
  135.     public function setSessionKey($sessionKey)
  136.     {
  137.         $this->sessionKey $sessionKey;
  138.         return $this;
  139.     }
  140.     /**
  141.      * Get sessionKey
  142.      *
  143.      * @return string
  144.      */
  145.     public function getSessionKey()
  146.     {
  147.         return $this->sessionKey;
  148.     }
  149.     /**
  150.      * Set page
  151.      *
  152.      * @param string $page
  153.      *
  154.      * @return Log
  155.      */
  156.     public function setPage($page)
  157.     {
  158.         $this->page $page;
  159.         return $this;
  160.     }
  161.     /**
  162.      * Get page
  163.      *
  164.      * @return string
  165.      */
  166.     public function getPage()
  167.     {
  168.         return $this->page;
  169.     }
  170.     /**
  171.      * Set action
  172.      *
  173.      * @param string $action
  174.      *
  175.      * @return Log
  176.      */
  177.     public function setAction($action)
  178.     {
  179.         $this->action $action;
  180.         return $this;
  181.     }
  182.     /**
  183.      * Get action
  184.      *
  185.      * @return string
  186.      */
  187.     public function getAction()
  188.     {
  189.         return $this->action;
  190.     }
  191.     /**
  192.      * Set details
  193.      *
  194.      * @param array $details
  195.      *
  196.      * @return Log
  197.      */
  198.     public function setDetails($details)
  199.     {
  200.         $this->details $details;
  201.         return $this;
  202.     }
  203.     /**
  204.      * Get details
  205.      *
  206.      * @return string
  207.      */
  208.     public function getDetails()
  209.     {
  210.         return $this->details;
  211.     }
  212.     /**
  213.      * @param $container ContainerInterface Container from controller
  214.      * @param $request \Symfony\Component\HttpFoundation\Request Current request
  215.      * @param $action string Action to log
  216.      * @param $user User Current logged in user
  217.      * @param $page string Current page
  218.      * @param $details array Array of extra details
  219.      */
  220.     public static function log($em$request$action$user null$page null$details = [])
  221.     {
  222.         // Get session
  223.         $session $request->getSession();
  224.         // Fetch entity manager
  225. //        $em = $container->get('doctrine.orm.default_entity_manager');
  226.         $log = new Log();
  227.         if (ConsultantHelper::isLoggedInSession($request->getSession())) {
  228.             $details['consultant'] = ConsultantHelper::getLoggedInConsultant($request$em)->getUsername();
  229.             if ($user != null && $page !== 'admin') {
  230.                 $details['targetUser'] = $user->getAnvaKey();
  231.                 $user null;
  232.             }
  233.         } else {
  234.             if ($user != null) {
  235.                 $user $em->getRepository(User::class)->findOneById($user->getId());
  236.                 $log->setUser($user);
  237.             }
  238.         }
  239.         $log->setAction($action);
  240.         $log->setPage($page);
  241.         $log->setDetails($details);
  242.         $log->setTimestamp(new \DateTime());
  243.         $log->setIpAddress($request->getClientIp());
  244.         $log->setSessionKey('');
  245.         $em->persist($log);
  246.         try {
  247.             $em->flush();
  248.         } catch (\Exception $e) {
  249.             $e->getTrace();
  250.         }
  251.     }
  252. }