Processes an entity if it has encryption enabled and it's not already processed

@param mixed $entity
@param string $operation

@return mixed
344866
private function processEntity($entity, $operation)
    {
        if ($this->settings[$operation.'_on_backend']) {

            $reflection = ClassUtils::newReflectionObject($entity);
            $classMetadata = $this->getEncryptionMetadataFor($reflection->getName());
            if ($classMetadata->encryptionEnabled && $this->toProcess($entity, $operation)) {
                // Get the encryption key data
                $keyData = $this->keyManager->getEntityEncryptionKeyData($entity);
                if ($keyData) {
                    // get the encrypted fields
                    $encryptionEnabledFields = $this->getEncryptionEnabledFields($reflection);

                    // Encrypt the fields
                    foreach ($encryptionEnabledFields as $fieldName => $field) {
                        $fieldEncrypter = $this->getFieldEncrypter($field, $reflection);
                        $value = $this->getFieldValue($entity, $field);
                        $processedValue = $fieldEncrypter->{$operation}($value, $keyData);
                        $this->setFieldValue($entity, $field, $processedValue);
                    }

                    // Set the encryption flag
                    $entity->setEncrypted($operation === self::ENCRYPT);

                    // If this entity has an encryptable file, process it
                    if ($this->isEncryptableFile($entity) && $this->toProcessFile($entity, $operation)) {
                        $method = $operation.'File';
                        $this->{$method}($entity, $keyData);
                    }
                }
            }
        }
    }
1059895
private function isEncryptableFile($entity)
    {
        $reflection = ClassUtils::newReflectionObject($entity);
        $classMetadata = $this->getEncryptionMetadataFor($reflection->getName());

        return $classMetadata->encryptionEnabled && $classMetadata->encryptedFile;
    }
532413
public function deepCloneAndSave($entity)
    {
        $clonedEntity = clone $entity;
        $this->eventDispatcher->dispatch(Events::DEEP_CLONE_AND_SAVE, new DeepCloneAndSaveEvent($entity, $clonedEntity, $this->em));

        $this->em->persist($clonedEntity);
        $this->em->flush();

        $this->eventDispatcher->dispatch(Events::POST_DEEP_CLONE_AND_SAVE, new DeepCloneAndSaveEvent($entity, $clonedEntity, $this->em));

        return $clonedEntity;
    }
446351
public function isEncryptable(EntityInterface $entity): bool
    {
        $enabled = $this->getConfig('enabled');
        if (is_callable($enabled)) {
            $enabled = $enabled($entity);
            if (!is_bool($enabled)) {
                throw new RuntimeException('Condition callable must return a boolean.');
            }
        }

        return $enabled;
    }
296038
public function removeOperation(OperationInterface $operation)
    {
        if ($this->operations->contains($operation)) {
            $this->operations->removeElement($operation);
        }

        return $this;
    }
1092237
public function eql($entity)
    {
        if ($entity instanceof AnDomainEntityProxy) {
            $object = $entity->getObject();
        } elseif ($entity instanceof AnMixinAbstract) {
            $object = $entity->getMixer();
        } else {
            $object = $entity;
        }

        return $this === $object;
    }
45373
public function processContent(Operation $operation, $data)
    {
        try {
            if(is_array($data)) {
                $message = $this->contentService->getMessageByOperation($operation);
                $message->setMessage($data['message']);
            } else {
                $message = $data;            
            }
        } catch (\Exception $e){
            // message has not been created yet, so do it from the form data.
            $message = $data;
        }
        return $message;
    }
838149
private function processFields($entity, $isEncryptOperation = true) {
        $encryptorMethod = $isEncryptOperation ? 'encrypt' : 'decrypt';
        $reflectionClass = new ReflectionClass($entity);
        $properties = $reflectionClass->getProperties();
        $withAnnotation = false;
        foreach ($properties as $refProperty) {
            if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
                $withAnnotation = true;
                // we have annotation and if it decrypt operation, we must avoid duble decryption
                $propName = $refProperty->getName();
                if ($refProperty->isPublic()) {
                    $entity->$propName = $this->encryptor->$encryptorMethod($refProperty->getValue());
                } else {
                    $methodName = self::capitalize($propName);
                    if ($reflectionClass->hasMethod($getter = 'get' . $methodName) && $reflectionClass->hasMethod($setter = 'set' . $methodName)) {
                        $currentPropValue = $this->encryptor->$encryptorMethod($entity->$getter());
                        $entity->$setter($currentPropValue);
                    } else {
                        throw new \RuntimeException(sprintf("Property %s isn't public and doesn't has getter/setter"));
                    }
                }
            }
        }

        return $withAnnotation;
    }
1206314
public function process(Transaction $transaction, $operation)
    {
        $response = parent::process($transaction, $operation);

        if ($response instanceof FailureResponse && $operation == Operation::CANCEL
            && $transaction->getBackendOperationForRefund()) {
            return parent::process($transaction, Operation::REFUND);
        } else {
            return $response;
        }
    }
1192489
private function runOperation(Operation $operation)
    {
        try {
            return $this->runner->run($operation);
        } catch (\Throwable $e) {
            // treated below
        } catch (\Exception $e) {
            // treated below
        }

        // @todo keep only inner if once ExceptionCatcher is gone
        if ($this->exceptionCatcherVoter instanceof ThrowableCatcherVoter) {
            if (!$this->exceptionCatcherVoter->shouldCatchThrowable($e)) {
                throw $e;
            }
        } elseif (!$this->exceptionCatcherVoter->shouldCatch($e)) {
            throw $e;
        }

        try {
            $this->waitStrategy->wait();
        } catch (WaiterException $waiterException) {
            throw $e;
        }

        return $this->runOperation($operation);
    }
