Git Product home page Git Product logo

Comments (7)

braianj avatar braianj commented on June 3, 2024 1

Hi @ronisaha i'm still working with v1.4.7 but I can share with you my entity resolver in case it help you

<?php
namespace LoginBundle\Resolver;
use Symfony\Component\EventDispatcher\Event;
use Xiidea\EasyAuditBundle\Events\DoctrineEntityEvent;
use Xiidea\EasyAuditBundle\Resolver\EntityEventResolver as BaseResolver;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Psr\Log\LoggerInterface;

class EntityEventResolver extends BaseResolver
{

    private $logger;
    private $serializer;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @param Event|DoctrineEntityEvent $event
     * @param $eventName
     *
     * @return array
     */
    public function getEventLogInfo(Event $event, $eventName)
    {
        if (!$event instanceof DoctrineEntityEvent) {
            return null;
        }
        $this->initialize($event, $eventName);
        $changesMetaData = $this->getChangeSets($this->entity);

        if ($this->isUpdateEvent() && $changesMetaData == NULL) {
            return NULL;
        }
        $reflectionClass = $this->getReflectionClassFromObject($this->entity);
        $typeName = $reflectionClass->getShortName();
        //$eventType = $this->getEventType($typeName);
        $eventDescription = $this->getDescriptionString($reflectionClass, $typeName);

        $data = [];
        $ignorar = array('tu', 'ts', 'updated', 'created', 'transitions', '__initializer__', '__cloner__', '__isInitialized__', 'timezone', 'salt', 'password', 'newPassword');


        if (empty($changesMetaData)) {
            $encoder = new JsonEncoder();
            $normalizer = new ObjectNormalizer();

            $this->serializer = new Serializer(array($normalizer), array($encoder));

            $reflClass = new \ReflectionClass($this->entity);

            $arrayDatos = [];
            foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
                if (
                    0 !== $reflMethod->getNumberOfRequiredParameters() ||
                    $reflMethod->isStatic() ||
                    $reflMethod->isConstructor() ||
                    $reflMethod->isDestructor()
                ) {
                    continue;
                }

                $name = $reflMethod->name;
                $attributeName = null;

                if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
                    // getters and hassers
                    $attributeName = substr($name, 3);

                    if (!$reflClass->hasProperty($attributeName)) {
                        $attributeName = lcfirst($attributeName);
                    }
                } elseif (0 === strpos($name, 'is')) {
                    // issers
                    $attributeName = substr($name, 2);

                    if (!$reflClass->hasProperty($attributeName)) {
                        $attributeName = lcfirst($attributeName);
                    }
                }

                if (!is_null($attributeName) && !in_array($attributeName, $ignorar))
                {
                    $val = $this->entity->$name();

                    $es_colecion = (is_array($val) || ($val instanceof \ArrayAccess && $val instanceof \Traversable));

                    if(!$es_colecion) {
                        $arrayDatos[$attributeName] = $this->extraerValor($val);
                    } else {
                        $arrayDatos[$attributeName] = [];
                        foreach ($val as $item) {
                            $arrayDatos[$attributeName][] = $this->extraerValor($item);
                        }
                    }
                }
            }

