<commit_before><?php

namespace Study\AspirinaBundle\EventListener;

use Doctrine\ORM\Event\OnFlushEventArgs;

class AudittrailListener
{
    public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();
        
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            
            $properties = array();

            foreach ($this->getPropertiesNames($entity) as $propertyName) {
                $propertyValue = $this->getPropertyValue($entity, $propertyName);
                $properties[$propertyName] = $propertyValue;
            }

            die(var_dump($properties));
            $this->getEntityColumnValues($entity, $em);            
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            $entity->getId();
            $uow->getEntityChangeSet($entity);
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            $this->getEntityColumnValues($entity, $em);
        }
        
        foreach ($uow->getScheduledCollectionDeletions() as $col) {

        }

        foreach ($uow->getScheduledCollectionUpdates() as $col) {

        }
    }

    private function getPropertiesNames($entity)
    {
        $reflectClass = new \ReflectionClass($entity);
        $properties = $reflectClass->getProperties(\ReflectionProperty::IS_PRIVATE);

        $propertiesNames = array();

        foreach ($properties as $property) {
            $propertiesNames[] = $property->getName();
        }

        return $propertiesNames;
    }

    private function getPropertyValue($entity, $propertyName)
    {
        $getter = 'get'.ucfirst($propertyName);
        return $entity->$getter();
    }
    
    private function getEntityColumnValues($entity,$em)
    {
        $cols = $em->getClassMetadata(get_class($entity))->getColumnNames();
        $values = array();
        foreach($cols as $col){
          $getter = 'get'.ucfirst($col);
          $values[$col] = $entity->$getter();
        }
        return $values;
    }
}<commit_msg>Utilizando reflection nas entities na AuditTrailListrner.<commit_after><?php

namespace Study\AspirinaBundle\EventListener;

use Doctrine\ORM\Event\OnFlushEventArgs;

class AudittrailListener
{
    public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();
        
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            $properties = array();
            
            foreach ($this->getPropertiesNames($entity) as $propertyName) {
                if($this->hasAuditTrailInProperty($entity, $propertyName)){
                    $propertyValue = $this->getPropertyValue($entity, $propertyName);
                    $properties[$propertyName] = $propertyValue;
                }
            }

            die(var_dump($properties));
            
            
            $this->getEntityColumnValues($entity, $em);            
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            $entity->getId();
            $uow->getEntityChangeSet($entity);
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            $this->getEntityColumnValues($entity, $em);
        }
        
        foreach ($uow->getScheduledCollectionDeletions() as $col) {

        }

        foreach ($uow->getScheduledCollectionUpdates() as $col) {

        }
    }

    private function getPropertiesNames($entity)
    {
        $reflectClass = new \ReflectionClass($entity);
        $properties = $reflectClass->getProperties();
        
        $propertiesNames = array();

        foreach ($properties as $property) {
            $propertiesNames[] = $property->getName();
        }

        return $propertiesNames;
    }
    
    private function hasAuditTrailInProperty($entity, $propertyName)
    {
        $reflectClass = new \ReflectionClass($entity);        
        $reflectProperty = new \ReflectionProperty($reflectClass->getName(), $propertyName);
        
        if (strpos($reflectProperty->getDocComment(), '@Audittrail')) {
            return true;
        }
        return false;
    }

    private function getPropertyValue($entity, $propertyName)
    {
        $getter = 'get'.ucfirst($propertyName);
        return $entity->$getter();
    }
    
}
{'fraction_non_alphanumeric': 0.12684031710079274, 'fraction_numerical': 0.0, 'mean_word_length': 2.2422907488986783, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3954709', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1170, 'n_words': 199}