<commit_before><?php

/*
 * This file is part of the XiideaEasyAuditBundle package.
 *
 * (c) Xiidea <http://www.xiidea.net>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Xiidea\EasyAuditBundle\Resolver;

use Symfony\Component\DependencyInjection\ContainerAware;
use Xiidea\EasyAuditBundle\Events\DoctrineEntityEvent;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
use Symfony\Component\EventDispatcher\Event;

/** Custom Event Resolver Example Class */
class EntityEventResolver extends ContainerAware implements EventResolverInterface
{
    protected $candidateProperties = array('name', 'title');

    protected $propertiesFound = array();

    protected $eventShortName;

    /** @var  $event DoctrineEntityEvent */
    protected $event;

    protected $entity;

    /**
     * @param Event|DoctrineEntityEvent $event
     *
     * @internal param $Event
     *
     * @return array
     */
    public function getEventLogInfo(Event $event)
    {
        $this->setEvent($event);

        $entity = $this->getEntity();

        $changesMetaData = $this->getChangeSets($entity);

        if($this->isUpdateEvent() && $changesMetaData === null) {
            return null;
        }

        $reflectionClass = $this->getReflectionClassFromObject($entity);

        $typeName = $reflectionClass->getShortName();
        $eventType = $this->getEventType($typeName);
        $eventDescription = $this->getDescription($reflectionClass, $typeName);

        return array(
            'description'=> $eventDescription,
            'type'=> $eventType,
        );

    }

    protected function setEvent(DoctrineEntityEvent $event)
    {
        $this->event = $event;
    }

    protected function getChangeSets($entity)
    {
        if($this->isUpdateEvent()) {
            return $this->getUnitOfWork()->getEntityChangeSet($entity);
        }
    }

    protected function isUpdateEvent() {
        return $this->getEventShortName() == 'updated';
    }

    /**
     * @return mixed
     */
    protected function getEntity()
    {
        if (!$this->entity) {
            $this->entity = $this->event->getLifecycleEventArgs()->getEntity();
        }

        return $this->entity;
    }

    protected function getProperty($name)
    {
        $entity = $this->getEntity();
        $propertyGetter = 'get'.$this->propertiesFound[$name];

        return $entity->$propertyGetter();
    }

    protected function getEventType($typeName)
    {
        return $typeName . " " . $this->getEventShortName();
    }

    protected function getDescription(\ReflectionClass $reflectionClass, $typeName)
    {
        $property = $this->getBestCandidatePropertyForIdentify($reflectionClass);

        $descriptionTemplate = '%s has been %s';

        if($property) {
            $descriptionTemplate .= sprintf(' with %s = "%s"', $property,  $this->getProperty($property));
        }

        return sprintf($descriptionTemplate,
            $typeName,
            $this->getEventShortName());
    }

    protected function getEventShortName(){

        if(!$this->eventShortName){
            $this->eventShortName = DoctrineEvents::getShortEventType($this->getName());
        }

        return $this->eventShortName;
    }

    protected function getBestCandidatePropertyForIdentify(\ReflectionClass  $reflectionClass)
    {
        $properties = $reflectionClass->getProperties(\ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE);

        $hasIdProperty = false;
        $idPropertyName = null;

        foreach($properties as $property)
        {
            $propertyName = strtolower($property->name);
            $hasIdProperty = $hasIdProperty || $this->isId($propertyName, $reflectionClass);

            if(!$idPropertyName && $hasIdProperty){
                $idPropertyName = $property->name;
                $this->propertiesFound['id'] = $idPropertyName;
            }

            foreach($this->candidateProperties as $candidate){
                $hasNameProperty = $this->isPropertyName($propertyName, $candidate);

                if($hasNameProperty){
                    $this->propertiesFound[$candidate] = $property->name;

                    return $property->name;
                }
            }
        }

        return $idPropertyName;
    }

    protected function isPropertyName($property, $name)
    {
        return $property == $name;
    }

