src/Package/Admin/Tools/EventSubscriber/RegistrySubscriber/RegistrySubscriber.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\Package\Admin\Tools\EventSubscriber\RegistrySubscriber;
  3. use Doctrine\ORM\EntityManagerInterface,
  4.     Symfony\Component\HttpFoundation\RequestStack,
  5.     Symfony\Component\EventDispatcher\EventSubscriberInterface,
  6.     Symfony\Component\HttpKernel\KernelEvents,
  7.     Symfony\Component\HttpKernel\Event\ControllerEvent,
  8.     Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface,
  9.     Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use App\Package\Toolkit\ApplicationMode\ApplicationMode,
  11.     App\Package\Toolkit\ConfigurationBag\ConfigurationBag,
  12.     App\Package\Admin\Tools\AdminUtil\AdminUtil,
  13.     App\Package\Admin\Tools\AdminState\AdminState;
  14. use App\Package\Admin\Main\EntityInterface\{ AdminInterfaceAdminOnlineInterfaceRegistryInterface };
  15. use App\Package\Admin\Base\EntityInterface\AdminLoggableInterface,
  16.     App\Package\Admin\Main\RepositoryInterface\AdminOnlineRepositoryInterface;
  17. /**
  18.  * RegistrySubscriber
  19.  * 
  20.  * Persists information about parentless request (admin online and registry entities)
  21.  * 
  22.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  23.  * @copyright  Openform
  24.  * @since      03.2019
  25.  */
  26. class RegistrySubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var ApplicationMode
  30.      */
  31.     protected $applicationMode;
  32.     /**
  33.      * @var ConfigurationBag
  34.      */
  35.     protected $configBag;
  36.     /**
  37.      * @var AdminUtil
  38.      */
  39.     protected $adminUtil;
  40.     /**
  41.      * @var AdminState
  42.      */
  43.     protected $adminState;
  44.     /**
  45.      * @var EntityManagerInterface
  46.      */
  47.     protected $em;
  48.     /**
  49.      * @var AdminInterface
  50.      */
  51.     protected $admin;
  52.     /**
  53.      * @var RequestStack
  54.      */
  55.     protected $requestStack;
  56.     /**
  57.      * @param ApplicationMode         $applicationMode
  58.      * @param ConfigurationBag        $configBag
  59.      * @param AdminUtil               $adminUtil
  60.      * @param AdminState              $adminState
  61.      * @param EntityManagerInterface  $em
  62.      * @param TokenStorageInterface   $tokenStorage
  63.      * @param RequestStack            $requestStack
  64.      */
  65.     public function __construct(
  66.         ApplicationMode        $applicationMode,
  67.         ConfigurationBag       $configBag
  68.         AdminUtil              $adminUtil,
  69.         AdminState             $adminState,
  70.         EntityManagerInterface $em,
  71.         TokenStorageInterface  $tokenStorage
  72.         RequestStack           $requestStack
  73.     ) {
  74.         $this->applicationMode $applicationMode;
  75.         $this->configBag  $configBag;
  76.         $this->adminUtil  $adminUtil;
  77.         $this->adminState $adminState;
  78.         $this->em    $em;
  79.         $this->admin 
  80.             $tokenStorage->getToken() ?
  81.             $tokenStorage->getToken()->getUser() :
  82.             null;
  83.         $this->requestStack $requestStack;
  84.     }
  85.     /**
  86.      * @return array
  87.      */
  88.     public static function getSubscribedEvents() : array
  89.     {
  90.         return [
  91.             KernelEvents::CONTROLLER => [
  92.                 [ 'persistRequest'10 ],
  93.             ]
  94.         ];
  95.     }
  96.     /**
  97.      * Persists information about parentless request (admin online and registry entities)
  98.      * 
  99.      * @param ControllerEvent  $event
  100.      * 
  101.      * @return void
  102.      */
  103.     public function persistRequest(ControllerEvent $event) : void
  104.     {
  105.         $controller $event->getController();
  106.         if (
  107.             $this->requestStack->getParentRequest() ||
  108.             $this->applicationMode->getCurrentMode() !== 'admin' ||
  109.             ! $this->admin ||
  110.             gettype($this->admin) === 'string'
  111.         ) {
  112.             return;
  113.         }
  114.         if (! $this->adminState->getCurrentModule()) {
  115.             return;
  116.         }
  117.         $this->persistAdminOnline($this->admin)
  118.              ->persistRegistry($this->admin);
  119.         $this->admin->setLastSeen(new \DateTime());
  120.         $this->em->persist($this->admin);
  121.         $this->em->flush();
  122.         return;
  123.     }
  124.     /**
  125.      * Persists admin online entity
  126.      * 
  127.      * @param AdminInterface  $admin
  128.      * 
  129.      * @return RegistrySubscriber
  130.      */
  131.     protected function persistAdminOnline(AdminInterface $admin) : RegistrySubscriber
  132.     {
  133.         $adminOnline $this->getAdminOnlineEntity($admin);
  134.         $this->fillLoggableEntity(
  135.             $adminOnline,
  136.             $admin
  137.         );
  138.         
  139.         $adminOnline->setUpdatedAt(new \DateTime());
  140.         $this->em->persist($adminOnline);
  141.         return $this;
  142.     }
  143.     /**
  144.      * Gets admin online entity
  145.      * - creates new entity if it does not exist
  146.      *
  147.      * @param AdminInterface  $admin
  148.      * 
  149.      * @return AdminOnlineInterface
  150.      */
  151.     protected function getAdminOnlineEntity(AdminInterface $admin) : AdminOnlineInterface
  152.     {
  153.         $adminOnline $this->adminUtil->getAdminOnlineRepository()->findOneByAdminId($admin->getId());
  154.         if (! $adminOnline) {
  155.             $adminOnlineClass $this->configBag->get('admin:entity:admin_online');
  156.             $adminOnline      = new $adminOnlineClass();
  157.         }
  158.         return $adminOnline;
  159.     }
  160.     /**
  161.      * Persists registry entity
  162.      *
  163.      * @param AdminInterface $admin
  164.      * 
  165.      * @return RegistrySubscriber
  166.      */
  167.     protected function persistRegistry(AdminInterface $admin) : RegistrySubscriber
  168.     {
  169.         $registry $this->createRegistryEntity();
  170.         $this->fillLoggableEntity(
  171.             $registry,
  172.             $admin
  173.         );
  174.    
  175.         if (! empty($_SERVER'HTTP_CLIENT_IP' ])) {
  176.             $ip $_SERVER'HTTP_CLIENT_IP' ];
  177.         } elseif (! empty($_SERVER'HTTP_X_FORWARDED_FOR' ])) {
  178.             $ip $_SERVER'HTTP_X_FORWARDED_FOR' ];
  179.         } else {
  180.             $ip $_SERVER'REMOTE_ADDR' ];
  181.         }
  182.             
  183.         $registry->setIp($ip);
  184.             
  185.         $requestData "";
  186.         
  187.         if (isset($_GET)) {
  188.             $requestData .= "GET: " var_export($_GETtrue) . "\n\n";
  189.         } elseif (isset($_POST)) {
  190.             $requestData .= "POST: " var_export($_POSTtrue) . "\n\n";
  191.         }
  192.         if (isset($_SESSION)) {
  193.             $requestData .= "SESSION: " var_export($_SESSIONtrue) . "\n\n";
  194.         }
  195.         
  196.         $registry->setRequestData($requestData);
  197.         $this->em->persist($registry);
  198.         return $this;
  199.     }
  200.     /**
  201.      * Creates registry entity
  202.      * 
  203.      * @return RegistryInterface
  204.      */
  205.     protected function createRegistryEntity() : RegistryInterface
  206.     {
  207.         $registryClass $this->configBag->get('admin:entity:registry');
  208.         return new $registryClass();
  209.     }
  210.     /**
  211.      * Fills loggable entity
  212.      * 
  213.      * @param AdminLoggableInterface  $entity
  214.      * @param AdminInterface          $admin
  215.      * 
  216.      * @return void
  217.      */
  218.     protected function fillLoggableEntity(AdminLoggableInterface $entityAdminInterface $admin) : void 
  219.     {
  220.         $entity->setAdmin($admin);
  221.         $entity->setAdminLogin($admin->getLogin());
  222.         $entity->setAdminModule(
  223.             $this->adminState->getCurrentModule()
  224.         );
  225.         $entity->setAdminModuleName(
  226.             $this->adminState->getCurrentModule()->getName()
  227.         );
  228.         $entity->setAdminAction(
  229.             $this->adminState->getCurrentAction()
  230.         );
  231.         $entity->setAdminActionName(
  232.             $this->adminState->getCurrentAction()->getName()
  233.         );
  234.         $entity->setObjectId(
  235.             $this->adminState->getCurrentObjectId()
  236.         );
  237.         return;
  238.     }
  239. }