src/EventSubscriber/LogEditSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\GhEditLog;
  4. use App\Repository\GhEditLogRepository;
  5. use Doctrine\Common\Annotations\Reader;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Psr\Container\ContainerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Security;
  13. use Twig\Environment;
  14. class LogEditSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var EntityManagerInterface
  18.      */
  19.     private $entityManager;
  20.     /**
  21.      * @var Security
  22.      */
  23.     private $security;
  24.     /**
  25.      * @var AnnotationReader
  26.      */
  27.     private $annotationReader;
  28.     const LOGEDIT 'App\Annotations\LogEdit';
  29.     /**
  30.      * @var GhEditLogRepository
  31.      */
  32.     private $editLogRepository;
  33.     /**
  34.      * @var Environment
  35.      */
  36.     private $twig;
  37.     public function __construct(Security $security,EntityManagerInterface $entityManager,
  38.                                 Reader $annotationReader,GhEditLogRepository $editLogRepository,
  39.                                 Environment $twig)
  40.     {
  41.         $this->entityManager $entityManager;
  42.         $this->security $security;
  43.         $this->annotationReader $annotationReader;
  44.         $this->editLogRepository $editLogRepository;
  45.         $this->twig $twig;
  46.     }
  47. /*
  48.     public function onKernelController(ControllerEvent $event)
  49.     {
  50.     }
  51. */
  52.     public function onKernelControllerArguments(ControllerArgumentsEvent $event)
  53.     {
  54.         if (!$this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
  55.             return false;
  56.         }
  57.         $params $event->getRequest()->attributes->all();
  58.         $user $this->security->getUser();
  59.         $arrController $event->getController();
  60.         if (!is_array($arrController)) {
  61.             return;
  62.         }
  63.         //
  64.         $method = new \ReflectionMethod (get_class($arrController[0]), $arrController[1]);
  65.         $annotation $this->annotationReader->getMethodAnnotation($methodself::LOGEDIT);
  66.         if (!is_null($annotation) && key_exists('id'$params)) {
  67.             if ($editor=$this->editLogRepository->findOtherEditor($user->getId(),$annotation->entity,$params)) {
  68.                 $editor[0]->getStart()->setTimezone(new \DateTimeZone('Europe/Berlin'));
  69.                if ($time=$editor[0]->getStart()->format('d.m.y') == date('d.m.y')){
  70.                    $time=$editor[0]->getStart()->format('H:i:s (d.m)');
  71.                    $this->twig->addGlobal('isedited','Achtung: Wird seit '$time .' von ' $editor[0]->getUser() .' bearbeitet: <b>' $editor[0]->getController() .'</b>' );
  72.                }
  73.             }
  74.             if($annotation->log) {
  75.                 $editLog = new GhEditLog();
  76.                 $editLog->setStart(new \DateTime());
  77.                 $editLog->setUser($user);
  78.                 $editLog->setRoute($params['_route']);
  79.                 $editLog->setController($annotation->action);
  80.                 $editLog->setEditId($params['id']);
  81.                 $editLog->setEntity($annotation->entity);
  82.                 $this->entityManager->persist($editLog);
  83.                 $this->entityManager->flush();
  84.             }
  85.         }
  86.         if (is_null($annotation) || ($annotation->log ===false && $annotation->endlog))
  87.          {
  88.              $sql 'UPDATE gh_edit_log SET endtime = NOW() WHERE user_id = :user';
  89.              $this->entityManager->getConnection()->executeQuery($sql, ['user' => $user->getId()]);
  90.          }
  91.     }
  92.     /**
  93.      * @inheritDoc
  94.      */
  95.     public static function getSubscribedEvents()
  96.     {
  97.         return [
  98.            // KernelEvents::CONTROLLER => 'onKernelController',
  99.             KernelEvents::CONTROLLER_ARGUMENTS =>'onKernelControllerArguments'
  100.         ];
  101.     }
  102. }