797192
private function decryptSymmetricKey($entity)
    {
        $decryptedKey = null;

        $encryptionMode = $this->getEncryptionMode($entity);
        switch ($encryptionMode) {
            case EncryptionService::MODE_PER_USER_SHAREABLE:
                $decryptedKey = $this->decryptSymmetricKeyWithUserKey($entity);
                break;
            case EncryptionService::MODE_SYSTEM_ENCRYPTION:
                $decryptedKey = $this->decryptSymmetricKeyWithSystemKey($entity);
                break;
            default:
                throw new EncryptionException(sprintf('Encryption mode %s not supported.', $encryptionMode));
        }

        return $decryptedKey;
    }
476273
private static function decrypt_entity( FS_Entity $entity ) {
            $clone = clone $entity;
            $props = get_object_vars( $entity );

            foreach ( $props as $key => $val ) {
                $clone->{$key} = self::_decrypt( $val );
            }

            return $clone;
        }
906162
protected function executeOperation(LdapOperationInterface $operation, LogOperation $log = null)
    {
        $lastServer = $this->connection->getServer();
        try {
            $this->connectIfNotBound($operation);
            $lastServer = $this->connection->getServer();
            $handler = $this->getOperationHandler($operation);
            $handler->setOperationDefaults($operation);
            $this->logStart($log);
            $this->invalidateCacheIfNeeded($operation);
            list($result, $usedCache, $cacheKey) = $this->getCacheOrHandlerResult($operation, $handler, $log);
            $this->cacheResultIfNeeded($operation, $result, $usedCache, $cacheKey);

            return $result;
        } catch (\Throwable $e) {
            $this->logExceptionAndThrow($e, $log);
        } catch (\Exception $e) {
            $this->logExceptionAndThrow($e, $log);
        } finally {
            $this->logEnd($log);
            // It would not have set controls or switched servers if the cache was used...
            if (isset($usedCache) && $usedCache === false) {
                $this->resetLdapControls($operation);
                $this->switchServerIfNeeded($this->connection->getServer(), $lastServer, $operation);
            }
            $this->dispatcher->dispatch(new LdapOperationEvent(Event::LDAP_OPERATION_EXECUTE_AFTER, $operation, $this->connection));
        }
    }
373231
private function runOperation(PromiseOperation $operation)
    {
        $promise = $operation->getPromise();

        return $promise->then(
            $this->onTerminate($operation, $this->fulfilledVoter, true, $promise),
            $this->onTerminate($operation, $this->rejectedVoter, false, $promise)
        );
    }
1342239
private function forEachChunk(ObjectManager $objectManager, array $entities, $operation)
    {
        $batch = [];
        foreach (array_chunk($entities, $this->configuration['batchSize']) as $chunk) {
            $searchableEntitiesChunk = [];
            foreach ($chunk as $entity) {
                $entityClassName = ClassUtils::getClass($entity);

                $searchableEntitiesChunk[] = new SearchableEntity(
                    $this->getFullIndexName($entityClassName),
                    $entity,
                    $objectManager->getClassMetadata($entityClassName),
                    $this->normalizer,
                    ['useSerializerGroup' => $this->canUseSerializerGroup($entityClassName)]
                );
            }

            $batch[] = $operation($searchableEntitiesChunk);
        }

        return $this->formatBatchResponse($batch);
    }
167919
protected function _createVersions(Table $table, $entity, array $operations) {
		$Storage = StorageManager::adapter($entity['adapter']);
		$path = $this->_buildPath($entity, true);
		$tmpFile = $this->_tmpFile($Storage, $path, TMP . 'image-processing');

		foreach ($operations as $version => $imageOperations) {
			$hash = StorageUtils::hashOperations($imageOperations);
			$string = $this->_buildPath($entity, true, $hash);

			if ($this->adapterClass === 'AmazonS3' || $this->adapterClass === 'AwsS3') {
				$string = str_replace('\\', '/', $string);
			}

			if ($Storage->has($string)) {
				return false;
			}

			try {
				$image = $table->processImage($tmpFile, null, ['format' => $entity['extension']], $imageOperations);
				$Storage->write($string, $image->get($entity['extension']), true);
			} catch (\Exception $e) {
				$this->log($e->getMessage());
				unlink($tmpFile);
				throw $e;
			}
		}

		unlink($tmpFile);
	}
1095555
public function processEntity(string $entity, callable $callback = null): array
    {
        $this->dbUtils->assertTableExists($entity);

        $this->priKey = $this->dbUtils->getPrimaryKey($entity);
        $this->entityCols = $this->dbUtils->getTableCols($entity);
        $this->entity = $entity;

        $actionsOnThatEntity = $this->whatToDoWithEntity();
        $this->queries = [];

        // Prepare CSV
        if ($this->mode === 'batch') {
            $this->csv = new CSVWriter();
            $this->csv->setCsvControl('|', $this->dbHelper->getEnclosureForCSV());
        }

        // Wrap everything in a transaction
        $conn = $this->dbUtils->getConn();
        try {
            $conn->beginTransaction();

            if ($actionsOnThatEntity & self::UPDATE_TABLE) {
                $this->updateData($callback);
            }

            if ($actionsOnThatEntity & self::INSERT_TABLE) {
                $this->insertData($callback);
            }

            $conn->commit();
        } catch (\Exception $e) {
            $conn->rollBack();
            $conn->close(); // To avoid locks

            throw $e;
        }

        return $this->queries;
    }