<?php
namespace App\EventSubscriber;
use App\Entity\GhEditLog;
use App\Repository\GhEditLogRepository;
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class LogEditSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var Security
*/
private $security;
/**
* @var AnnotationReader
*/
private $annotationReader;
const LOGEDIT = 'App\Annotations\LogEdit';
/**
* @var GhEditLogRepository
*/
private $editLogRepository;
/**
* @var Environment
*/
private $twig;
public function __construct(Security $security,EntityManagerInterface $entityManager,
Reader $annotationReader,GhEditLogRepository $editLogRepository,
Environment $twig)
{
$this->entityManager = $entityManager;
$this->security = $security;
$this->annotationReader = $annotationReader;
$this->editLogRepository = $editLogRepository;
$this->twig = $twig;
}
/*
public function onKernelController(ControllerEvent $event)
{
}
*/
public function onKernelControllerArguments(ControllerArgumentsEvent $event)
{
if (!$this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
return false;
}
$params = $event->getRequest()->attributes->all();
$user = $this->security->getUser();
$arrController = $event->getController();
if (!is_array($arrController)) {
return;
}
//
$method = new \ReflectionMethod (get_class($arrController[0]), $arrController[1]);
$annotation = $this->annotationReader->getMethodAnnotation($method, self::LOGEDIT);
if (!is_null($annotation) && key_exists('id', $params)) {
if ($editor=$this->editLogRepository->findOtherEditor($user->getId(),$annotation->entity,$params)) {
$editor[0]->getStart()->setTimezone(new \DateTimeZone('Europe/Berlin'));
if ($time=$editor[0]->getStart()->format('d.m.y') == date('d.m.y')){
$time=$editor[0]->getStart()->format('H:i:s (d.m)');
$this->twig->addGlobal('isedited','Achtung: Wird seit '. $time .' von ' . $editor[0]->getUser() .' bearbeitet: <b>' . $editor[0]->getController() .'</b>' );
}
}
if($annotation->log) {
$editLog = new GhEditLog();
$editLog->setStart(new \DateTime());
$editLog->setUser($user);
$editLog->setRoute($params['_route']);
$editLog->setController($annotation->action);
$editLog->setEditId($params['id']);
$editLog->setEntity($annotation->entity);
$this->entityManager->persist($editLog);
$this->entityManager->flush();
}
}
if (is_null($annotation) || ($annotation->log ===false && $annotation->endlog))
{
$sql = 'UPDATE gh_edit_log SET endtime = NOW() WHERE user_id = :user';
$this->entityManager->getConnection()->executeQuery($sql, ['user' => $user->getId()]);
}
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
// KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::CONTROLLER_ARGUMENTS =>'onKernelControllerArguments'
];
}
}