<commit_before><?php

namespace WidgetsNL\DoctrineEncryptionBundle\EventListener;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Event\PreFlushEventArgs;
use WidgetsNL\DoctrineEncryptionBundle\Algorithm\Aes;
use WidgetsNL\DoctrineEncryptionBundle\Mapping\Encrypt;
use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use ReflectionClass;

class DoctrineEncryptionEvent implements EventSubscriber
{
    const ANNOTATION_CLASS = Encrypt::class;

    /**
     * Annotation reader
     * @var \Doctrine\Common\Annotations\Reader
     */
    private $annReader;

    /**
     * Algoritm
     */
    private $algoritm;

    public function __construct(Reader $annReader)
    {
        $this->annReader = $annReader;
        $this->algoritm  = new Aes('random');
    }


    /**
     * Returns an array of events this subscriber wants to listen to.
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return [
            Events::preFlush,
            Events::postLoad,
        ];
    }

    public function postLoad(LifecycleEventArgs $args)
    {
        $this->decrypt($args->getEntity());
    }

    public function preFlush(PreFlushEventArgs $preFlushEventArgs) {
        $unitOfWork = $preFlushEventArgs->getEntityManager()->getUnitOfWork();
        foreach($unitOfWork->getScheduledEntityInsertions() as $entity) {
            $this->encrypt($entity);
        }
    }


    private function encrypt($entity)
    {
        if (strstr(get_class($entity), "Proxies")) {
            $class = ClassUtils::getClass($entity);
        } else {
            $class = get_class($entity);
        }<commit_msg>Added postFlush for using the entity after the flush<commit_after><?php

namespace WidgetsNL\DoctrineEncryptionBundle\EventListener;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use WidgetsNL\DoctrineEncryptionBundle\Algorithm\Aes;
use WidgetsNL\DoctrineEncryptionBundle\Mapping\Encrypt;
use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use ReflectionClass;

class DoctrineEncryptionEvent implements EventSubscriber
{
    const ANNOTATION_CLASS = Encrypt::class;

    /**
     * Annotation reader
     * @var \Doctrine\Common\Annotations\Reader
     */
    private $annReader;

    /**
     * Algoritm
     */
    private $algoritm;

    public function __construct(Reader $annReader)
    {
        $this->annReader = $annReader;
        $this->algoritm  = new Aes('random');
    }


    /**
     * Returns an array of events this subscriber wants to listen to.
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return [
            Events::preFlush,
            Events::postFlush,
            Events::postLoad,
        ];
    }

    public function postLoad(LifecycleEventArgs $args)
    {
        $this->decrypt($args->getEntity());
    }

    public function preFlush(PreFlushEventArgs $preFlushEventArgs) {
        $unitOfWork = $preFlushEventArgs->getEntityManager()->getUnitOfWork();
        foreach($unitOfWork->getScheduledEntityInsertions() as $entity) {
            $this->encrypt($entity);
        }
    }
    public function postFlush(PostFlushEventArgs $postFlushEventArgs) {
        $unitOfWork = $postFlushEventArgs->getEntityManager()->getUnitOfWork();
        foreach($unitOfWork->getIdentityMap() as $identityMap) {
            foreach ($identityMap as $entity){
                $this->decrypt($entity);
            }
        }
    }


    private function encrypt($entity)
    {
        if (strstr(get_class($entity), "Proxies")) {
            $class = ClassUtils::getClass($entity);
        } else {
            $class = get_class($entity);
        }
{'fraction_non_alphanumeric': 0.10843672456575683, 'fraction_numerical': 0.0, 'mean_word_length': 2.8946859903381643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '356141', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1163, 'n_words': 211}