src/EventListener/AppAdminListener.php line 194

Open in your IDE?
  1. <?php
  2. namespace EventListener;
  3. use App\Entity\App;
  4. use App\Entity\AppCustomer;
  5. use App\Entity\Customer;
  6. use App\Entity\CustomerGroup;
  7. use App\Entity\CustomerGroupConfig;
  8. use App\Entity\CustomerGroupConfigKey;
  9. use App\Entity\CustomerGroupModul;
  10. use App\Entity\CustomerGroupModule;
  11. use App\Entity\CustomerGroupModuleCategory;
  12. use App\Entity\History;
  13. use App\Entity\User;
  14. use Doctrine\ORM\Event\LifecycleEventArgs;
  15. use Doctrine\ORM\Event\PostFlushEventArgs;
  16. use Doctrine\ORM\Event\PreUpdateEventArgs;
  17. use Doctrine\Persistence\ObjectManager;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\Security\Core\Security;
  21. class AppAdminListener implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var Security
  25.      */
  26.     private $security;
  27.     private $history = [];
  28.     public function __construct(Security $security)
  29.     {
  30.         $this->security $security;
  31.     }
  32.     public function preUpdate(PreUpdateEventArgs $args){
  33.         $entity =  $args->getObject();
  34.         // if this subscriber only applies to certain entity types,
  35.         // add some code to check the entity type as early as possible
  36.         if ($entity instanceof App) {
  37.             $fields = ['name''appId'];
  38.             foreach ($fields as $field) {
  39.                 if ($args->hasChangedField($field)) {
  40.                     $this->writeHistory('updated''App'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  41.                 }
  42.             }
  43.         } elseif ($entity instanceof AppCustomer) {
  44.             $fields = ['app''customer''customerGroup'];
  45.             foreach ($fields as $field) {
  46.                 if ($args->hasChangedField($field)) {
  47.                     $this->writeHistory('updated''AppCustomer'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  48.                 }
  49.             }
  50.         } elseif ($entity instanceof Customer) {
  51.             $fields = ['name'];
  52.             foreach ($fields as $field) {
  53.                 if ($args->hasChangedField($field)) {
  54.                     $this->writeHistory('updated''Customer'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  55.                 }
  56.             }
  57.         } elseif ($entity instanceof CustomerGroup) {
  58.             $fields = ['name''identifier''registerKey''databaseSuffix''isActive'];
  59.             foreach ($fields as $field) {
  60.                 if ($args->hasChangedField($field)) {
  61.                     $this->writeHistory('updated''CustomerGroup'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  62.                 }
  63.             }
  64.         } elseif ($entity instanceof CustomerGroupConfig) {
  65.             $fields = ['value''customerGroupId''CustomerGroupConfigKeyId'];
  66.             foreach ($fields as $field) {
  67.                 if ($args->hasChangedField($field)) {
  68.                     $this->writeHistory('updated''CustomerGroupConfig'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  69.                 }
  70.             }
  71.         } elseif ($entity instanceof CustomerGroupConfigKey) {
  72.             $fields = ['keyName''name'];
  73.             foreach ($fields as $field) {
  74.                 if ($args->hasChangedField($field)) {
  75.                     $this->writeHistory('updated''CustomerGroupConfigKey'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  76.                 }
  77.             }
  78.         } elseif ($entity instanceof CustomerGroupModul) {
  79.             $fields = ['customerGroupModuleCategoryId''customerGroupModuleId''orderby''customerGroupId'];
  80.             foreach ($fields as $field) {
  81.                 if ($args->hasChangedField($field)) {
  82.                     $this->writeHistory('updated''CustomerGroupModul'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  83.                 }
  84.             }
  85.         } elseif ($entity instanceof CustomerGroupModule) {
  86.             $fields = ['name''active''orderby''home'];
  87.             foreach ($fields as $field) {
  88.                 if ($args->hasChangedField($field)) {
  89.                     $this->writeHistory('updated''CustomerGroupModule'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  90.                 }
  91.             }
  92.         } elseif ($entity instanceof CustomerGroupModuleCategory) {
  93.             $fields = ['category''orderby'];
  94.             foreach ($fields as $field) {
  95.                 if ($args->hasChangedField($field)) {
  96.                     $this->writeHistory('updated''CustomerGroupModuleCategory'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  97.                 }
  98.             }
  99.         } elseif ($entity instanceof User) {
  100.             $fields = ['username''roles''password''email'];
  101.             foreach ($fields as $field) {
  102.                 if ($args->hasChangedField($field)) {
  103.                     if ($field === 'roles') {
  104.                         $this->writeHistory('updated''User'$fieldimplode($args->getOldValue($field)), implode($args->getNewValue($field)), $args->getObjectManager());
  105.                     } else {
  106.                         $this->writeHistory('updated''User'$field$args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112.     public function preRemove(LifecycleEventArgs $args){
  113.         $entity =  $args->getObject();
  114.         if ($entity instanceof App) {
  115.             $this->writeHistory('deleted''App''name'$entity->getName(), ''$args->getObjectManager());
  116.         } elseif ($entity instanceof AppCustomer) {
  117.             $this->writeHistory('deleted''AppCustomer''id'$entity->getId(), ''$args->getObjectManager());
  118.             $this->writeHistory('deleted''AppCustomer''app'$entity->getApp(), ''$args->getObjectManager());
  119.             $this->writeHistory('deleted''AppCustomer''Customer'$entity->getCustomer(), ''$args->getObjectManager());
  120.             $this->writeHistory('deleted''AppCustomer''CustomerGroup'$entity->getCustomerGroup(), ''$args->getObjectManager());
  121.         } elseif ($entity instanceof Customer) {
  122.             $this->writeHistory('deleted''Customer''name'$entity->getName(), ''$args->getObjectManager());
  123.         } elseif ($entity instanceof CustomerGroup) {
  124.             $this->writeHistory('deleted''CustomerGroup''name'$entity->getName(), ''$args->getObjectManager());
  125.         } elseif ($entity instanceof CustomerGroupConfig) {
  126.             $this->writeHistory('deleted''CustomerGroupConfig''id'$entity->getId(), ''$args->getObjectManager());
  127.             $this->writeHistory('deleted''CustomerGroupConfig''CustomerGroup'$entity->getCustomerGroupId(), ''$args->getObjectManager());
  128.             $this->writeHistory('deleted''CustomerGroupConfig''CustomerGroupConfigKey'$entity->getCustomerGroupConfigKeyId(), ''$args->getObjectManager());
  129.             $this->writeHistory('deleted''CustomerGroupConfig''value'$entity->getValue(), ''$args->getObjectManager());
  130.         } elseif ($entity instanceof CustomerGroupConfigKey) {
  131.             $this->writeHistory('deleted''CustomerGroupConfigKey''name'$entity->getName(), ''$args->getObjectManager());
  132.         } elseif ($entity instanceof CustomerGroupModul) {
  133.             $this->writeHistory('deleted''CustomerGroupModul''id'$entity->getId(), ''$args->getObjectManager());
  134.             $this->writeHistory('deleted''CustomerGroupModul''CustomerGroupId'$entity->getCustomerGroupId(), ''$args->getObjectManager());
  135.             $this->writeHistory('deleted''CustomerGroupModul''CustomerGroupModulCategory'$entity->getCustomerGroupModuleCategoryId(), ''$args->getObjectManager());
  136.             $this->writeHistory('deleted''CustomerGroupModul''CustomerGroupModuleId'$entity->getCustomerGroupModuleId(), ''$args->getObjectManager());
  137.         } elseif ($entity instanceof CustomerGroupModule) {
  138.             $this->writeHistory('deleted''CustomerGroupModule''name'$entity->getName(), ''$args->getObjectManager());
  139.         } elseif ($entity instanceof CustomerGroupModuleCategory) {
  140.             $this->writeHistory('deleted''CustomerGroupModuleCategory''category'$entity->getCategory(), ''$args->getObjectManager());
  141.         } elseif ($entity instanceof User) {
  142.             $this->writeHistory('deleted''User''username'$entity->getUsername(), ''$args->getObjectManager());
  143.         }
  144.     }
  145.     public function postPersist(LifecycleEventArgs $args){
  146.         //Wenn keine preUpdate oder PreRemove Events vorliegen, wurde ein neuer Datensatz erstellt
  147.         if (empty($this->history)) {
  148.             $entity $args->getObject();
  149.             if ($entity instanceof App) {
  150.                 $this->writeHistory('created''App''id'''$entity->getId(), $args->getObjectManager());
  151.             } elseif ($entity instanceof AppCustomer) {
  152.                 $this->writeHistory('created''AppCustomer''id'''$entity->getId(), $args->getObjectManager());
  153.             } elseif ($entity instanceof Customer) {
  154.                 $this->writeHistory('created''Customer''id'''$entity->getId(), $args->getObjectManager());
  155.             } elseif ($entity instanceof CustomerGroup) {
  156.                 $this->writeHistory('created''CustomerGroup''id'''$entity->getId(), $args->getObjectManager());
  157.             } elseif ($entity instanceof CustomerGroupConfig) {
  158.                 $this->writeHistory('created''CustomerGroupConfig''id'''$entity->getId(), $args->getObjectManager());
  159.             } elseif ($entity instanceof CustomerGroupConfigKey) {
  160.                 $this->writeHistory('created''CustomerGroupConfigKey''id'''$entity->getId(), $args->getObjectManager());
  161.             } elseif ($entity instanceof CustomerGroupModul) {
  162.                 $this->writeHistory('created''CustomerGroupModul''id'''$entity->getId(), $args->getObjectManager());
  163.             } elseif ($entity instanceof CustomerGroupModule) {
  164.                 $this->writeHistory('created''CustomerGroupModule''id'''$entity->getId(), $args->getObjectManager());
  165.             } elseif ($entity instanceof CustomerGroupModuleCategory) {
  166.                 $this->writeHistory('created''CustomerGroupModuleCategory''id'''$entity->getId(), $args->getObjectManager());
  167.             } elseif ($entity instanceof User) {
  168.                 $this->writeHistory('created''User''id'''$entity->getId(), $args->getObjectManager());
  169.             }
  170.         }
  171.     }
  172.     public function postFlush(PostFlushEventArgs $args)
  173.     {
  174.         if (!empty($this->history)) {
  175.             $em $args->getObjectManager();
  176.             foreach ($this->history as $history) {
  177.                 $em->persist($history);
  178.             }
  179.             $this->history = [];
  180.             $em->flush();
  181.         }
  182.     }
  183.     public function onKernelRequest(RequestEvent $event)
  184.     {
  185.     }
  186.     public static function getSubscribedEvents()
  187.     {
  188.         return [
  189.             RequestEvent::class => 'onKernelRequest'
  190.         ];
  191.     }
  192.     public function writeHistory(string $actionstring $entityNamestring $field, ?string $oldValue, ?string $newValueObjectManager $objectManager): void
  193.     {
  194.         $history = new(History::class);
  195.         $history->setUserId($this->security->getUser());
  196.         $history->setEntity($entityName);
  197.         $history->setField($field);
  198.         $history->setOldValue($oldValue);
  199.         $history->setNewValue($newValue);
  200.         $dateTime = new \DateTime();
  201.         if ($action === 'updated') {
  202.             $history->setUpdated($dateTime);
  203.         } elseif ($action === 'deleted') {
  204.             $history->setDeleted($dateTime);
  205.         } elseif ($action === 'created'){
  206.             $history->setCreated($dateTime);
  207.         }
  208.         $this->history[] = $history;
  209.     }
  210. }