<?php
namespace EventListener;
use App\Entity\App;
use App\Entity\AppCustomer;
use App\Entity\Customer;
use App\Entity\CustomerGroup;
use App\Entity\CustomerGroupConfig;
use App\Entity\CustomerGroupConfigKey;
use App\Entity\CustomerGroupModul;
use App\Entity\CustomerGroupModule;
use App\Entity\CustomerGroupModuleCategory;
use App\Entity\History;
use App\Entity\User;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Security;
class AppAdminListener implements EventSubscriberInterface
{
/**
* @var Security
*/
private $security;
private $history = [];
public function __construct(Security $security)
{
$this->security = $security;
}
public function preUpdate(PreUpdateEventArgs $args){
$entity = $args->getObject();
// if this subscriber only applies to certain entity types,
// add some code to check the entity type as early as possible
if ($entity instanceof App) {
$fields = ['name', 'appId'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'App', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof AppCustomer) {
$fields = ['app', 'customer', 'customerGroup'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'AppCustomer', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof Customer) {
$fields = ['name'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'Customer', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof CustomerGroup) {
$fields = ['name', 'identifier', 'registerKey', 'databaseSuffix', 'isActive'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'CustomerGroup', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof CustomerGroupConfig) {
$fields = ['value', 'customerGroupId', 'CustomerGroupConfigKeyId'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'CustomerGroupConfig', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof CustomerGroupConfigKey) {
$fields = ['keyName', 'name'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'CustomerGroupConfigKey', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof CustomerGroupModul) {
$fields = ['customerGroupModuleCategoryId', 'customerGroupModuleId', 'orderby', 'customerGroupId'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'CustomerGroupModul', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof CustomerGroupModule) {
$fields = ['name', 'active', 'orderby', 'home'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'CustomerGroupModule', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof CustomerGroupModuleCategory) {
$fields = ['category', 'orderby'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
$this->writeHistory('updated', 'CustomerGroupModuleCategory', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
} elseif ($entity instanceof User) {
$fields = ['username', 'roles', 'password', 'email'];
foreach ($fields as $field) {
if ($args->hasChangedField($field)) {
if ($field === 'roles') {
$this->writeHistory('updated', 'User', $field, implode($args->getOldValue($field)), implode($args->getNewValue($field)), $args->getObjectManager());
} else {
$this->writeHistory('updated', 'User', $field, $args->getOldValue($field), $args->getNewValue($field), $args->getObjectManager());
}
}
}
}
}
public function preRemove(LifecycleEventArgs $args){
$entity = $args->getObject();
if ($entity instanceof App) {
$this->writeHistory('deleted', 'App', 'name', $entity->getName(), '', $args->getObjectManager());
} elseif ($entity instanceof AppCustomer) {
$this->writeHistory('deleted', 'AppCustomer', 'id', $entity->getId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'AppCustomer', 'app', $entity->getApp(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'AppCustomer', 'Customer', $entity->getCustomer(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'AppCustomer', 'CustomerGroup', $entity->getCustomerGroup(), '', $args->getObjectManager());
} elseif ($entity instanceof Customer) {
$this->writeHistory('deleted', 'Customer', 'name', $entity->getName(), '', $args->getObjectManager());
} elseif ($entity instanceof CustomerGroup) {
$this->writeHistory('deleted', 'CustomerGroup', 'name', $entity->getName(), '', $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupConfig) {
$this->writeHistory('deleted', 'CustomerGroupConfig', 'id', $entity->getId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'CustomerGroupConfig', 'CustomerGroup', $entity->getCustomerGroupId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'CustomerGroupConfig', 'CustomerGroupConfigKey', $entity->getCustomerGroupConfigKeyId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'CustomerGroupConfig', 'value', $entity->getValue(), '', $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupConfigKey) {
$this->writeHistory('deleted', 'CustomerGroupConfigKey', 'name', $entity->getName(), '', $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupModul) {
$this->writeHistory('deleted', 'CustomerGroupModul', 'id', $entity->getId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'CustomerGroupModul', 'CustomerGroupId', $entity->getCustomerGroupId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'CustomerGroupModul', 'CustomerGroupModulCategory', $entity->getCustomerGroupModuleCategoryId(), '', $args->getObjectManager());
$this->writeHistory('deleted', 'CustomerGroupModul', 'CustomerGroupModuleId', $entity->getCustomerGroupModuleId(), '', $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupModule) {
$this->writeHistory('deleted', 'CustomerGroupModule', 'name', $entity->getName(), '', $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupModuleCategory) {
$this->writeHistory('deleted', 'CustomerGroupModuleCategory', 'category', $entity->getCategory(), '', $args->getObjectManager());
} elseif ($entity instanceof User) {
$this->writeHistory('deleted', 'User', 'username', $entity->getUsername(), '', $args->getObjectManager());
}
}
public function postPersist(LifecycleEventArgs $args){
//Wenn keine preUpdate oder PreRemove Events vorliegen, wurde ein neuer Datensatz erstellt
if (empty($this->history)) {
$entity = $args->getObject();
if ($entity instanceof App) {
$this->writeHistory('created', 'App', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof AppCustomer) {
$this->writeHistory('created', 'AppCustomer', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof Customer) {
$this->writeHistory('created', 'Customer', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof CustomerGroup) {
$this->writeHistory('created', 'CustomerGroup', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupConfig) {
$this->writeHistory('created', 'CustomerGroupConfig', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupConfigKey) {
$this->writeHistory('created', 'CustomerGroupConfigKey', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupModul) {
$this->writeHistory('created', 'CustomerGroupModul', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupModule) {
$this->writeHistory('created', 'CustomerGroupModule', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof CustomerGroupModuleCategory) {
$this->writeHistory('created', 'CustomerGroupModuleCategory', 'id', '', $entity->getId(), $args->getObjectManager());
} elseif ($entity instanceof User) {
$this->writeHistory('created', 'User', 'id', '', $entity->getId(), $args->getObjectManager());
}
}
}
public function postFlush(PostFlushEventArgs $args)
{
if (!empty($this->history)) {
$em = $args->getObjectManager();
foreach ($this->history as $history) {
$em->persist($history);
}
$this->history = [];
$em->flush();
}
}
public function onKernelRequest(RequestEvent $event)
{
}
public static function getSubscribedEvents()
{
return [
RequestEvent::class => 'onKernelRequest'
];
}
public function writeHistory(string $action, string $entityName, string $field, ?string $oldValue, ?string $newValue, ObjectManager $objectManager): void
{
$history = new(History::class);
$history->setUserId($this->security->getUser());
$history->setEntity($entityName);
$history->setField($field);
$history->setOldValue($oldValue);
$history->setNewValue($newValue);
$dateTime = new \DateTime();
if ($action === 'updated') {
$history->setUpdated($dateTime);
} elseif ($action === 'deleted') {
$history->setDeleted($dateTime);
} elseif ($action === 'created'){
$history->setCreated($dateTime);
}
$this->history[] = $history;
}
}