Skip to content

Commit

Permalink
Merge pull request #53 from openeuropa/EWPP-2210
Browse files Browse the repository at this point in the history
EWPP-2210: Optimize performance for sanitize query.
  • Loading branch information
upchuk authored May 16, 2022
2 parents 497b7d4 + 136851a commit e58d9cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion oe_contact_forms.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ services:
- { name: access_check, applies_to: _oe_contact_forms_access_check }
oe_contact_forms.contact.sanitize_commands:
class: Drupal\oe_contact_forms\Commands\sql\ContactFormSanitizeCommand
arguments: ['@entity_type.manager']
arguments: ['@entity_type.manager', '@database']
tags:
- { name: drush.command }
55 changes: 42 additions & 13 deletions src/Commands/sql/ContactFormSanitizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\oe_contact_forms\Commands\sql;

use Drupal\Core\Database\Connection;
use Drush\Commands\DrushCommands;
use Drush\Drupal\Commands\sql\SanitizePluginInterface;
use Consolidation\AnnotatedCommand\CommandData;
Expand All @@ -22,15 +23,25 @@ class ContactFormSanitizeCommand extends DrushCommands implements SanitizePlugin
*/
protected $entityTypeManager;

/**
* The database connection to use.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* SanitizeContactFormFieldsCommands constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Database\Connection $connection
* The database connection to use.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $connection) {
parent::__construct();
$this->entityTypeManager = $entityTypeManager;
$this->connection = $connection;
}

/**
Expand All @@ -47,18 +58,36 @@ public function sanitize($result, CommandData $commandData) {
foreach ($contact_forms as $key => $contact_form) {
$is_corporate_form = $contact_form->getThirdPartySetting('oe_contact_forms', 'is_corporate_form', FALSE);
if ($is_corporate_form) {
$messages = $this->entityTypeManager->getStorage('contact_message')->loadByProperties(['contact_form' => $contact_form->id()]);
foreach ($messages as $message) {
$message->set('name', 'User' . $message->id());
$message->set('mail', 'user+' . $message->id() . '@example.com');
$message->set('subject', 'subject-by-' . $message->id());
$message->set('message', 'message-by-' . $message->id());
$message->set('oe_country_residence', 'residence-in-' . $message->id());
$message->set('oe_telephone', '+000-' . $message->id());
$message->set('oe_topic', 'topic-' . $message->id());
$message->set('ip_address', '127.0.0.1');
$message->save();
}
$this->connection->update('contact_message')
->expression('name', 'CONCAT(:name_dummy_string, id)', [
':name_dummy_string' => 'User',
])
->expression('mail', 'CONCAT(:mail_dummy_string, id, :maildomain_dummy_string)', [
':mail_dummy_string' => 'user+',
':maildomain_dummy_string' => '@example.com',
])
->expression('subject', 'CONCAT(:subject_dummy_string, id)', [
':subject_dummy_string' => 'subject-by-',
])
->expression('message', 'CONCAT(:message_dummy_string, id)', [
':message_dummy_string' => 'message-by-',
])
->expression('oe_country_residence', 'CONCAT(:residence_dummy_string, id)', [
':residence_dummy_string' => 'residence-in-',
])
->expression('oe_telephone', 'CONCAT(:telephone_dummy_string, id)', [
':telephone_dummy_string' => '+000-',
])
->expression('oe_topic', 'CONCAT(:topic_dummy_string, id)', [
':topic_dummy_string' => 'topic-',
])
->fields(['ip_address' => '127.0.0.1'])
->condition('contact_form', $contact_form->id())
->execute();

// Make sure that we don't have sensitive data of contact messages
// in the cache.
$this->entityTypeManager->getStorage('contact_message')->resetCache();

$topics = $contact_form->getThirdPartySetting('oe_contact_forms', 'topics', []);
foreach ($topics as $key => $topic) {
Expand Down

0 comments on commit e58d9cf

Please sign in to comment.