private function encryptFile(EncryptableFile $fileEntity, KeyData $keyData)
    {
        $file = $fileEntity->getFile();
        $encrypt = false;

        if ($file) {
            $filePath = $file->getRealPath();
            $encrypt = true;
        } elseif ($fileEntity->getId() && $fileEntity->fileExists() && !$fileEntity->isFileEncrypted()) {
            // The document was persisted but somehow the file was not encrypted
            $filePath = $fileEntity->getAbsolutePath();
            $encrypt = true;
        }

        if ($encrypt) {
            $fileContent = file_get_contents($filePath);
            // Get the encryption key data
            if ($keyData) {
                $encType = CryptographyProviderInterface::FILE_ENCRYPTION;
                $encryptedContent = $this->cryptographyProvider->encrypt($fileContent, $keyData, $encType);

                // Replace the file content with the encrypted
                file_put_contents($filePath, $encryptedContent);
                $fileEntity->setFileEncrypted(true);
            }
        }
    }