/**
  * Listen a preUpdate lifecycle event. Checking and encrypt entities fields
  * which have @Encrypted annotation. Using changesets to avoid preUpdate event
  * restrictions.
  *
  * @param LifecycleEventArgs $args
  */
 public function preUpdate(PreUpdateEventArgs $args)
 {
     $reflectionClass = new ReflectionClass($args->getEntity());
     $properties = $reflectionClass->getProperties();
     foreach ($properties as $refProperty) {
         if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
             $propName = $refProperty->getName();
             if ($args->hasChangedField($propName)) {
                 $args->setNewValue($propName, $this->encryptor->encrypt($args->getNewValue($propName)));
             }
         }
     }
 }
 /**
  * Listen a preUpdate lifecycle event. Checking and encrypt entities fields
  * which have @Encrypted annotation. Using changesets to avoid preUpdate event
  * restrictions
  * 
  * @param LifecycleEventArgs $args 
  * 
  * @return void
  * @access public
  * @author etienne de Longeaux <*****@*****.**>
  */
 public function preUpdate(PreUpdateEventArgs $args)
 {
     if ($this->_update_enabled == true) {
         $entity = $args->getEntity();
         $em = $args->getEntityManager();
         $uow = $em->getUnitOfWork();
         $reflectionClass = new ReflectionClass($args->getEntity());
         $properties = $reflectionClass->getProperties();
         $className = get_class($entity);
         foreach ($properties as $refProperty) {
             foreach ($this->options as $key => $encrypter) {
                 if (isset($encrypter['encryptor_annotation_name']) && isset($encrypter['encryptor_class']) && isset($encrypter['encryptor_options'])) {
                     $this->encryptor = $this->getEncryptorService($key);
                     if ($this->annReader->getPropertyAnnotation($refProperty, $encrypter['encryptor_annotation_name'])) {
                         // we have annotation and if it decrypt operation, we must avoid duble decryption
                         $propName = $refProperty->getName();
                         // we encrypt the field
                         if ($refProperty->isPublic()) {
                             $entity->{$propName} = $this->encryptor->encrypt($refProperty->getValue());
                         } else {
                             $methodName = \Sfynx\ToolBundle\Util\PiStringManager::capitalize($propName);
                             if ($reflectionClass->hasMethod($getter = 'get' . $methodName) && $reflectionClass->hasMethod($setter = 'set' . $methodName)) {
                                 // we get the locale value
                                 $locale = false;
                                 $om = $args->getObjectManager();
                                 $object = $args->getObject();
                                 $meta = $om->getClassMetadata(get_class($object));
                                 $config = $this->getConfiguration($om, $meta->name);
                                 if (isset($config['fields'])) {
                                     $locale = $this->getTranslatableLocale($object, $meta);
                                 }
                                 // we set the encrypt value
                                 $currentPropValue = $entity->{$getter}();
                                 if (!empty($currentPropValue)) {
                                     $currentPropValue = $this->encryptor->encrypt($currentPropValue);
                                 }
                                 // we set locale value
                                 if ($locale) {
                                     //                                        if ($locale == $this->locale) {
                                     //                                            $entity->$setter($currentPropValue);
                                     //                                        }
                                     $entity->{$setter}($currentPropValue);
                                     $entity->translate($locale)->{$setter}($currentPropValue);
                                     //$uow->persist($entity);
                                     //$uow->computeChangeSets();
                                 }
                             } else {
                                 throw new \RuntimeException(sprintf("Property %s isn't public and doesn't has getter/setter"));
                             }
                         }
                     }
                 } else {
                     throw new \RuntimeException(sprintf("encrypter is not correctly configured"));
                 }
             }
         }
     }
 }