            $data = $this->serializer->serialize($arrayDatos,'json');

        } elseif (is_string($changesMetaData)) {
            $data = $changesMetaData;
        } else {
            foreach ($changesMetaData as $key => $value) {
                if($value[0]!=$value[1] && !in_array($key, $ignorar)) {
                    if(gettype($value[0])== 'object') {

                        $entity = null;
                        foreach (explode('\\',get_class($value[0])) as $v) {
                            $entity = $v;
                        }

                        if((method_exists($value[0], 'getId'))) {

                            $value1 = $value[0]->getId();
                            $value2 = is_null($value[1]) ? null : $value[1]->getId();
                            $data[$key] = array('entity' => $entity, 'id' => array($value1, $value2));

                        } else {
                            $value1 = $this->isDateOrNot($value[0]);
                            $value2 = $this->isDateOrNot($value[1]);
                            $data[$key] = array($value1, $value2);
                        }
                    } else {
                        $data[$key] = $value;
                    }
                }
            }
            if (!empty($data)) {
                $data = json_encode($data);
            }
        }

        $infoFinal = array(
            'table'         => $typeName,
            'type'          => $this->translateToSpanishType(),
            'description'   => $eventDescription,
        );

        if ($typeName!='Agentes') {
            $infoFinal['tableId'] = $this->getProperty('id');
        }

        if (!empty($data)) {
            $infoFinal['data'] = $data;
        }
        return $infoFinal;
    }

    protected function extraerValor ($value) {
        $es_entidad = method_exists($value, 'getId');

        if($es_entidad) {
            return $value->getId();
        } elseif ($value instanceof \Date) {
            return $value->format("Y-m-d");
        } elseif ($value instanceof \DateTime) {
            return $value->format("Y-m-d H:i:s");
        } elseif (gettype($value)== 'object') {
            $value = json_encode($value, JSON_PRETTY_PRINT|JSON_ERROR_UNSUPPORTED_TYPE|JSON_PARTIAL_OUTPUT_ON_ERROR);
        }
        return $value;
    }

    /**
     * @param DoctrineEntityEvent $event
     * @param string $eventName
     */
    private function initialize(DoctrineEntityEvent $event, $eventName)
    {
        $this->eventShortName = null;
        $this->propertiesFound = array();
        $this->eventName = $eventName;
        $this->event = $event;
        $this->entity = $event->getLifecycleEventArgs()->getEntity();
    }

    protected function isDateOrNot($var) {
        if(is_a($var, 'DateTime')){
            return print_r(($var->format('H:i:s')!='00:00:00') ? $var->format('c') : $var->format('Y-m-d'), true);
        } else {
            return print_r($var, true);
        }
    }

    protected function getDescriptionString(\ReflectionClass $reflectionClass, $typeName)
    {
        $property = $this->getBestCandidatePropertyForIdentify($reflectionClass);
        $descriptionTemplate = '%s %s';//'%s ha sido %s ';
        if ($property) {
            $descriptionTemplate .= sprintf('.%s = %s', $property, $this->getProperty($property));
        }


        return sprintf($descriptionTemplate,
            $this->translateToSpanishType(),
            $typeName);
    }
    /**
     * @param $changesMetaData
     *
     * @return null|string
     */
    protected function getAsSerializedString($changesMetaData)
    {
        if (empty($changesMetaData)) {
            return NULL;
        } elseif (is_string($changesMetaData)) {
            return $changesMetaData;
        }
        return serialize($changesMetaData);
    }

    protected function translateToSpanishType() {
        $accion = null;
        switch ($this->getEventShortName()) {
            case 'created':
                $accion = 'Crear';
                break;

            case 'updated':
                $accion = 'Actualizar';
                break;

            case 'deleted':
                $accion = 'Eliminar';
                break;

            default:
                $accion = 'Acción';
                break;
        }
        return $accion;
    }
}

from easyauditbundle.

wh1pp3rz avatar wh1pp3rz commented on June 3, 2024 1

@ronisaha I can confirm the latest branch does work for both ODM and ORM

from easyauditbundle.

ronisaha avatar ronisaha commented on June 3, 2024 1

Yes you are right, I was waiting for some time to get a feedback from users, as I'm not using ODM in any of my projects.
Also I was waiting for SF4.3 release to test it. But could not make time for it. Hopefully I'll release a new tag with SF4.3 support checking soon.

from easyauditbundle.

ronisaha avatar ronisaha commented on June 3, 2024

Planning to rename following config values:

Old value New value
entity_class audit_log_class
entity_event_resolver doctrine_event_resolver
doctrine_entities doctrine_objects

Any suggestion/thought from any one? @wh1pp3rz, @braianj

Thanks.

from easyauditbundle.

wh1pp3rz avatar wh1pp3rz commented on June 3, 2024

from easyauditbundle.

ronisaha avatar ronisaha commented on June 3, 2024

@wh1pp3rz Could you please confirm the latest odm branch is working for both ORM and Document ? After your confirmation i'll merge it to master branch.

from easyauditbundle.

pribeirojtm avatar pribeirojtm commented on June 3, 2024

Hi @ronisaha. Since this feature has lot of changes, is it still in master branch to get more matured, and to be better tested? It is kind of experimental feature?
I don't have rush in having this feature since I'm currently only using ORM, I'm just asking if is it planned to release a new tag regarding this.
Thanks.

from easyauditbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.