    protected function isId($property, \ReflectionClass  $reflectionClass)
    {
        $entityIdStr = strtolower($reflectionClass->getShortName()) . "id";
<commit_msg>only handle if event is instance of DoctrineEntityEvent<commit_after><?php

/*
 * This file is part of the XiideaEasyAuditBundle package.
 *
 * (c) Xiidea <http://www.xiidea.net>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Xiidea\EasyAuditBundle\Resolver;

use Symfony\Component\DependencyInjection\ContainerAware;
use Xiidea\EasyAuditBundle\Events\DoctrineEntityEvent;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
use Symfony\Component\EventDispatcher\Event;

/** Custom Event Resolver Example Class */
class EntityEventResolver extends ContainerAware implements EventResolverInterface
{
    protected $candidateProperties = array('name', 'title');

    protected $propertiesFound = array();

    protected $eventShortName;

    /** @var  $event DoctrineEntityEvent */
    protected $event;

    protected $entity;

    /**
     * @param Event|DoctrineEntityEvent $event
     *
     * @internal param $Event
     *
     * @return array
     */
    public function getEventLogInfo(Event $event)
    {
        if (!$event instanceof DoctrineEntityEvent) {
            return null;
        }

        $this->event = $event;

        $entity = $this->getEntity();

        $changesMetaData = $this->getChangeSets($entity);

        if ($this->isUpdateEvent() && $changesMetaData === null) {
            return null;
        }

        $reflectionClass = $this->getReflectionClassFromObject($entity);

        $typeName = $reflectionClass->getShortName();
        $eventType = $this->getEventType($typeName);
        $eventDescription = $this->getDescription($reflectionClass, $typeName);

        return array(
            'description' => $eventDescription,
            'type' => $eventType,
        );

    }

    protected function getChangeSets($entity)
    {
        if ($this->isUpdateEvent()) {
            return $this->getUnitOfWork()->getEntityChangeSet($entity);
        }
    }

    protected function isUpdateEvent()
    {
        return $this->getEventShortName() == 'updated';
    }

    /**
     * @return mixed
     */
    protected function getEntity()
    {
        if (!$this->entity) {
            $this->entity = $this->event->getLifecycleEventArgs()->getEntity();
        }

        return $this->entity;
    }

    protected function getProperty($name)
    {
        $entity = $this->getEntity();
        $propertyGetter = 'get' . $this->propertiesFound[$name];

        return $entity->$propertyGetter();
    }

    protected function getEventType($typeName)
    {
        return $typeName . " " . $this->getEventShortName();
    }

    protected function getDescription(\ReflectionClass $reflectionClass, $typeName)
    {
        $property = $this->getBestCandidatePropertyForIdentify($reflectionClass);

        $descriptionTemplate = '%s has been %s';

        if ($property) {
            $descriptionTemplate .= sprintf(' with %s = "%s"', $property, $this->getProperty($property));
        }

        return sprintf(
            $descriptionTemplate,
            $typeName,
            $this->getEventShortName()
        );
    }

    protected function getEventShortName()
    {

        if (!$this->eventShortName) {
            $this->eventShortName = DoctrineEvents::getShortEventType($this->getName());
        }

        return $this->eventShortName;
    }

    protected function getBestCandidatePropertyForIdentify(\ReflectionClass $reflectionClass)
    {
        $properties = $reflectionClass->getProperties(
            \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE
        );

        $hasIdProperty = false;
        $idPropertyName = null;

        foreach ($properties as $property) {
            $propertyName = strtolower($property->name);
            $hasIdProperty = $hasIdProperty || $this->isId($propertyName, $reflectionClass);

            if (!$idPropertyName && $hasIdProperty) {
                $idPropertyName = $property->name;
                $this->propertiesFound['id'] = $idPropertyName;
            }

            foreach ($this->candidateProperties as $candidate) {
                $hasNameProperty = $this->isPropertyName($propertyName, $candidate);

                if ($hasNameProperty) {
                    $this->propertiesFound[$candidate] = $property->name;

                    return $property->name;
                }
            }
        }

        return $idPropertyName;
    }

    protected function isPropertyName($property, $name)
    {
        return $property == $name;
    }

    protected function isId($property, \ReflectionClass $reflectionClass)
    {
        $entityIdStr = strtolower($reflectionClass->getShortName()) . "id";

{'fraction_non_alphanumeric': 0.12222459436379163, 'fraction_numerical': 0.0, 'mean_word_length': 2.546177138531416, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2113687', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2465, 'n_words': 491}